无法在“空”频道上发布通知

时间:2018-11-07 14:24:48

标签: java android firebase firebase-cloud-messaging

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

int[][] tab = {zero.clone(), zero.clone()};

我需要进行哪些更改才能使其再次运行?如果它也适用于sdk 24起的设备,那将是很好的。

1 个答案:

答案 0 :(得分: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());
    }
}