【问题标题】:Android FCM failed to post notification on channel "my_channel_01"Android FCM 未能在频道“my_channel_01”上发布通知
【发布时间】:2018-03-02 16:03:03
【问题描述】:

我正在从 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());

    }
}

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

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

Failed to post notification on channel "my_channel_01"

我做错了什么?

【问题讨论】:

  • 可能是因为您使用的图片不包含 Play 商店。
  • 但是我在 logcat 中收到了消息。另外,我的图片是带有 Android 8.0(Google API)的 nexus 5s

标签: android firebase firebase-cloud-messaging


【解决方案1】:

当您的构建指定 targetSdkVersion 为 26,并且您在 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);
}

而且因为它只需要 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);
}

重新创建NotificationChannel 并没有什么坏处,这为您在哪里执行它提供了一些灵活性。如果您的应用程序有多个入口点(活动、广播接收器等),请注意确保为所有情况创建通道。您还可以使用NotificationManager.getNotificationChannel() 确保它只创建一次:

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

【讨论】:

  • 我的最小 sdk 级别是 15,目标是 26。当我尝试初始化 NotificationChannel 构造函数时,它给了我一个 lint 错误说:调用需要 api 级别 26(当前最小值为 15)
  • 如我在答案中所示,用Build.VERSION.SDK_INT >= Build.VERSION_CODES.O 将通道创建代码括在if 语句中。或者使用@RequiresApi 注解。
  • 我已将代码更改为以下内容:gist.github.com/neeraj87/97cf10d979005c4dfd13f16a8e26147a 但我仍然不确定如何处理小于 26 的 API 通知,因为 NotificationCompat.Builder 的新构造函数将通道作为其第二个参数
  • 在 API 级别低于 26 的设备上,构造函数中的通道 ID 只是一个占位符。我不认为它被使用。这些 API 级别不希望存在具有该 ID 的通道。我认为您发布的代码应该可以工作,在您取消注释通知生成之后。
  • 鲍勃,你是上帝派来的人。这就像一个魅力。我真的很感谢你的帮助。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-17
  • 2021-05-23
  • 2021-09-16
  • 1970-01-01
相关资源
最近更新 更多