Java 推送通知未显示在通知栏中

Java 推送通知未显示在通知栏中,java,android,firebase,firebase-cloud-messaging,Java,Android,Firebase,Firebase Cloud Messaging,我刚刚迁移到androidx以实现firebase消息传递。 我已将所有内容更改为androidx,并将我的应用程序连接到firebase。 起初,当我从firebase控制台发送通知时,它崩溃了。 为了避免崩溃,我将FirebaseApp.initializeApp(这个)在我的主要活动中 但我仍然没有在通知列表中看到任何通知 在我的清单中我将 <service android:name=".MyFirebaseMessagingService" android:exported

我刚刚迁移到androidx以实现firebase消息传递。 我已将所有内容更改为androidx,并将我的应用程序连接到firebase。 起初,当我从firebase控制台发送通知时,它崩溃了。 为了避免崩溃,我将
FirebaseApp.initializeApp(这个)在我的主要活动中

但我仍然没有在通知列表中看到任何通知

在我的
清单中
我将

<service android:name=".MyFirebaseMessagingService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>
更新我的代码后,我的模拟器显示
无法在通道“null”上发布通知


提前感谢您的帮助。希望您能帮助我。

您可以尝试使用OREO中介绍的频道。这可能是导致问题的原因

    public class MyFirebaseMessagingService extends FirebaseMessagingService {

    public static final String TAG = "FirebaseSerive";
    public static final String NOTIFICATION_CHANNEL_ID = "IMP_NOTIFICATIONS";


    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        String title = remoteMessage.getNotification().getTitle();
        String body = remoteMessage.getNotification().getBody();

        Notification.Builder notification = new Notification.Builder(this)
                .setContentTitle(title)
                .setContentText(body)
                .setSmallIcon(R.drawable.ss_icon);

        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_HIGH;
            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 mNotificationManager != null;
            notification.setChannelId(NOTIFICATION_CHANNEL_ID);
            mNotificationManager.createNotificationChannel(notificationChannel);
        }

        mNotificationManager.notify(/*notification id*/0, notification.build());
    }
}
编辑: 可能是您得到的是通知负载,而不是数据,您可以使用下面的代码进行检查

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }


收到消息时,应用程序是在前台还是在后台?你发送文本或数据消息吗?嗨@Robert应用程序在前台。文本消息和数据消息的区别是什么?我只使用Firebase控制台发送通知。您测试的Android版本是什么?Hi@lazy当前我正在Android 27上测试它我的mind sdk版本是24,目标sdk版本是28@natsumiyu要在android 8.0之后发送通知,您必须首先创建一个频道。我是否应该将其放入
onMessageReceived
?此代码具有1)不寻常的格式,2)与问题无关的不必要代码,3)包含可能导致应用程序崩溃的危险
断言4),未知的
通知\u频道ID
,5)使用mHungarian符号,最后6)将打破干燥原则,形成代码@Zun,并添加拼凑在一起的相关代码。谢谢您的退房。我已收到通知,但标题和正文为空。即使我在firebase控制台中放置了标题和正文。@natsumiyu您也必须创建NotificationCompat.Builder来设置文本和标题。
    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }