android - Xamarin.Forms (AltBeacon Android Library) - 在后台检测到信标时发送本地通知
问题描述
我感谢一些有关如何处理此问题的反馈:
前台应用程序工作正常,检测到信标,我可以从那里生成本地通知。当应用程序处于后台或终止时,当信标进入范围内时,我仍然可以使其恢复活力,但这“迫使”我打开应用程序。我想要以下行为:
- 当在后台检测到信标而不打开应用程序时,应用程序应静默发送本地通知。当我点击它们时,我的本地通知应该有打开应用程序的意图。
从这里他们有正确的场景,但是样本似乎没有做应该做的事情。 https://altbeacon.github.io/android-beacon-library/notifications.html
任何帮助深表感谢。谢谢你,布鲁诺
解决方案
我编写了那个示例,它是用 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());
推荐阅读
- java - 如何在同一模块内进行测试?
- laravel - 如何将 laravel 5.4 更新到 laravel 7 以及如何将 Symphony 进程更新到 5
- ubuntu - 安装了 GMP 但看不到它
- if-statement - 机器人框架中的嵌套运行关键字语句
- c# - 在另一个类中使用我的 ViewModel 的 RelayCommand
- flutter - Flutter:在折线图中隐藏线和轴
- python - SELECT 语句是 MySql 使用 python 返回错误值
- reactjs - 如何使内容可用于高级 React Native
- c - 为什么我得到一个'public_stream!= nullptr' 执行时出错,即使我已成功使用 'fopen_s' 读取文件?
- rabbitmq - RabbitMQ LDAP 身份验证问题