【问题标题】:Heads-up Ongoing Notification not hide单挑持续通知不隐藏
【发布时间】:2016-05-18 11:38:33
【问题描述】:

我需要无法从通知抽屉中取消的提示通知,因此我创建了提示持续通知:

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);

问题是通知显示在屏幕顶部后,它不会隐藏到通知抽屉。但如果通知不在进行中,它就会隐藏。我需要将隐藏到通知抽屉的提示通知。

【问题讨论】:

  • 不要使用“setOngoing”,你的通知应该会在一段时间后自动隐藏。

标签: android heads-up-notifications


【解决方案1】:

AFAIK,通知需要设置以下属性才能显示为提示通知。

.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_ALL)

setOngoing() 表示通知正在进行中,用户无法隐藏。我相信这可能就是您的通知保持在顶部并且不隐藏的原因。删除setOngoing(true),它应该工作。

【讨论】:

  • 但是我需要通知,它不能从通知列表(通知抽屉)中取消,但几秒钟后会从屏幕顶部隐藏。
  • 为此,您可以使用这些标志mNotification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
  • 我试过了,但通知仍然停留在屏幕顶部并热隐藏到通知列表
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多