我正在使用此代码创建一个 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的例子,模拟我想要的行为。
最佳答案
问题是因为你使用了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/