有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在收到远程通知时对iBeacon进行java扫描

我目前有一个应用程序设置,可以使用Azure通知中心接收远程通知。 现在,我想扫描iBeacons,看看附近是否有一个特定的iBeacons,如果是这样,通知不应该显示给用户。但是,如果看不到信标,用户应该会收到此通知

基本上,我希望信标抑制此应用程序的通知

一个人怎么做呢


共 (2) 个答案

  1. # 1 楼答案

    您可以使用一些开源库来处理信标。例如,我使用了Altbeacon库。 以下是示例:https://altbeacon.github.io/android-beacon-library/samples.html 对于您的目标,您需要在活动或服务上实现BeaconConsumer接口。它在BeaConserviceConnect()上有一个方法。实施示例:

        @Override
        public void onBeaconServiceConnect() {
            beaconManager.addRangeNotifier(new RangeNotifier() {
                @Override 
                public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
                    if (beacons.size() == 0) {
                        Log.i(TAG, "Show your notification here");        
                    }
                }
            });
    
            try {
                beaconManager.startRangingBeaconsInRegion(new Region("someRangingUniqueId", null, null, null));
            } catch (RemoteException e) {    }
        }
    
  2. # 2 楼答案

    基于the docs from Azure,当收到远程通知时,您会收到如下回调:

    public class MyHandler extends NotificationsHandler {
      public static final int NOTIFICATION_ID = 1;
      private NotificationManager mNotificationManager;
      NotificationCompat.Builder builder;
      Context ctx;
    
      @Override
      public void onReceive(Context context, Bundle bundle) {
        ctx = context;
        String nhMessage = bundle.getString("message");
        sendNotification(nhMessage);
        if (MainActivity.isVisible) {
            MainActivity.mainActivity.ToastNotify(nhMessage);
        }
      }
    
      private void sendNotification(String msg) {
        // put your notification code here
        ...
      }
    

    }

    如果要根据存在的信标筛选通知,可以将该逻辑添加到onReceive方法中,如下所示:

      public void onReceive(Context context, Bundle bundle) {
         if (!(MyApplication)this.getApplication()).isBeaconVisible()) {
            // Suppress notification by returning early from method
            return;
         }
         ...
      }
    

    上面的isBeaconVisible()可以在定制的Android应用程序类中使用Android Beacon库实现,如下所示。您需要阅读更多有关如何设置该库以使其正常工作的信息。您还需要在AndroidManifest中注册自定义应用程序类。xml

    public class MyApplication extends Application implements BeaconConsumer, RangeNotifier {
    
        public Collection<Beacon> mVisibleBeacons;
    
        public void onCreate() {
            super.onCreate();
            BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
            // TODO: look up the proper I_BEACON_LAYOUT in a google search
            beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(I_BEACON_LAYOUT));
            beaconManager.addRangeNotifier(this);
    
        }
    
        @Override
        public void onBeaconServiceConnect() {
            BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
            try {
                beaconManager.startRangingBeaconsInRegion(new Region("all-beacons", null, null, null));
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    
        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
            mVisibleBeacons = beacons;
        }
    
        public boolean isBeaconVisible() {
            return mVisibleBeacons.size() > 0;
        }
    }
    

    如果在最后一秒钟看到任何具有任何标识符的信标,则上述isBeaconVisible()逻辑返回true。但您可以根据自己的需求对其进行修改,使其更加复杂