FCM 通知未在后台显示弹出通知



我使用firebase云功能在我的应用程序中显示通知,并且工作完美,我面临的唯一问题是当应用程序关闭或在后台通知不显示弹出。提醒在app中使用,我确实会收到通知但通知不会像app在前台时那样弹出。我已经将频道和通知的优先级设置为高,但仍然不起作用。

My Service Class:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d("msg", "onMessageReceived: " + remoteMessage.getData().get("message"));
Intent intent = new Intent(this, WebViewActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
String channelId = "Default";
NotificationCompat.Builder builder = new  NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.app_logo)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody())
.setAutoCancel(true).setContentIntent(pendingIntent)
.setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE)
.setPriority(Notification.PRIORITY_MAX);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_HIGH);
channel.setShowBadge(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
manager.createNotificationChannel(channel);
}
manager.notify(0, builder.build());
}

清单

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.noderon.riscalert.WebViewActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.noderon.riscalert.MainActivity">
</activity>
<service
android:name=".NotificationService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
</application>

任何帮助都会很感激。Thanku

因为你已经设置了优先级,似乎你没有为通知添加元数据,当应用程序在后台时,它不会得到任何数据,这就是为什么它只显示在前台弹出在代码中添加这些元数据:

<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_stat_ic_notification" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id" />

另外,您可以查看Google Cloud Messaging文档了解更多详细信息

快乐编码

我也遇到过同样的问题。我遵循了firebase的官方文档,现在通知工作正常。点击此链接.

是否在Activity中创建了FirebaseMessage Instance() ?

最新更新