繁体   English   中英

推送通知未显示在Android的通知栏中

[英]Push Notification is not displaying in Notification Bar Android

我刚刚迁移到androidx来实现Firebase消息传递。 我已将所有内容更改为androidx,并将我的应用连接到Firebase。 起初,当我从Firebase控制台发送通知时,它崩溃了。 为了避免崩溃,我将FirebaseApp.initializeApp(this); 在我的MainActivity中。

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

Manifest我把

<service android:name=".MyFirebaseMessagingService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

对于MyFirebaseMessagingService

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    public static final String TAG = "FirebaseSerive";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        String title = remoteMessage.getData().get("title");
        String body = remoteMessage.getData().get("body");

        Notification notification = new Notification.Builder(this)
            .setContentTitle(title)
            .setContentText(body)
            .setSmallIcon(R.drawable.ss_icon)
            .build();
        NotificationManagerCompat manager = NotificationManagerCompat.from(getApplicationContext());
        manager.notify(/*notification id*/0, notification);
    }
}

更新代码后,我的模拟器显示“ Failed to post notification on channel “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());
    }

例如,在此处阅读更多内容

文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM