Ibeacons在Android设备进入特定范围时发送通知
在我的项目中,我想在安装了android应用程序的android设备上显示通知。 我实际上想做的是,假设某人进入商店,我想向他显示欢迎通知或MySQL数据库中的任何其他文本。 有可能吗?我已经搜索了很多,但是我对任何解决方案都没有很好的印象。Ibeacons在Android设备进入特定范围时发送通知,android,bluetooth-lowenergy,android-notifications,iot,ibeacon-android,Android,Bluetooth Lowenergy,Android Notifications,Iot,Ibeacon Android,在我的项目中,我想在安装了android应用程序的android设备上显示通知。 我实际上想做的是,假设某人进入商店,我想向他显示欢迎通知或MySQL数据库中的任何其他文本。 有可能吗?我已经搜索了很多,但是我对任何解决方案都没有很好的印象。 任何人都可以给我正确的提示和代码。是的,这样做是可能的,这是一件常见的事情。要解决此问题,您需要知道两件关键的事情: 您需要将应用程序设置为检测信标,并在检测到信标时发送通知。您可以在此处看到如何使用的示例: 您可以在项目网站上阅读更多有关使用Andro
任何人都可以给我正确的提示和代码。是的,这样做是可能的,这是一件常见的事情。要解决此问题,您需要知道两件关键的事情:
sendNotification
方法,以查询该数据库中的信标标识符并显示正确的文本。Android开发者网站提供了一个很好的教程,介绍如何在SQL中存储日期并在此处检索:MonitoringActivity.class的用途和包含的stackBuilder.addNextIntent(新意图(此,MonitoringActivity.class));很抱歉,您不需要该代码,它是意外包含的。我从示例中删除了它。感谢您的帮助,实际上我正在使用iBeacon,因此需要设置beaconParser。最终它成功了
public class BeaconNotificationApplication extends Application implements BootstrapNotifier {
private static final String TAG = "BeaconReferenceApp";
private RegionBootstrap regionBootstrap;
private BackgroundPowerSaver backgroundPowerSaver;
public void onCreate() {
super.onCreate();
BeaconManager beaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this);
// By default the AndroidBeaconLibrary will only find AltBeacons. If you wish to make it
// find a different type of beacon, you must specify the byte layout for that beacon's
// advertisement with a line like below. The example shows how to find a beacon with the
// same byte layout as AltBeacon but with a beaconTypeCode of 0xaabb. To find the proper
// layout expression for other beacon types, do a web search for "setBeaconLayout"
// including the quotes.
//
//beaconManager.getBeaconParsers().clear();
//beaconManager.getBeaconParsers().add(new BeaconParser().
// setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
// wake up the app when a beacon is seen
Region region = new Region("backgroundRegion",
null, null, null);
regionBootstrap = new RegionBootstrap(this, region);
backgroundPowerSaver = new BackgroundPowerSaver(this);
}
@Override
public void didEnterRegion(Region arg0) {
sendNotification();
}
@Override
public void didExitRegion(Region region) {
}
@Override
public void didDetermineStateForRegion(int state, Region region) {
}
private void sendNotification() {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setContentTitle("Beacon Reference Application")
.setContentText("An beacon is nearby.")
.setSmallIcon(R.drawable.ic_launcher);
NotificationManager notificationManager =
(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
}
}