gpt4 book ai didi

java - 无法在 channel “null” 上发布通知

转载 作者:搜寻专家 更新时间:2023-11-01 09:22:50 25 4
gpt4 key购买 nike

我有一个可以向用户发送通知的应用程序,但由于新的 PlayStore 更新需要与 targetSdkVersion 26 匹配,因此通知不再有效,并出现错误 toast 无法在 channel “null”上发布通知。有一些关于它的线索,但我没有得到解决方案。这是我实际的 FirebaseMessagingService 类:

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {


@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);

String notification_title = remoteMessage.getNotification().getTitle();
String notification_message = remoteMessage.getNotification().getBody();
String click_action = remoteMessage.getNotification().getClickAction();
String from_user_id = remoteMessage.getData().get("from_user_id");

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(notification_title)
.setContentText(notification_message);


Intent resultIntent = new Intent(click_action);
resultIntent.putExtra("user_id", from_user_id);

PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);

mBuilder.setContentIntent(resultPendingIntent);
int mNotificationId = (int) System.currentTimeMillis();
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);

mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
}

我需要更改什么才能使其再次运行?如果它也适用于 sdk 24 以上的设备,那就太好了。

最佳答案

从 Android Oreo 开始,您需要一个 channel 来发送您的通知。你可以做这样的事情(未经测试):

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {

public static final String NOTIFICATION_CHANNEL_ID = "10001";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);

String notification_title = remoteMessage.getNotification().getTitle();
String notification_message = remoteMessage.getNotification().getBody();
String click_action = remoteMessage.getNotification().getClickAction();
String from_user_id = remoteMessage.getData().get("from_user_id");

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(notification_title)
.setContentText(notification_message);


Intent resultIntent = new Intent(click_action);
resultIntent.putExtra("user_id", from_user_id);

PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);

mBuilder.setContentIntent(resultPendingIntent);
int mNotificationId = (int) System.currentTimeMillis();
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
{
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);

mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotifyMgr.createNotificationChannel(notificationChannel);
}
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
}

关于java - 无法在 channel “null” 上发布通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53191386/

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