【发布时间】:2021-01-13 22:57:21
【问题描述】:
不幸的是,关于我的 startForegroundService 通知的另一个问题...我搜索了,真的,我做到了:
我有一个运行良好的前台服务。我想在此通知中添加一些操作。一方面,当用户点击他们发送到 MainActivity 的通知以及添加“退出”addAction 时,这样做。
这是我用来创建通知的 sn-p:
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
String channelId = getNotificationChannel(notificationManager);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
Notification notification = notificationBuilder.setOngoing(true)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setSmallIcon(R.drawable.ic_notif_icon)
.setContentTitle("My app")
.setContentText("Background service is running...")
.setContentIntent(pendingIntent)
.build();
startForeground(13365, notification);
使用上面的通知可以正常显示,但单击它不会导致任何结果。我也尝试使用 addAction,也没有。我知道添加 addAction 时的语法有点不同(....Action.Builder)。
我正在前台服务的 onCreate 处理程序中创建通知。在 SDK 26 上运行。
startForeground 通知可以附加 setContentIntent / addAction 吗?
谢谢!
【问题讨论】:
标签: android android-intent service notifications foreground