gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:54:35 25 4
gpt4 key购买 nike

我正在从 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。所以我只是为我的 channel 分配了一个随机字符串。

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

Failed to post notification on channel "my_channel_01"

我做错了什么?

最佳答案

当您的构建将 targetSdkVersion 指定为 26,并且您在 API 级别 26 设备或模拟器上运行时,您必须在构建 NotificationCompat.Builder 时同时指定 channel ID ,并创建 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 需要它并且需要那个级别,所以调用它是这样的:

// 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 没有坏处,这在您执行操作的位置上提供了一些灵 active 。如果您的应用程序有多个入口点( Activity 、广播接收器等),请注意确保为所有情况创建 channel 。您还可以使用 NotificationManager.getNotificationChannel() 确保它只创建一次:

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

关于Android FCM 无法在 channel "my_channel_01"上发布通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46341603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com