用户通过滑动删除它后在华为设备上重新启动前台通知(喜欢Strava/Endomondo)
Restart notification on foreground on Huawei device after user removes it by swiping(Like Strava/Endomondo)
我有一个显示通知栏的前台任务,目的是让这个通知栏不可移动。通知的配置为:
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 并传递接收者的未决意图 class。
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();
在receiver的onReceive方法上,我又启动了我的前台服务
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, RTMForegroundService.class);
context.startService(service);
}
现在,每次用户滑动删除通知时,它都会重新开始。
@Sabid Habib 的回答是正确的,但与其使用系统级广播,不如使用如下定义的自定义本地广播以及一些优化
定义您的自定义广播接收器并放置一个静态布尔值 NotificationService.sEnabled
来检查您所需的服务是否已经 运行 以及如果您不想不必要地启动服务则进行额外检查,另外您可以将布尔值发送到服务 notificationServiceIntent.putExtra(NotificationService.EXTRA_RUN_IN_FOREGROUND, true);
以指示服务应该只重新启动前台通知而不是从 onStartCommand
完全重新启动它
public class NotificationRemovalReceiver extends BroadcastReceiver {
public static final String FILTER_NOTIFICATION_REMOVED = "FILTER_NOTIFICATION_REMOVED";
@Override
public void onReceive(Context context, Intent intent) {
if(NotificationService.sEnabled) {
Intent notificationServiceIntent = new Intent(context, NotificationService.class);
notificationServiceIntent.putExtra(NotificationService.EXTRA_RUN_IN_FOREGROUND, true);
context.startService(notificationServiceIntent);
}
}
}
为删除通知添加此广播接收器
Intent removeNotificationIntent = new Intent(NotificationRemovalReceiver.FILTER_NOTIFICATION_REMOVED);
PendingIntent removeNotificationPendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, removeNotificationIntent, 0);
Notification mNotification = new NotificationCompat.Builder(this, getNotificationChannelId())
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(smallIcon)
.setDeleteIntent(removeNotificationPendingIntent)
.setContentIntent(pendingIntent).build();
startForeground(NOTIFICATION_ID, mNotification);
在清单文件中定义您的接收器
<receiver android:name=".receivers.NotificationRemovalReceiver" >
<intent-filter>
<action android:name="FILTER_NOTIFICATION_REMOVED" >
</action>
</intent-filter>
</receiver>
我有一个显示通知栏的前台任务,目的是让这个通知栏不可移动。通知的配置为:
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 并传递接收者的未决意图 class。
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();
在receiver的onReceive方法上,我又启动了我的前台服务
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, RTMForegroundService.class);
context.startService(service);
}
现在,每次用户滑动删除通知时,它都会重新开始。
@Sabid Habib 的回答是正确的,但与其使用系统级广播,不如使用如下定义的自定义本地广播以及一些优化
定义您的自定义广播接收器并放置一个静态布尔值 NotificationService.sEnabled
来检查您所需的服务是否已经 运行 以及如果您不想不必要地启动服务则进行额外检查,另外您可以将布尔值发送到服务 notificationServiceIntent.putExtra(NotificationService.EXTRA_RUN_IN_FOREGROUND, true);
以指示服务应该只重新启动前台通知而不是从 onStartCommand
public class NotificationRemovalReceiver extends BroadcastReceiver {
public static final String FILTER_NOTIFICATION_REMOVED = "FILTER_NOTIFICATION_REMOVED";
@Override
public void onReceive(Context context, Intent intent) {
if(NotificationService.sEnabled) {
Intent notificationServiceIntent = new Intent(context, NotificationService.class);
notificationServiceIntent.putExtra(NotificationService.EXTRA_RUN_IN_FOREGROUND, true);
context.startService(notificationServiceIntent);
}
}
}
为删除通知添加此广播接收器
Intent removeNotificationIntent = new Intent(NotificationRemovalReceiver.FILTER_NOTIFICATION_REMOVED);
PendingIntent removeNotificationPendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, removeNotificationIntent, 0);
Notification mNotification = new NotificationCompat.Builder(this, getNotificationChannelId())
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(smallIcon)
.setDeleteIntent(removeNotificationPendingIntent)
.setContentIntent(pendingIntent).build();
startForeground(NOTIFICATION_ID, mNotification);
在清单文件中定义您的接收器
<receiver android:name=".receivers.NotificationRemovalReceiver" >
<intent-filter>
<action android:name="FILTER_NOTIFICATION_REMOVED" >
</action>
</intent-filter>
</receiver>