Dismiss Heads Up notification and create a normal one

喜你入骨 提交于 2019-12-05 13:43:51
Mattia Maestrini

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