Android FCM未能在频道“上发布通知”;我的“U频道”01;

Android FCM未能在频道“上发布通知”;我的“U频道”01;,android,firebase,firebase-cloud-messaging,Android,Firebase,Firebase Cloud Messaging,我正在从Firebase控制台向在emulator上运行的应用程序发送推送通知消息 MyFirebaseMessagingService类如下所示: public class MyFirebaseMessagingService extends FirebaseMessagingService { private static final String TAG = MyFirebaseMessagingService.class.getSimpleName(); @Overri

我正在从Firebase控制台向在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());

    }
}
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);
}
// 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);
}
API 26的NotificationCompat.Builder的构造函数现在接受两个参数,一个是Context,另一个是String channelId。所以我给我的频道分配了一个随机字符串

但是,当我从firebase控制台发送消息时,emulator上的应用程序会给我一个错误提示:

Failed to post notification on channel "my_channel_01"

我做错了什么?

当您的构建指定了26的
targetSdkVersion
,并且您在API级别26的设备或模拟器上运行时,您必须在构建时指定通道ID,同时创建通道

您可以使用如下方法:

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

    }
}
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);
}
// 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);
}
因为它只在API 26中需要,并且需要该级别,所以像这样调用它:

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

    }
}
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);
}
// 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);
}
重新创建并没有什么坏处,这为您的操作提供了一定的灵活性。如果您的应用程序有多个入口点(活动、广播接收器等),请注意确保为所有情况创建频道。您还可以使用以下方法确保仅创建一次:


也许是因为你正在使用的图像不包括Play Store,但我在logcat中得到了信息。另外,我的图片是Nexus5s和安卓8.0(谷歌API),我的最小sdk级别是15,目标是26。当我尝试初始化NotificationChannel构造函数时,它给了我一个lint错误,它说:调用需要api级别26(当前最小值为15)在if语句中用
Build.VERSION.SDK\u INT>=Build.VERSION\u CODES.O
括起通道创建代码,如我在回答中所示。或者使用@RequiresApi注释。我已将代码更改为以下内容:但我仍然不确定如何处理api小于26的通知,因为NotificationCompat的新构造函数。Builder将通道作为其第二个参数,api级别小于26的设备,构造函数中的通道ID只是一个占位符。我认为它没有被使用。这些API级别不希望存在具有该ID的通道。我认为在取消通知生成的注释后,你发布的代码应该可以工作。鲍勃,你是上帝派来的人。这很有魅力。我真的很感谢你的帮助。谢谢