没有显示 Android 通知

我需要一个程序,将添加一个 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);
131086 次浏览

我觉得你忘了

addAction(int icon, CharSequence title, PendingIntent intent)

看这里: AddAction 添加动作

事实上,答案来自 iernando Valle看起来并不正确。然后,你的问题过于模糊,因为你没有提到什么是错误的或不工作。

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

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

addAction只能在4.1之后使用。在此之前,您将使用 PendingIntent来启动 Activity。您似乎指定了 PendingIntent,所以您的问题在别处。从逻辑上讲,人们必须得出结论,这是缺失的图标。

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

.setSmallIcon(R.drawable.icon)

安卓奥利奥(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());

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

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

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

  2. SetContentTitle ()设定的标题

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

  4. 在 Android 8.0(API 级别26)和更高的 上,有效的通知通道 ID,由 SetChannelId ()设置或在 NotificationCompat 中提供。创建通道时的生成器构造函数。

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

今天我被这个问题绊倒了,但是我意识到这是因为在 Android 9.0(Pie)上,请勿打扰默认也隐藏了所有的通知,而不是像在 安卓8.1(Oreo)和之前那样只是让它们沉默。这不适用于通知。

我喜欢在我的开发设备上打开 DND,所以进入 DND 设置,改变设置,只是静音通知(但不隐藏他们)为我修复它。

在使通知可见的 安卓8.1(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);
}
}

我的 Android 应用程序也有同样的问题。我尝试了一下通知,发现我的 Android 模拟器上显示的是通知,它运行的是 Android 7.0(牛轧糖)系统,而我的手机上运行的是 安卓8.1(奥利奥)系统。

在阅读了文档之后,我发现 Android 有一个叫做通知通道的功能,如果没有这个功能,奥利奥设备上就不会出现通知。下面是通知渠道中官方 Android 文档的链接。

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


}
});
}
});

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

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

这对我来说简直是神来之笔。

如果你在使用通知频道时使用的是 version > = 安卓8.1(Oreo) ,请将其重要性设置为更高:

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

如果你先后迅速显示通知或取消现有通知,然后立即再次显示通知(例如触发提示通知,通知用户正在进行的通知有变更) ,通知可能不会显示。在这些情况下,系统 决定只是阻止通知时,它觉得他们可能会成为过于铺天盖地/垃圾邮件的用户。

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

如果您有这个选项,那么在更新通知时不要取消它,而是使用更新后的通知调用 NotificationManager.notify()。这似乎不会触发上述系统阻塞。

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)

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

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

确保 NotificationId 是唯一的。我不知道为什么我的测试推送没有显示出来,但这是因为通知 ID 是根据推送内容生成的,而且因为我一遍又一遍地推送相同的通知,所以通知 ID 保持不变。