Xamarin.Forms (AltBeacon Android Library) - 在后台检测到信标时发送本地通知



我感谢一些关于如何处理这个问题的反馈:

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

  • 当在后台检测到信标时,应用应静默发送本地通知,而不是打开应用。我的本地通知应该在我单击它们时具有打开应用程序的意图。

从这里开始,他们有正确的方案,但随后样本似乎没有做应该做的事情。 https://altbeacon.github.io/android-beacon-library/notifications.html

任何帮助都非常感谢。 谢谢 布鲁诺

我写了那个示例,它是用 Java for 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());

最新更新