通知未显示

时间:2013-04-16 19:43:35

标签: android notifications

我需要一个在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 its selected

        notificationManager.notify(0, n);

13 个答案:

答案 0 :(得分:357)

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

.setSmallIcon(R.drawable.icon)

Android Oreo(8.0)及以上

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

private 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.SdkInt >= Android.OS.BuildVersionCodes.O)
   {
    String channelId = "Your_channel_id";
    NotificationChannel channel = new NotificationChannel(channelId,
                            "Channel human readable title", Android.App.NotificationImportance.Default);
   mNotificationManager.CreateNotificationChannel(channel);
   notificationBuilder.SetChannelId(channelId);              
}

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

答案 1 :(得分:56)

实际上@ tato469的答案似乎不正确。再说一次,你的问题过于含糊,因为你没有提到错误或不起作用。

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

您的通知未显示,因为您没有提供图标。虽然SDK文档没有提到它是必需的,但实际上非常如此,如果没有一个,Notification将无法显示。

addAction仅在4.1之后可用。在此之前,您可以使用PendingIntent发布Activity。您似乎指定了PendingIntent,因此您的问题出在其他地方。从逻辑上讲,必须得出结论,这是缺少的图标。

答案 2 :(得分:25)

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

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

  1. setSmallIcon()

  2. 设置的小图标
  3. 标题,由setContentTitle()

  4. 设置
  5. 详细信息文本,由setContentText()

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

  8. 请参阅http://developer.android.com/guide/topics/ui/notifiers/notifications.html

答案 3 :(得分:6)

今天这让我震惊,但我意识到这是因为在Android P上,默认情况下“请勿打扰”还会隐藏所有通知,而不仅仅是像O和以前那样使它们静音。这不适用于通知。

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

答案 4 :(得分:5)

对于Android版本,必须创建通知渠道>要使通知可见,必须使用Android Oreo。如果在您的应用程序中看不到Oreo +机器人的通知,则需要在应用程序启动时调用以下函数-

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);
   }
}

答案 5 :(得分:1)

我认为你忘记了

addAction(int icon, CharSequence title, PendingIntent intent)

看这里Add Action

答案 6 :(得分:0)

我的Android应用程序存在相同的问题。我正在尝试通知,发现通知在运行牛轧糖(Android 7)系统的android仿真器上显示,而没有在装有oreo(Android 8)的手机上运行。阅读文档后,我发现android具有称为通知频道的功能,没有通知功能,通知将不会显示在oreo设备上。以下是通知渠道上官方android文档的链接。

https://developer.android.com/guide/topics/ui/notifiers/notifications.html#Templates https://developer.android.com/training/notify-user/channels.html

干杯!

答案 7 :(得分:0)

对我来说,这是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) {



                    }
                });

    }
});

答案 8 :(得分:0)

不确定该选项是否仍处于活动状态,但是您还需要更改build.gradle文件,并将使用的Android SDK版本添加到其中,

实现'com.android.support:appcompat-v7:28.0.0'

在我的情况下,这就像一种魅力

答案 9 :(得分:0)

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

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

答案 10 :(得分:0)

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

请注意,至少从外部看,在正版的Android(经过10个测试)上,此行为看起来有些随机:它只是有时发生而有时则没有。我的猜测是,有一个非常短的时间阈值,在此阈值内不允许您发送太多通知。依次调用NotificationManager.cancel()NotificationManager.notify()有时可能会导致此行为。

如果您有此选择,则在更新通知时,请不要先取消它,而只需使用更新后的通知调用NotificationManager.notify()。这似乎不会触发系统的上述阻止。

答案 11 :(得分:0)

我遇到了与您类似的问题,在寻找解决方案时,我找到了这些答案,但它们并不像我希望的那样直接,但它提供了一个想法;您的通知可能不会显示,因为对于 >=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());
}

答案 12 :(得分:-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)
相关问题