抬头通知有时不起作用

Heads up notification do not work sometimes

我是这样提示的:

Notification.Builder nb = new Notification.Builder(context)
        .setSmallIcon(icon)
        .setContentTitle("Title")
        .setContentText("Content")
        .setDeleteIntent(delete)
        .setPriority(Notification.PRIORITY_HIGH)
        .setVibrate(new long[0]);

notificationManager.notify(1, nb.build()); // TODO hardcode

我第一次在测试设备上安装应用程序时,通知是抬头的,但如果我扩大通知区域(当抬头仍然是 运行)并从那里关闭通知,下次通知就不会了小心。重新安装应用程序后,通知再次出现。 是否有任何原因导致单挑行为不能保持不变?

抬头通知有一个内置的速率限制 - 如果用户向上滑动您的抬头通知(将其放回通知托盘)或向侧面滑动(关闭它),那么这会向系统发出信号以防止进一步在一段时间内提醒通知(默认为一分钟)。

我希望下面的代码能帮助您最了解哪些属性对显示提醒通知最重要,而且通过使用这段代码我没有发现有时不工作的问题。

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    sendNotification(remoteMessage.getNotification().getTitle(),     
    remoteMessage.getNotification().getBody(),remoteMessage.getData());
}

private void sendNotification(String messageTitle, String messageBody, 
Map<String, String> data) {

Intent intent = HomeActivity.getHomeActivityIntent(this,data.get(Constants.PUSH_URL));

   PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
           PendingIntent.FLAG_ONE_SHOT);

   NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "1")
           .setSmallIcon(R.drawable.icon_notification)
           .setContentTitle(messageTitle)
           .setContentText(messageBody)
           .setContentIntent(pendingIntent)
           .setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE) //Important for heads-up notification
           .setPriority(Notification.PRIORITY_MAX); //Important for heads-up notification

   Notification buildNotification = mBuilder.build();
   int notifyId = (int) System.currentTimeMillis(); //For each push the older one will not be replaced for this unique id

   // Since android Oreo notification channel is needed.
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
       String name = getString(R.string.channel_name);
       String description = getString(R.string.channel_description);
       int importance = NotificationManager.IMPORTANCE_HIGH; //Important for heads-up notification
       NotificationChannel channel = new NotificationChannel(getResources().getString(R.string.default_notification_channel_id),
               name,
               importance);
       channel.setDescription(description);
       channel.setShowBadge(true);
       channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
       NotificationManager notificationManager = getSystemService(NotificationManager.class);

       if (notificationManager != null) {
           notificationManager.createNotificationChannel(channel);
           notificationManager.notify(notifyId, buildNotification);
       }
   }else{

       NotificationManager mNotifyMgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
       if (mNotifyMgr != null) {
           mNotifyMgr.notify(notifyId, buildNotification);
       }
   }
}

要显示提示信息,请复制并粘贴整个代码并修复基于字符串和导入的错误。单挑成功后,根据您的要求删除或添加任何内容。

您也可以在 this link 上关注我的 post 以获得更详细的答案。