android - 用户通过滑动删除华为设备后在前台重新启动通知(如 Strava/Endomondo)

标签 android notifications huawei-mobile-services foreground-service

我有一个显示通知栏的前台任务,目的是使该通知栏不可删除。通知的配置为:

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。

当滑动通知时,华为会出现以下屏幕

enter image description here

我在华为设备上的 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/

相关文章:

android - 如何获取listview中选中项的index和textview?

android - 避免取消从最近的应用程序列表中删除应用程序的通知

iOS 当应用程序处于后台时处理通知和有效负载

publish - 谷歌禁止华为。我应该如何处理 google 依赖项?

java - AV Pipeline Kit Demo 不工作,有什么问题?

java - Android 运行访问

java - FTP - Android 客户端和 Java 服务器

java - Android setText 导致崩溃

ios - 我的应用程序推送通知仅在我客户的 iPhone 中不起作用

android - View.SYSTEM_UI_FLAG_FULLSCREEN 不适用于 HUAWEI P20