单挑正在进行的通知不隐藏
Heads-up Ongoing Notification not hide
我需要无法从通知抽屉中取消的单挑通知,因此我创建了单挑持续通知:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.logo_notification)
.setContentTitle("Title")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_ALL)
.setGroup(KEY)
.setGroupSummary(true)
.setOngoing(true)
.setColor(context.getResources().getColor(R.color.tab_indicator_color));
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(context, Activity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(WActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent pIntent = new Intent(context, PService.class);
pIntent.setAction("ACTION");
PendingIntent piPause = PendingIntent.getService(context, NOTIFICATION_ID, pIntent, 0);
mBuilder.addAction(icon, title, piPause);
Intent sIntent = new Intent(context, SService.class);
sIntent.setAction("ACTION");
PendingIntent piDismiss = PendingIntent.getService(context, NOTIFICATION_ID, sIntent, 0);
mBuilder.addAction(icon2, title2, piDismiss);
问题是通知显示在屏幕顶部后,它不会隐藏到通知抽屉中。但如果通知没有进行,它就会隐藏。我需要提醒通知,它将隐藏到通知抽屉中。
据我所知,通知需要设置以下属性才能显示为提示通知。
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_ALL)
setOngoing()表示Notification正在进行,用户无法隐藏。我相信这可能就是为什么您的通知保持在顶部并且不会隐藏的原因。删除 setOngoing(true)
并且它 应该 工作。
我需要无法从通知抽屉中取消的单挑通知,因此我创建了单挑持续通知:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.logo_notification)
.setContentTitle("Title")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_ALL)
.setGroup(KEY)
.setGroupSummary(true)
.setOngoing(true)
.setColor(context.getResources().getColor(R.color.tab_indicator_color));
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(context, Activity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(WActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent pIntent = new Intent(context, PService.class);
pIntent.setAction("ACTION");
PendingIntent piPause = PendingIntent.getService(context, NOTIFICATION_ID, pIntent, 0);
mBuilder.addAction(icon, title, piPause);
Intent sIntent = new Intent(context, SService.class);
sIntent.setAction("ACTION");
PendingIntent piDismiss = PendingIntent.getService(context, NOTIFICATION_ID, sIntent, 0);
mBuilder.addAction(icon2, title2, piDismiss);
问题是通知显示在屏幕顶部后,它不会隐藏到通知抽屉中。但如果通知没有进行,它就会隐藏。我需要提醒通知,它将隐藏到通知抽屉中。
据我所知,通知需要设置以下属性才能显示为提示通知。
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_ALL)
setOngoing()表示Notification正在进行,用户无法隐藏。我相信这可能就是为什么您的通知保持在顶部并且不会隐藏的原因。删除 setOngoing(true)
并且它 应该 工作。