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

时间:2017-09-21 10:31:32

标签: 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());

    }
}

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

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

Failed to post notification on channel "my_channel_01"

我做错了什么?

1 个答案:

答案 0 :(得分:8)

当您的构建指定vwx, ijk, lmn为26,并且您在API级别26设备或模拟器上运行时,您必须在构建NotificationCompat.Builder时指定通道ID,并且还要创建通道。 / p>

你可以使用这样的方法:

categories = cat.getAllCatagories()

for value in categories:
    categoriesData = {}
    subCategories = cat.getSubCategoriesByCategoryId(value.id)
    for val in subCategories:
        categoriesData[values.name] = val.name

context = {'categoriesData': categoriesData}

return render(request, 'demo/test/industries_catagories.html', context)

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

targetSdkVersion

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

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