【问题标题】:Dismiss Heads Up notification and create a normal one关闭抬头通知并创建一个普通通知
【发布时间】:2016-07-09 20:26:54
【问题描述】:

我正在使用此代码创建一个提醒通知。

private static void showNotificationNew(final Context context,final String title,final String message,final Intent intent, final int notificationId, final boolean isHeaderNotification) {
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context.getApplicationContext())
            .setSmallIcon(R.drawable.prime_builder_icon)
            .setPriority(Notification.PRIORITY_DEFAULT)
            .setCategory(Notification.CATEGORY_MESSAGE)
            .setContentTitle(title)
            .setContentText(message)
            .setWhen(0)
            .setTicker(context.getString(R.string.app_name));

    PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    notificationBuilder.setContentText(message);
    if(isHeaderNotification) {
        notificationBuilder.setFullScreenIntent(fullScreenPendingIntent, false);
    }

    notificationBuilder.setContentIntent(fullScreenPendingIntent);
    notificationBuilder.setAutoCancel(true);


    Notification notification = notificationBuilder.build();
    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(notificationId, notification);
}

问题是,通知应该占据顶部屏幕的很大一部分以引起用户注意,但几秒钟后它应该消失并且应该出现正常通知。

但是这段代码并没有这样做。通知会一直占据整个顶部屏幕,直到用户将其关闭。

我正在考虑在几秒钟后使用 Handler 创建另一个具有相同 ID 的普通通知,但我想知道是否有更好的方法来做到这一点。

按照 WhatsApp 的示例,模拟我想要的行为。

【问题讨论】:

    标签: android notifications heads-up-notifications


    【解决方案1】:

    问题是因为你使用setFullScreenIntent:

    意图启动而不是将通知发布到状态 酒吧。仅用于要求极高优先级的通知 用户的即时关注,例如来电或 用户已明确设置为特定时间的闹钟。如果 此设施用于其他用途,请给用户一个 关闭它并使用正常通知的选项,因为这可以是 极具破坏性。

    同样如answer 中所述,您应该使用setVibrate 进行单挑。

    这是一个工作提示通知的示例:

    private static void showNotificationNew(final Context context, final String title, final String message, final Intent intent, final int notificationId) {
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context.getApplicationContext())
                .setSmallIcon(R.drawable.small_icon)
                .setPriority(Notification.PRIORITY_HIGH)
                .setContentTitle(title)
                .setContentText(message)
                .setVibrate(new long[0])
                .setContentIntent(pendingIntent)
                .setAutoCancel(true);
    
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(notificationId, notificationBuilder.build());
    }
    

    【讨论】:

    • 有趣的是,设置全屏 Intent 可以防止抬头通知自动关闭。当我想到我的电话响起时,这是有道理的,但我花了一段时间才弄清楚发生了什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-07
    • 2016-02-04
    • 2018-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-22
    相关资源
    最近更新 更多