通知管理器只创建一个通知通道,但我需要创建三个通道



在我的应用程序中,我已经创建了3个通知通道(Follow),等,)接收来自Firebase Cloud Messaging的通知。

当我运行应用程序并进入设置>应用程序在我的App>应用程序通知,我发现它只显示一个通知通道(例如关注通道),其他两个通道没有显示…

但是当我收到来自评论通道的通知时,例如,它用评论通道更新当前通道,当我收到来自Like通道的通知时,它用Like通道更新当前通道,等等。

那么为什么它不同时显示三个分开的通道呢?!

这里是Code I work with:

public class MyFireBaseMessagingService extends FirebaseMessagingService {
String title, content, type;
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);

title = remoteMessage.getData().get("Notif_Title");
content= remoteMessage.getData().get("Notif_Content");
type = remoteMessage.getData().get("Type");

if (type.equals("Follow")) {
NotificationManager manager1 = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

Intent result = new Intent(this, MainActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, result, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(title)
.setContentText(postContent)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentIntent(resultPendingIntent)

//to work on android 8 and high..
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId("YOUR_PACKAGE_NAME");
builder.setSmallIcon(R.drawable.logo);
NotificationChannel channel = new NotificationChannel(
"YOUR_PACKAGE_NAME",
"Follow",
NotificationManager.IMPORTANCE_HIGH);
manager1.createNotificationChannel(channel);

}
manager1.notify(0, builder.build());
} else if (type.equals("Comment")) {
NotificationManager manager2 = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent result = new Intent(this, MainActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 1, result, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(title)
.setContentText(content)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentIntent(resultPendingIntent)

//to work on android 8 and high..
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId("YOUR_PACKAGE_NAME");
builder.setSmallIcon(R.drawable.logo);
NotificationChannel channel = new NotificationChannel(
"YOUR_PACKAGE_NAME",
"Comment",
NotificationManager.IMPORTANCE_HIGH);
manager3.createNotificationChannel(channel);
}
manager3.notify(1, builder.build());

} else if (type.equals("Like")) {
Intent result = new Intent(this, MainActivity.class);

PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 2, result, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager manager3 = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(title)) 
.setContentText(content)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentIntent(resultPendingIntent)

//to work on android 8 and high..
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId("YOUR_PACKAGE_NAME");
builder.setSmallIcon(R.drawable.logo);
NotificationChannel channel = new NotificationChannel(
"YOUR_PACKAGE_NAME",
"Like",
NotificationManager.IMPORTANCE_HIGH);
manager3.createNotificationChannel(channel);
}
manager3.notify(2, builder.build());
}
}
}

有人能帮我吗?

我知道怎么解决…你应该修改这个代码

builder.setChannelId("YOUR_PACKAGE_NAME");
builder.setSmallIcon(R.drawable.logo);

进入特定的channel id到每个,如:

builder.setChannelId("com.myapp.first_notification");
builder.setSmallIcon(R.drawable.logo);

相关内容

  • 没有找到相关文章

最新更新