【发布时间】:2018-01-11 18:27:09
【问题描述】:
我正在尝试集成 Firebase 通知,但在尝试处理收到的通知时出现“无法在频道上发布通知”错误。
此问题仅在应用程序位于前台时发生。
如果在后台,通知显示没有问题。
我正在尝试发布到频道 ID“12345”。我不确定这是否是问题所在,因为我没有创建频道,但如果这是问题所在,如果应用程序在后台,它是如何工作的?
我错过了什么?
我没有使用 Android 8,实际上我找到了一个在这种情况下应该用于创建频道的 sn-p:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel notificationChannel = new NotificationChannel(Constants.NOTIFICATION_CHANNEL_ID, Constants.NOTIFICATION_CHANNEL_NAME, importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
}
Intent intent = new Intent(this, ActivityMain.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// 0 is request code
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, Constants.NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("FCM Message")
.setContentText(messageBody)
.setAutoCancel(true)
//.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// 0 is id of notification
notificationManager.notify(0, notificationBuilder.build());
【问题讨论】:
-
发布生成通知的代码。
-
我知道您目前没有使用您发布的 sn-p 创建频道。如果您确实使用过它,则需要进行更正。最后,添加对NotificationManager.createNotificationChannel(...)的调用。
-
@BobSnyder 我刚刚添加了其余的代码。 if 部分未在版本低于 8 的情况下执行,这就是在我的模拟器中执行的内容。我错过了什么?
-
您表明您在 API 级别
标签: android firebase firebase-notifications