Android 设备进入特定范围时 Ibeacons 发送通知
Ibeacons sending notification when Android device comes in specific range
在我的项目中,我想在安装了 android 应用程序的 android 设备上显示通知。
我真正想做的是,假设有人进入商店,我想向他显示欢迎通知或 MySQL 数据库中的任何其他文本。
有可能这样做吗?我进行了很多搜索,但对任何解决方案都没有留下深刻的印象。
谁能给我正确的提示和代码。
是的,可以这样做,而且这是很常见的事情。要解决此问题,您需要了解两个关键事项:
您需要将应用设置为检测信标并在检测到信标时发送通知。您可以在此处查看如何使用 Android Beacon Library 执行此操作的示例:
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());
}
}
您可以在项目网站上阅读更多关于使用 Android 信标库检测信标的详细信息。
- 您需要设置一个 SQL 数据库来存储您的信标消息,然后修改上面的
sendNotification
方法以在该数据库中查询信标标识符并显示正确的文本。 Android 开发者网站有一个很好的教程,介绍如何在 SQL 中存储日期并在此处检索它:
https://developer.android.com/training/basics/data-storage/databases.html
在我的项目中,我想在安装了 android 应用程序的 android 设备上显示通知。 我真正想做的是,假设有人进入商店,我想向他显示欢迎通知或 MySQL 数据库中的任何其他文本。 有可能这样做吗?我进行了很多搜索,但对任何解决方案都没有留下深刻的印象。 谁能给我正确的提示和代码。
是的,可以这样做,而且这是很常见的事情。要解决此问题,您需要了解两个关键事项:
您需要将应用设置为检测信标并在检测到信标时发送通知。您可以在此处查看如何使用 Android Beacon Library 执行此操作的示例:
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()); } }
您可以在项目网站上阅读更多关于使用 Android 信标库检测信标的详细信息。
- 您需要设置一个 SQL 数据库来存储您的信标消息,然后修改上面的
sendNotification
方法以在该数据库中查询信标标识符并显示正确的文本。 Android 开发者网站有一个很好的教程,介绍如何在 SQL 中存储日期并在此处检索它:
https://developer.android.com/training/basics/data-storage/databases.html