android - 关闭 Heads Up 通知并创建一个普通通知

标签 android notifications heads-up-notifications

我正在使用此代码创建一个 Heads Up 通知。

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的例子,模拟我想要的行为。

enter image description here enter image description here

最佳答案

问题是因为你使用了setFullScreenIntent :

An intent to launch instead of posting the notification to the status bar. Only for use with extremely high-priority notifications demanding the user's immediate attention, such as an incoming phone call or alarm clock that the user has explicitly set to a particular time. If this facility is used for something else, please give the user an option to turn it off and use a normal notification, as this can be extremely disruptive.

也如本 answer 中所述你应该使用 setVibrate使 Heads-up 发挥作用。

这是一个有效的单挑通知示例:

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

关于android - 关闭 Heads Up 通知并创建一个普通通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36163060/

相关文章:

android - 如何为使用 Room Auto-Migrations 创建的新列设置现有行的默认值?

ios 火灾前本地通知检查

android - 已跳过 setExact 方法调用。警报未按预期触发

java - 从 SQLServer 到 Java 的通知

android - 如何检测 uiautomator 中的抬头通知?

android - MyFirstApp Android 编码教程中的错误“Intent 构造函数未定义。

java - Base64 解码无法与 Android 中的 Cipher.Decrypt 一起正常工作

android - 抬头通知有时不起作用

java - 如何将数组列表从适配器传递到 fragment ?