抬头通知有时不起作用

时间:2015-12-30 21:04:49

标签: android notifications heads-up-notifications

我以这种方式提出通知:

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

我第一次在测试设备上安装应用程序时,通知会抬头,但如果我展开通知区域(当抬头仍在运行时)并从那里解除通知,则下次通知不会抬头。重新安装后,应用程序通知再次抬头。 抬头行为不能保持不变是否有任何理由?

2 个答案:

答案 0 :(得分:5)

抬头通知具有内置的速率限制 - 如果用户向上滑动抬头通知(将其放回到通知托盘中)或侧向(解除通知托盘),则会向系统发出信号以防止进一步抬头通知一段时间(默认为〜分钟)。

答案 1 :(得分:0)

我希望以下代码能帮助您最大程度地了解哪些属性对显示抬头通知最重要,并且通过使用此代码,我没有发现有时无法正常工作的问题。

@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上关注我的帖子,以获取更多详细信息。