Android FCM无法在频道"my_channel_01"上发布通知

cod*_*ess 3 android firebase firebase-cloud-messaging

我正在从Firebase控制台向我在模拟器上运行的应用发送推送通知消息.

MyFirebaseMessagingService类看起来是这样的:

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "From: " + remoteMessage.getFrom());

        if(remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        }

        if(remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        }

        Intent intent = new Intent(this, SplashActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "my_channel_01");
        notificationBuilder.setContentTitle("FCM Notification");
        notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher_round);
        notificationBuilder.setContentIntent(pendingIntent);
        notificationBuilder.setChannelId("my_channel_01");

        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());

    }
}
Run Code Online (Sandbox Code Playgroud)

用于API 26的NotificationCompat.Builder的构造函数现在需要两个参数,一个是Context,另一个是String channelId.所以我只是给我的频道分配了一个随机字符串.

但是当我从firebase控制台发送消息时,模拟器上的应用程序给我一个错误TOAST说:

Failed to post notification on channel "my_channel_01"
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Bob*_*der 9

如果构建指定了targetSdkVersion26,并且您在API级别26设备或模拟器上运行,则必须在构造NotificationCompat.Builder时指定通道ID ,并且还要创建通道.

你可以使用这样的方法:

public static final String NOTIF_CHANNEL_ID = "my_channel_01";

...

@RequiresApi(Build.VERSION_CODES.O)
private void createNotifChannel(Context context) {
    NotificationChannel channel = new NotificationChannel(NOTIF_CHANNEL_ID,
            "MyApp events", NotificationManager.IMPORTANCE_LOW);
    // Configure the notification channel
    channel.setDescription("MyApp event controls");
    channel.setShowBadge(false);
    channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);

    NotificationManager manager = context.getSystemService(NotificationManager.class);

    manager.createNotificationChannel(channel);
    Log.d(TAG, "createNotifChannel: created=" + NOTIF_CHANNEL_ID);
}
Run Code Online (Sandbox Code Playgroud)

并且因为它只需要API 26并且需要该级别,所以像这样调用它:

// The channel need only be created for API 26 devices.  For devices
// running an API less the 26, there is no way to create a channel and the
// channel ID specified in the constuctor to NotificationCompat.Builder is
// merely a placeholder.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    createNotifChannel(this);
}
Run Code Online (Sandbox Code Playgroud)

重新创建NotificationChannel没有任何害处,它可以为您提供一些灵活性.如果您的应用有多个入口点(活动,广播接收器等),请注意确保为所有情况创建频道.您还可以确保仅使用NotificationManager.getNotificationChannel()创建一次:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    if (manager.getNotificationChannel(NOTIF_CHANNEL_ID) == null) {
        createNotifChannel(this);
    }
}
Run Code Online (Sandbox Code Playgroud)