我希望在我的应用程序处于打开状态但是处于后台时有推送通知。现在我已经更改了Estimote演示,当我的应用程序处于前台时,我的应用程序会给我一个通知,这个用户并没有太多用处。 我在这里发布了我的NotifyDemoActivity类代码,一旦打开应用程序就会调用它
public class NotifyDemoActivity extends Activity {
private static final String TAG = NotifyDemoActivity.class.getSimpleName();
private static final int NOTIFICATION_ID = 123;
private BeaconManager beaconManager;
private NotificationManager notificationManager;
private Region region;
private long[] mVibratePattern = { 0, 200, 200, 300 };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notify_demo);
getActionBar().setDisplayHomeAsUpEnabled(true);
beacon.getMinor());
region = new Region("rid", null, null, null);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
beaconManager = new BeaconManager(this);
beaconManager.setBackgroundScanPeriod(TimeUnit.SECONDS.toMillis(1), 0);
beaconManager.setMonitoringListener(new MonitoringListener() {
@Override
public void onEnteredRegion(Region region, List<Beacon> beacons) {
postNotification("Entered region");
}
@Override
public void onExitedRegion(Region region) {
postNotification("Exited region");
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onResume() {
super.onResume();
notificationManager.cancel(NOTIFICATION_ID);
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
@Override
public void onServiceReady() {
try {
beaconManager.startMonitoring(region);
} catch (RemoteException e) {
Log.d(TAG, "Error while starting monitoring");
}
}
});
}
@Override
protected void onDestroy() {
notificationManager.cancel(NOTIFICATION_ID);
beaconManager.disconnect();
super.onDestroy();
}
private void postNotification(String msg) {
Intent notifyIntent = new Intent(NotifyDemoActivity.this, NotifyDemoActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivities(
NotifyDemoActivity.this,
0,
new Intent[]{notifyIntent},
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(NotifyDemoActivity.this)
.setSmallIcon(R.drawable.beacon_gray)
.setContentTitle("Notify Demo")
.setContentText(msg)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setVibrate(mVibratePattern)
.build();
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notificationManager.notify(NOTIFICATION_ID, notification);
TextView statusTextView = (TextView) findViewById(R.id.status);
statusTextView.setText(msg);
}
}
答案 0 :(得分:3)
您应该在应用程序类中保留BeaconManager
而不是活动。
活动将被停止,销毁,BeaconManager
将停止监控。另一方面,申请仍将保留参考,并将继续监督。
在监控应用程序类时发现信标时,可以发布通知。当用户决定点击它时,它会触发一些活动。
答案 1 :(得分:0)
您应该创建服务。
在您的代码中,您应该做一些更改。
在
beaconManager = new BeaconManager(this);
你应该开始&#34;信标服务&#34;
if (!beaconManager.isBluetoothEnabled()) {
stopSelf();
}
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
@Override
public void onServiceReady() {
try {
beaconManager.startRanging(ALL_ESTIMOTE_BEACONS_REGION);
} catch (RemoteException e) {
Log.e("error", "Cannot start ranging", e);
}
}
});
同时检查设备上的蓝牙是否处于活动状态。
在过程beaconManager.setMonitoringListener中(新的MonitoringListener()添加命令以创建通知。