Android FCM无法在频道"my_channel_01"上发布通知 [英] Android FCM failed to post notification on channel "my_channel_01"

查看:188
本文介绍了Android FCM无法在频道"my_channel_01"上发布通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am sending push notification message from Firebase console to my app running on emulator.

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());

    }
}

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

The constructor for NotificationCompat.Builder for API 26 now takes two params one is Context and another is String channelId. So I just assigned a random string to my channel.

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

But when I send the message from the firebase console the app on emulator gives me an error TOAST saying:

Failed to post notification on channel "my_channel_01"

我在做什么错了?

推荐答案

当内部版本指定targetSdkVersion为26,并且您在API级别26的设备或仿真器上运行时,在构造 NotificationCompat.Builder ,并创建频道.

When your build specifies a targetSdkVersion of 26, and you run on an API level 26 device or emulator, you must both specify a channel ID when constructing NotificationCompat.Builder, and also create the channel.

您可以使用如下方法:

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);
}

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

And because it is only needed for API 26 and requires that level, invoke it like this:

// 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);
}

重新创建 NotificationChannel 并没有什么害处.做事的灵活性.如果您的应用有多个入口点(活动,广播接收器等),请确保确保在所有情况下都创建了频道.您还可以确保使用仅创建一次NotificationManager.getNotificationChannel():

It does no harm to recreate the NotificationChannel, which provides some flexibility in where you do it. If your app has multiple entry points (activity, broadcast receiver, etc) take care to ensure the channel is created for all cases. You can also ensure it is only created once using NotificationManager.getNotificationChannel():

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

这篇关于Android FCM无法在频道"my_channel_01"上发布通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆