gpt4 book ai didi

android - 如果应用程序处于 Activity 状态,则无法在 channel 上发布通知

转载 作者:行者123 更新时间:2023-11-29 15:00:04 25 4
gpt4 key购买 nike

我正在尝试集成 firebase 通知,但在尝试处理收到的通知时收到“无法在 channel 上发布通知”错误。

此问题仅在应用程序位于前台时发生。

如果它在后台,则可以毫无问题地显示通知。

我正在尝试发布到 channel ID“12345”。我不确定这是否可能是问题所在,因为我没有创建 channel ,但如果这是问题所在,如果应用程序在后台,它是如何工作的?

我错过了什么?

我没有使用 Android 8,实际上我找到了在这种情况下应该用来创建 channel 的 fragment :

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel notificationChannel = new NotificationChannel(Constants.NOTIFICATION_CHANNEL_ID, Constants.NOTIFICATION_CHANNEL_NAME, importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
}


Intent intent = new Intent(this, ActivityMain.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// 0 is request code
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);



Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, Constants.NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("FCM Message")
.setContentText(messageBody)
.setAutoCancel(true)
//.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);

NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// 0 is id of notification
notificationManager.notify(0, notificationBuilder.build());

最佳答案

我上面的逻辑有一个我错过的错误。问题出在 >= 26 的版本中,因为我创建了一个 channel ,然后没有将通知发送到正确的通知管理器,而是创建了一个新的通知管理器实例(查看问题代码的最后两行)。

这里是更正后的逻辑:

private void sendNotification(String messageBody) {

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// create channel in new versions of android
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel notificationChannel = new NotificationChannel(Constants.NOTIFICATION_CHANNEL_ID, Constants.NOTIFICATION_CHANNEL_NAME, importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
notificationManager.createNotificationChannel(notificationChannel);
}


// show notification
Intent intent = new Intent(this, ActivityMain.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// 0 is request code
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, Constants.NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.icon_contact_purple)
.setContentTitle(getString(R.string.app_name))
.setContentText(messageBody)
.setAutoCancel(true)
//.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);

// 0 is id of notification
notificationManager.notify(0, notificationBuilder.build());

}

关于android - 如果应用程序处于 Activity 状态,则无法在 channel 上发布通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48213657/

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