【问题标题】:Notification in android beacon appandroid信标应用程序中的通知
【发布时间】:2015-09-13 19:12:09
【问题描述】:

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

         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);
                 }
                   }

【问题讨论】:

    标签: android monitoring android-notifications estimote android-ibeacon


    【解决方案1】:

    您正在初始化Activity 中的BeaconManager,该Activity 在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 源以获得更详细的实现。

    【讨论】:

    • 嘿,谢谢@dhaval 的回复。我想问一下“所以你需要在接收器中移动 BeaconManager 扫描部分”是什么意思。表示是否是其他活动或课程?我知道这可能是一个愚蠢的问题,但请指导我!
    • 基本上Activity 是一个屏幕,因此只要屏幕对用户可见,您的代码就可以正常工作。但是由于 Beacon 通知可能随时出现,您需要将监听部分移至 BackgroundReceiver,这就像 Android 运行的后台任务,无论用户是否使用该应用程序。我在答案中提到的 Github 演示正是这样做的,如果你只是浏览代码你就会明白其中的逻辑。如果您是 Android 新手,我建议您对 Activity、BackgroundReceiver 和 Service 有基本的了解,这将对您有很大帮助。
    • yhh ok 意味着我必须根据链接项目进行信标管理器的新活动,并且我必须扩展广播接收器。!!剩下的就是我要做的代码了。对吧??@dhaval
    【解决方案2】:

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

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

    长话短说,诀窍是在 Application 子类中创建 BeaconManager。这样,它就不会与任何特定活动相关联。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-14
      • 1970-01-01
      • 2013-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-20
      相关资源
      最近更新 更多