首页 > 解决方案 > Xamarin.Forms (AltBeacon Android Library) - 在后台检测到信标时发送本地通知

问题描述

我感谢一些有关如何处理此问题的反馈:

前台应用程序工作正常,检测到信标,我可以从那里生成本地通知。当应用程序处于后台或终止时,当信标进入范围内时,我仍然可以使其恢复活力,但这“迫使”我打开应用程序。我想要以下行为:

从这里他们有正确的场景,但是样本似乎没有做应该做的事情。 https://altbeacon.github.io/android-beacon-library/notifications.html

任何帮助深表感谢。谢谢你,布鲁诺

标签: androidnotificationslocalaltbeacon

解决方案


我编写了那个示例,它是用 Java 为 Android 编写的。我可以确认它确实如图所示工作,但在针对 Android 9+ 的应用程序上,您还必须为所有通知创建一个通知通道。在 Java 中,您可以这样做:

        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(this)
                        .setContentTitle("Beacon Reference Application")
                        .setContentText("An 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);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("My Notification Channel ID",
                    "My Notification Name", NotificationManager.IMPORTANCE_DEFAULT);
            channel.setDescription("My Notification Channel Description");
            NotificationManager notificationManager = (NotificationManager) getSystemService(
                    Context.NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(channel);
            builder.setChannelId(channel.getId());
        }
        notificationManager.notify(1, builder.build());


推荐阅读