首页 文章

在后台监视期间接收来自信标区域检测的通知

提问于
浏览
0

我正在尝试创建一个基本的应用程序,在其中我创建一个regionBootstrap用于各种类型的信标的背景监控,就像在参考应用程序中一样 .

但是,我不想在进入信标区域时将应用程序带到前台,而是想简单地显示“您已输入信标区域”本地通知 .

我认为这需要在'extends Application implements BootStrapNotifier'类中的onCreate方法中编码 . 但是我也看到启动主活动的意图是在didEnterRegion方法中实例化的,所以这实际上是我需要编写通知的地方吗?

1 回答

  • 0

    触发后台通知的最简单方法是创建一个实现 BootstrapNotifier 的自定义 Application 类 . 然后将通知代码放在 didEnterRegion 回调方法中,如下所示:

    @Override
    public void didEnterRegion(Region arg0) {
        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(this)
                        .setContentTitle("Beacon Reference Application")
                        .setContentText("A beacon is nearby.")
                        .setSmallIcon(R.drawable.ic_launcher);
    
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addNextIntent(new Intent(this, MonitoringActivity.class));
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        builder.setContentIntent(resultPendingIntent);
        NotificationManager notificationManager =
                (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1, builder.build());
    
    }
    

    您可以在reference application中查看Android Beacon Library的完整示例 .

相关问题