【发布时间】:2018-05-07 07:48:13
【问题描述】:
我们的应用现在有 targetSdkVersion 26 (Android 8) 并且应用使用 FCM 推送通知。当应用程序处于前台状态时,推送通知运行良好。当应用程序未处于前台状态时,单击通知时会出现此问题。我刚刚在 manifest 中添加了 元数据,但仍然出现同样的错误。
AndroidManifest.xml
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel"
android:value="@string/default_notification_channel_id"/>
MyFirebaseMessagingService.java
/**
* Create and show a simple notification containing the received FCM message.
*/
private void sendNotification(NotificationModel notificationModel)
{
Intent intent;
if (notificationModel.getAppLink() != null)
{
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(notificationModel.getAppLink()));
} else
{
intent = new Intent(this, NoticeActivity.class);
}
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
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);
if (notificationModel.getAppLink() != null)
{
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
} else
{
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
}
notificationBuilder.setContentTitle(notificationModel.getTitle())
.setContentText(notificationModel.getMessage())
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
{
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "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});
assert notificationManager != null;
notificationBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
notificationManager.createNotificationChannel(notificationChannel);
}
assert notificationManager != null;
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
【问题讨论】:
-
Related question 有更好的答案。重要的是,请参阅接受的答案下的 cmets,解释如何在您的
strings.xml中定义@string/default_notification_channel_id- 否则,您指的是不存在的值。
标签: push-notification firebase-cloud-messaging android-notifications android-studio-3.0 android-8.0-oreo