简体   繁体   English

收到远程通知后扫描iBeacon

[英]Scan for iBeacon upon receiving remote notification

I currently have an app setup to receive remote notifications using Azure Notification Hub. 我目前有一个应用程序设置,可以使用Azure Notification Hub接收远程通知。 Now, I would like to scan for iBeacons, see if a specific one is close by and if so, the notification should not be shown to the user. 现在,我想扫描iBeacons,看看附近是否有特定的iBeacons,如果是,则不应将通知显示给用户。 However, if the beacon isn't in sight, the user should receive this notification. 但是,如果看不到信标,则用户应收到此通知。

Basically I want the beacon to supress the notifications for this app. 基本上,我希望信标禁止该应用程序的通知。

How would one go about doing this? 人们将如何去做呢?

Based on the docs from Azure , when a remote notification comes in, you get a callback like this: 根据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
    ...
  }

} }

If you want to filter the notifications based on what beacons are present, you can add that logic to the onReceive method like this: 如果要基于存在的信标来过滤通知,可以将该逻辑添加到onReceive方法中,如下所示:

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

The above isBeaconVisible() could be implemented in a custom Android Application class using the Android Beacon Library with something like below. 上面的isBeaconVisible()可以使用Android Beacon库在自定义Android Application类中实现,如下所示。 You'll need to read more about how to set up that library to make this work. 您需要阅读更多有关如何设置该库以使其工作的信息。 You'll also need to register the custom Application class in your AndroidManifest.xml. 您还需要在AndroidManifest.xml中注册自定义Application类。

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;
    }
}

The above logic for isBeaconVisible() returns true if any beacon with any identifier has been seen in the last second. 如果在最后一秒钟内看到带有任何标识符的isBeaconVisible()标,则上述isBeaconVisible()逻辑将返回true。 But you can alter this to make it more sophisticated per your requirements. 但是您可以更改此设置,使其根据您的要求变得更加复杂。

You can use some opensource library to work with beacons. 您可以使用一些开源库来使用信标。 I used Altbeacon library for example. 我以Altbeacon库为例。 Here is the samples : https://altbeacon.github.io/android-beacon-library/samples.html For your target you need to implement BeaconConsumer interface on Activity or Service. 这是示例: https ://altbeacon.github.io/android-beacon-library/samples.html对于您的目标,您需要在Activity或Service上实现BeaconConsumer接口。 It has a method onBeaconServiceConnect(). 它具有onBeaconServiceConnect()方法。 Example of implementation: 实现示例:

    @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) {    }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM