关闭 Heads Up 通知并创建一个普通通知 [英] Dismiss Heads Up notification and create a normal one

查看:161
本文介绍了关闭 Heads Up 通知并创建一个普通通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码创建抬头通知.

I'm using this code to create a Heads Up notification.

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

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

The thing is, notification should appear occupying a good part of top screen to call user attention, but after a couple seconds it should dismiss and a normal notification should appear.

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

But this code doesn't do that. The notification stay occupying all the top screen until user dismiss it.

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

I'm thinking in create another normal notification with same ID after a couple of seconds using Handler, but I want to know if there is a better way to do this.

以 WhatsApp 为例,模拟我想要的行为.

Follow an example of WhatsApp, simulating the behavior I want.

推荐答案

这个问题是因为你使用了 setFullScreenIntent:

The issue is caused because you use 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.

同样如本答案中所述,您应该使用setVibrate 进行单挑工作.

Also as explained in this answer you should use setVibrate to make Heads-up work.

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

This is an example of working Heads-up notification:

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

这篇关于关闭 Heads Up 通知并创建一个普通通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆