【发布时间】:2017-12-08 22:03:42
【问题描述】:
我目前有一个应用程序设置来使用 Azure 通知中心接收远程通知。 现在,我想扫描 iBeacons,查看附近是否有特定的 iBeacons,如果是,则不应向用户显示通知。但是,如果信标不在视线范围内,用户应该会收到此通知。
基本上我希望信标禁止此应用的通知。
要怎么做呢?
【问题讨论】:
标签: java android android-studio ibeacon azure-notificationhub
我目前有一个应用程序设置来使用 Azure 通知中心接收远程通知。 现在,我想扫描 iBeacons,查看附近是否有特定的 iBeacons,如果是,则不应向用户显示通知。但是,如果信标不在视线范围内,用户应该会收到此通知。
基本上我希望信标禁止此应用的通知。
要怎么做呢?
【问题讨论】:
标签: java android android-studio ibeacon azure-notificationhub
基于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 Beacon 库在自定义Android 应用程序类中实现,如下所示。您需要阅读有关如何设置该库以使其工作的更多信息。您还需要在 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。但是您可以根据您的要求对其进行更改以使其更加复杂。
【讨论】:
您可以使用一些开源库来处理信标。例如,我使用了 Altbeacon 库。 这是样本:https://altbeacon.github.io/android-beacon-library/samples.html 对于您的目标,您需要在 Activity 或 Service 上实现 BeaconConsumer 接口。它有一个 onBeaconServiceConnect() 方法。实现示例:
@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) { }
}
【讨论】: