我有一个显示通知栏的前台任务,目的是使该通知栏不可删除。通知的配置为:
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("Notification")
.setContentText("Notification for app")
.setSmallIcon(R.mipmap.ic_launcher)
.setContent(notificationView)
.setOngoing(true)
.build();
虽然应用了setongoing(true),但在华为设备(Android 5.1.1)上可以删除通知。尽管它适用于三星 J3。
当滑动通知时,华为会出现以下屏幕
我在华为设备上的 strava/endomondo 应用程序中看到,用户滑动删除通知后,它们会立即返回通知。
用户删除通知后如何将其带回到前台?
最佳答案
我终于能够像 strava/endomondo 一样在华为设备上重新启动通知了。
为了触发通知删除事件,我必须使用 setDeleteIntent 并传递接收器类的挂起 Intent 。
Intent intent = new Intent(this, BootCompleteReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, 0);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("Foreground Task")
.setTicker("Notification")
.setContent(notificationView)
.setDeleteIntent(pendingIntent) //the delete intent I used
.setOngoing(true).build();
在接收方onReceive方法上,我再次启动了前台服务
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, RTMForegroundService.class);
context.startService(service);
}
现在,每次用户滑动删除通知时,它都会重新开始。
关于android - 用户通过滑动删除华为设备后在前台重新启动通知(如 Strava/Endomondo),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49001145/