【问题标题】:Failed to post notification on channel “null”无法在频道“null”上发布通知
【发布时间】:2018-11-07 14:24:48
【问题描述】:

我有一个可以向用户发送通知的应用程序,但是由于新的 PlayStore 更新需要匹配 targetSdkVersion 26,因此通知不再起作用,错误 toast 无法在频道“null”上发布通知。有一些关于它的线程,但我没有得到解决方案。这是我实际的 FirebaseMessagingService 类:

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {


    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        String notification_title = remoteMessage.getNotification().getTitle();
        String notification_message = remoteMessage.getNotification().getBody();
        String click_action = remoteMessage.getNotification().getClickAction();
        String from_user_id = remoteMessage.getData().get("from_user_id");

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle(notification_title)
                        .setContentText(notification_message);


        Intent resultIntent = new Intent(click_action);
        resultIntent.putExtra("user_id", from_user_id);

        PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        this,
                        0,
                        resultIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );

        mBuilder.setContentIntent(resultPendingIntent);
        int mNotificationId = (int) System.currentTimeMillis();
        NotificationManager mNotifyMgr =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        mNotifyMgr.notify(mNotificationId, mBuilder.build());
    }
}

我需要进行哪些更改才能使其再次运行?如果它也适用于 sdk 24 以上的设备,那就太好了。

【问题讨论】:

    标签: java android firebase firebase-cloud-messaging


    【解决方案1】:

    从 Android Oreo 开始,您需要一个频道来发送通知。 你可以做这样的事情(未经测试):

    public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
    
      public static final String NOTIFICATION_CHANNEL_ID = "10001";
    
        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            super.onMessageReceived(remoteMessage);
    
            String notification_title = remoteMessage.getNotification().getTitle();
            String notification_message = remoteMessage.getNotification().getBody();
            String click_action = remoteMessage.getNotification().getClickAction();
            String from_user_id = remoteMessage.getData().get("from_user_id");
    
            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(this)
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle(notification_title)
                            .setContentText(notification_message);
    
    
            Intent resultIntent = new Intent(click_action);
            resultIntent.putExtra("user_id", from_user_id);
    
            PendingIntent resultPendingIntent =
                    PendingIntent.getActivity(
                            this,
                            0,
                            resultIntent,
                            PendingIntent.FLAG_UPDATE_CURRENT
                    );
    
            mBuilder.setContentIntent(resultPendingIntent);
            int mNotificationId = (int) System.currentTimeMillis();
            NotificationManager mNotifyMgr =
                    (NotificationManager) getSystemService(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);
    
                mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
                mNotifyMgr.createNotificationChannel(notificationChannel);
            }
            mNotifyMgr.notify(mNotificationId, mBuilder.build());
        }
    }
    

    【讨论】:

    • mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);原因无法解析方法'setChannelId(java.lang.String)'
    • 您使用的是最后一个 AppCompat 吗?或者,至少是 26.0.2 ?
    • 在应用打开时工作。如果应用程序已关闭或在后台,则错误消息无法在频道“null”上发布通知再次出现。
    • nmv 我已将 firebase 消息更新到 11.8.0,现在可以使用了。谢谢
    • 好的,很酷。如果我的回答适合你,请不要犹豫,投赞成票:它可以在未来帮助其他人;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    • 2022-07-23
    • 2016-09-21
    • 2021-11-08
    • 2022-12-10
    • 1970-01-01
    相关资源
    最近更新 更多