Dismiss Heads Up notification and create a normal one

旧时模样 提交于 2020-01-23 09:15:56

问题


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.

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.

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


回答1:


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.

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


来源:https://stackoverflow.com/questions/36163060/dismiss-heads-up-notification-and-create-a-normal-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!