【问题标题】:Android notification is not showingAndroid 通知未显示
【发布时间】:2013-04-09 08:53:10
【问题描述】:

我需要一个可以在 Android 上添加通知的程序。当有人点击通知时,它会引导他们进入我的第二个活动。

我已经建立了代码。该通知应该有效,但由于某种原因它不起作用。 Notification 根本没有显示。我不知道我错过了什么。

这些文件的代码:

Notification n = new Notification.Builder(this)
        .setContentTitle("New mail from " + "test@gmail.com")
        .setContentText("Subject")
        .setContentIntent(pIntent).setAutoCancel(true)
        .setStyle(new Notification.BigTextStyle().bigText(longText))
        .build();

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Hide the notification after it's selected

notificationManager.notify(0, n);

【问题讨论】:

  • 请在提问时尽量简洁。您没有提及 what 不起作用。事实上,你甚至没有提到某些东西不起作用。 Activity 没有启动吗? Notification 没有显示吗?
  • @dmmh 我在我的问题中说我的问题是什么!我没有发现这个问题有什么问题。我的问题是关于这个问题的。如果您不是专家,则无需戳!并且 tato469 的回答很好。
  • 我要指出,这个网站的目的不是让您解决问题,而是让其他人也能够找到问题的解决方案。我不是在戳。对不起,我伤害了你的感情,但事实是你的问题是错误的,答案是错误的,所有这些误导性的信息会导致其他新手程序员,他们有与你描述的相同的问题,诉诸于尝试不起作用的解决方案根据您提供的代码。无论我是否是专家,这都无关紧要。尤其不是因为我是对的,你的问题是模糊的,而另一个答案是错误的。
  • 我将要求删除此问题和答案,因为它具有误导性。祝你有美好的一天。
  • 更改了问题,以便人们了解问题所在。

标签: android notifications


【解决方案1】:

我遇到了与您类似的问题,在寻找解决方案时我找到了这些答案,但它们并不像我希望的那样直接,但它给出了一个想法;您的通知可能不会显示,因为对于 >=8 的版本,通知的处理方式相对不同,有一个 NotificationChannel 可以帮助管理通知 this helped me。编码愉快。

void Note(){
    //Creating a notification channel
    NotificationChannel channel=new NotificationChannel("channel1",
                                                        "hello",
                                                        NotificationManager.IMPORTANCE_HIGH);
    NotificationManager manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.createNotificationChannel(channel); 
    
    //Creating the notification object
    NotificationCompat.Builder notification=new NotificationCompat.Builder(this,"channel1");
    //notification.setAutoCancel(true);
    notification.setContentTitle("Hi this is a notification");
    notification.setContentText("Hello you");
    notification.setSmallIcon(R.drawable.ic_launcher_foreground);   
    
    //make the notification manager to issue a notification on the notification's channel
    manager.notify(121,notification.build());
}

【讨论】:

  • 请查看您粘贴的链接。
【解决方案2】:
val pendingIntent = PendingIntent.getActivity(applicationContext, 0, Intent(), 0)

var notification = NotificationCompat.Builder(applicationContext, CHANNEL_ID)
                                    .setContentTitle("Title")
                                    .setContentText("Text")
                                    .setSmallIcon(R.drawable.icon)
                                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                                    .setContentIntent(pendingIntent)
                                    .build()
val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

mNotificationManager.notify(sameId, notification)

【讨论】:

  • 上面的代码对我有用,以便通知现有的通知。
  • 您能否解释一下您的方法,以及为什么您更喜欢它而不是公认的答案?
【解决方案3】:

如果没有图标,代码将无法运行。因此,像这样将setSmallIcon 调用添加到构建器链中以使其工作:

.setSmallIcon(R.drawable.icon)

Android Oreo (8.0) 及以上

Android 8 引入了使用NotificationChannel 设置channelId 属性的新要求。

NotificationManager mNotificationManager;

NotificationCompat.Builder mBuilder =
    new NotificationCompat.Builder(mContext.getApplicationContext(), "notify_001");
Intent ii = new Intent(mContext.getApplicationContext(), RootActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, ii, 0);

NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
bigText.bigText(verseurl);
bigText.setBigContentTitle("Today's Bible Verse");
bigText.setSummaryText("Text in detail");

mBuilder.setContentIntent(pendingIntent);
mBuilder.setSmallIcon(R.mipmap.ic_launcher_round);
mBuilder.setContentTitle("Your Title");
mBuilder.setContentText("Your text");
mBuilder.setPriority(Notification.PRIORITY_MAX);
mBuilder.setStyle(bigText);

mNotificationManager =
    (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

// === Removed some obsoletes
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
    String channelId = "Your_channel_id";
    NotificationChannel channel = new NotificationChannel(
                                        channelId,
                                        "Channel human readable title",
                                        NotificationManager.IMPORTANCE_HIGH);
   mNotificationManager.createNotificationChannel(channel);
  mBuilder.setChannelId(channelId);
}

mNotificationManager.notify(0, mBuilder.build());

【讨论】:

  • 如何获取CHANNEL_ID?请帮忙
  • @LAnantaPrasad CHANNEL_ID 是您的标识符。这是您将通知分组的依据,例如聊天对话或群聊。
  • 谢谢,setChannelId 是必需的。在我添加之前,通知会显示在我的 SONY 设备上,而不是 SAMSUNG 设备上。
  • 还包括检查if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ mBuilder.setChannelId(channelId); },因为如果没有此检查,即使您的设备 API>=26,通知也不会显示。
【解决方案4】:

如果您在使用通知渠道时使用 >= Android 8.1 (Oreo) 版本,请将其重要性设置为高:

int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);

【讨论】:

  • 你说的"一个版本>="是什么意思?您的意思是“8.1(或更高版本)”?请通过editing your answer 回复,而不是在 cmets 中(视情况而定)。
  • @PeterMortensen 请原谅我,这只是一种类型,感谢您纠正我
【解决方案5】:

如果您一个接一个地快速显示通知或取消现有通知,然后立即再次显示,则可能不会显示通知(例如,触发提醒通知以通知用户正在进行的更改通知)。在这些情况下,系统可能决定仅在感觉通知对用户来说过于庞大/垃圾邮件时才阻止通知。

请注意,从外部来看,至少在现有的 Android(使用 10 测试)上,这种行为看起来有点随机:它有时会发生,有时不会。我的猜测是,有一个非常短的时间阈值,在此期间您不允许发送太多通知。调用NotificationManager.cancel() 然后NotificationManager.notify() 有时可能会导致这种行为。

如果您可以选择,在更新通知时不要取消它,而只需致电NotificationManager.notify() 并提供更新的通知。这似乎不会触发上述系统阻止。

【讨论】:

    【解决方案6】:

    您还需要更改 build.gradle 文件,并将使用的 Android SDK 版本添加到其中:

    implementation 'com.android.support:appcompat-v7:28.0.0'
    

    这对我来说就像一个魅力。

    【讨论】:

      【解决方案7】:

      对我来说这是deviceToken 的问题。请检查接收方和发送方设备令牌是否在您的数据库中或您访问它以发送通知的任何地方正确更新。

      例如,使用以下内容在应用启动时更新设备令牌。因此,它将始终正确更新。

      // Device token for push notifications
      FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(
        new OnSuccessListener<InstanceIdResult>() {
      
          @Override
          public void onSuccess(InstanceIdResult instanceIdResult) {
      
              deviceToken = instanceIdResult.getToken();
      
              // Insert device token into Firebase database
              fbDbRefRoot.child("user_detail_profile").child(currentUserId).child("device_token")).setValue(deviceToken)
                      .addOnSuccessListener(
                        new OnSuccessListener<Void>() {
      
                          @Override
                          public void onSuccess(Void aVoid) {
      
                          }
                      });
          }
      });
      

      【讨论】:

      • 这个deviceToken是在哪里声明的?
      • 为什么onSuccess 是空的?
      • @PeterMortensen.. 成功后,你可以做任何你的逻辑允许的事情,比如开始一个新的意图,或者敬酒,或者保持在同一页面上。至于 deviceToken 声明,您可以在页面中的 onCreate 之前将其声明为 Private String deviceToken
      • (为了让我自己的评论更清楚:有两个onSuccesss - 我指的是内部的一个(它是空的))
      【解决方案8】:

      我的 Android 应用遇到了同样的问题。我正在尝试通知,发现通知显示在我的运行 Android 7.0 (Nougat) 系统的 Android 模拟器上,而我的手机上没有运行 Android 8.1 (Oreo)。

      阅读文档后,我发现 Android 有一个称为通知通道的功能,没有它,通知将不会显示在 Oreo 设备上。以下是通知渠道上官方 Android 文档的链接。

      【讨论】:

        【解决方案9】:

        Android 8.1 (Oreo) 之后的 Android 版本必须创建通知通道以使通知可见。如果通知在您的 Oreo+ Android 应用中不可见,您需要在应用启动时调用以下函数 -

        private void createNotificationChannel() {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                CharSequence name = getString(R.string.channel_name);
                String description = getString(R.string.channel_description);
                int importance = NotificationManager.IMPORTANCE_DEFAULT;
                NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name,
               importance);
                channel.setDescription(description);
                // Register the channel with the system; you can't change the importance
                // or other notification behaviours after this
                NotificationManager notificationManager =
                getSystemService(NotificationManager.class);
                notificationManager.createNotificationChannel(channel);
           }
        }
        

        【讨论】:

          【解决方案10】:

          今天这让我大吃一惊,但我意识到这是因为在 Android 9.0 (Pie) 上,请勿打扰 默认情况下也会隐藏所有通知,而不是像在 Android 8.1 中那样将它们静音(奥利奥)和之前。这不适用于通知。

          我喜欢为我的开发设备启用 DND,因此进入 DND 设置并更改设置以简单地使通知静音(但不隐藏它们)为我修复了它。

          【讨论】:

          • 一个多小时没有结果,你拯救了我的一天。谢谢!
          • 哇。半小时后,拉我的头发,你救了我的培根。谢谢!
          【解决方案11】:

          实际上the answer by ƒernando Valle 似乎并不正确。再说一次,您的问题过于含糊,因为您没有提及错误或无效。

          查看您的代码,我假设 Notification 根本没有显示。

          您的通知未显示,因为您没有提供图标。尽管 SDK 文档没有提到它是必需的,但实际上非常需要,而且您的 Notification 没有它就不会显示。

          addAction 仅从 4.1 开始可用。在此之前,您将使用PendingIntent 启动Activity。您似乎指定了PendingIntent,所以您的问题出在其他地方。从逻辑上讲,我们必须断定它是丢失的图标。

          【讨论】:

          • 我发现了这个问题。但感谢您的正确回复。非常感谢您的帮助。
          • 你说 addAction 是从 4.​​1 开始才有的,但是错了,我是在 2.3 用的。参考手册谈论的是按钮而不是通知:“操作按钮不会出现在 Android 4.1 之前的平台上。”你可以在这里查看:AddAction 包含在 android.support.v4.app 中
          • OP 使用的是Notification.Builder,而不是NotificationCompat.Builder,就像你建议的那样。仅仅因为有一个兼容库并且可以使用,并不意味着该函数在普通 API 中可用。您提供的链接实际上规定了这一点。如示例所示,此问题显然与Notification.Builder 有关。并且兼容性库可能是在 4.1 之后才包含这个功能。您的回答具有误导性,因为现在您建议它从 2.3 开始就可用。
          • 对不起,如果我说错了,我的意思是这在 2.3 中是可能的
          • 图标啊!!甚至没有意识到它丢失了,但添加它可以解决问题。在控制台中没有看到关于丢失图标的任何有用信息,似乎静默失败。
          【解决方案12】:

          我认为你忘记了

          addAction(int icon, CharSequence title, PendingIntent intent)
          

          看这里:Add Action

          【讨论】:

          • addAction() 方法在 Notification.Builder 上不可用。
          • 它确实存在,但已弃用。相反,您可以传递一个Action 实例,您可以使用Notification.Action.Builder 创建该实例。 P.S.,始终使用 Compat 替代品(即 NotificationCompat),因为 Google 倾向于修补错误并提供更好的兼容性
          • 如果您不想指定任何操作,只需在构建器中使用.setContentIntent(null)
          【解决方案13】:

          您错过了小图标。 我犯了同样的错误,上面的步骤解决了。

          根据官方文档: Notification 对象必须包含以下内容:

          1. 一个小图标,由setSmallIcon()设置

          2. 标题,由setContentTitle()设置

          3. 详细文本,由setContentText()设置

          4. 在 Android 8.0(API 级别 26)及更高版本上,有效的通知通道 ID,由 setChannelId() 设置或在创建通道时在 NotificationCompat.Builder 构造函数中提供。 p>

          http://developer.android.com/guide/topics/ui/notifiers/notifications.html

          【讨论】:

          • 这不是真的。如果您通过 setContent 使用自定义通知布局,则不需要标题或文本。
          • 它确实对我有用,没有内容标题或文本,但没有小图标。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-08-17
          • 1970-01-01
          • 1970-01-01
          • 2018-12-23
          • 2021-12-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多