首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >提示通知有时不起作用

提示通知有时不起作用
EN

Stack Overflow用户
提问于 2015-12-30 21:04:49
回答 2查看 3.6K关注 0票数 3

我用这样的方式发出通知:

代码语言:javascript
复制
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

当我第一次在测试设备上安装应用程序时,通知是提示的,但是如果我扩展通知区域(当提示仍在运行)并从那里取消通知时,下次通知将不会提示。重新安装应用程序后,通知将再次提示。是否有任何理由,为什么提醒行为不能是不变的?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-30 21:49:51

提示通知有内置的速率限制--如果用户将提示向上滑动(将其放回通知托盘中)或侧(将其删除),则这会向系统发出信号,以防止在一段时间内进一步提醒通知(默认情况下为一分钟)。

票数 10
EN

Stack Overflow用户

发布于 2019-02-26 16:52:24

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

代码语言:javascript
复制
@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);
       }
   }
}

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

你也可以关注我在此链接媒体上的文章,以获得更详细的答案。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34537224

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档