android 信标应用中的通知

Notification in android beacon app

我正在 Android 开发 Beacon 应用程序。我想要一个功能,当应用程序进入信标区域时,即使应用程序在 android.When 中不是 运行,它也会退出,然后“区域退出然后再次退出”通知应该 come.I 当应用程序不在后台 运行 时,我不知道如何在顶部栏中显示通知。 此外,当我点击通知时,它应该会带我到信标屏幕。 请帮助我,我是新手。在此先感谢 这是我的代码

         public class NotifyDemoActivity extends BaseActivity {

         private static final String TAG =NotifyDemoActivity.class.getSimpleName();
          private static final int NOTIFICATION_ID = 123;

           private BeaconManager beaconManager;
             private NotificationManager notificationManager;
            private Region region;

             @Override protected int getLayoutResId() {
              return R.layout.notify_demo;
               }

             @Override
              protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);

            Beacon beacon = getIntent().getParcelableExtra(ListBeaconsActivity.EXTRAS_BEACON);
region = new Region("regionId", beacon.getProximityUUID(), beacon.getMajor(), beacon.getMinor());
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
beaconManager = new BeaconManager(this);

// Default values are 5s of scanning and 25s of waiting time to save CPU cycles.
// In order for this demo to be more responsive and immediate we lower down those values.
beaconManager.setBackgroundScanPeriod(TimeUnit.SECONDS.toMillis(3), 0);

beaconManager.setMonitoringListener(new MonitoringListener() {
  @Override
  public void onEnteredRegion(Region region, List<Beacon> beacons) {
    postNotification("Entered region");
      Log.d(TAG, "entered");

  }

  @Override
  public void onExitedRegion(Region region) {
    postNotification("Exited region");
      Log.d(TAG, "exited");

  }
});
         }

              @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() {
             super.onDestroy();
            /* notificationManager.cancel(NOTIFICATION_ID);*/
          Log.d(TAG, "Beacons monitoring service destroyed");
           Toast.makeText(this, "Beacons monitoring service done",                         Toast.LENGTH_SHORT).show();
        Notification noti = new  Notification.Builder(NotifyDemoActivity.this)
             .setContentTitle("Stopped")
             .setContentText("See you!")
             .setSmallIcon(R.drawable.variance_new_logo)
             .build();

          /*
             beaconManager.disconnect();
             super.onDestroy();
              */}

     private void postNotification(String msg) {
       Intent notifyIntent = new Intent(NotifyDemoActivity.this,DistanceBeaconActivity.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("Beacon Found")
                 .setContentText(msg)
                 .setAutoCancel(true)
                     .setContentIntent(pendingIntent)
                 .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);
                 }
                   }

您正在初始化 Activity 中的 BeaconManager,它在 UI 线程上运行,最终将在后台销毁。因此,您需要移动接收器中的 BeaconManager 扫描部件,这应该可以工作。从代码的外观来看,您似乎正在使用 Estimote,但下面的示例应该可以工作。

这是来自 Estimote 论坛的两个链接:

https://community.estimote.com/hc/communities/public/questions/200535593-Background-notification-possible-on-Android-

https://community.estimote.com/hc/communities/public/questions/202762783-Android-notification-when-app-in-background-without-open-the-app

这是在 Github 上使用后台 BeaconManager 的简单应用程序:

https://github.com/zakaprov/network-switcher

检查注册 BeaconServiceBeaconServiceReceiverAndroidManifest.xml 文件,然后检查 BeaconServiceReceiver 源以获得更详细的实现。

您可以按照我们的 Android 教程学习如何设置后台监控:

http://developer.estimote.com/android/tutorial/part-2-background-monitoring/

长话短说,诀窍是在 Application 子类中创建 BeaconManager。这样,它就不会绑定到任何特定的 activity.

截至目前,Estimote SDK 尚不支持完全杀死时的监控。我们正在努力在即将推出的 SDK 更新之一中解决这个问题。

正如@dhaval 指出的那样,您仍然可以通过将 BeaconManager 绑定到启用了 START_STICKY 选项的服务中来手动使其以这种方式工作——这样,Android 永远不会终止该服务即使应用程序本身被杀死,监控也会继续。不过,这是一个更高级的主题。