所以我试图在Android上创建FCM抬头通知(通过后端调用)。显示通知的代码如下(我在通知时发送数据,因此它将始终调用onMessageReceived)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),
new Random().nextInt(), intent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel channelId = new NotificationChannel("channel01", "name",
NotificationManager.IMPORTANCE_HIGH);
channelId.setDescription("test push notif");
// Register channel with system
notificationManager.createNotificationChannel(channelId);
}
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, "channel01")
.setSmallIcon(R.mipmap.ic_collection)
.setContentTitle(data.get("title"))
.setContentText(data.get("message"))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setOnlyAlertOnce(true)
.setFullScreenIntent(pendingIntent, true)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_HIGH);
notificationManager.notify(99 /* ID of notification */, notificationBuilder.build());
这是我发送的数据
"to":"token-id",
"content_available": true,
"priority": "high",
"data": {
"title":"title",
"message": "message body",
"notificationKey":"1",
"priority": "high",
"sound": "default"
}
我的测试设备是Android Pie,因此我需要创建频道以使用Heads-Up。但是,抬头永远不会隐藏,它始终会显示出来,直到单击它或打开系统托盘为止。我希望平视显示像其他任何平视通知一样,在5秒钟内隐藏到系统托盘中。我已经尝试过这些
.setOngoing(true)
.setCategory(Notification.CATEGORY_CALL)
但没有任何区别,除非单击,否则Heads Up通知仍会无限显示。大约5秒钟后,如何隐藏平视提示?