[英]Working with Firebase Push Notifications without Notification channels
我需要在接收到PUSH通知時生成通知,但是當應用程序中發生某些事情時,我還需要生成通知(以便在設備的通知欄中顯示),因此我正在使用NotificationCompat.Builder
。
如您所知,android已棄用對Notification.Builder的此調用:
Notification.Builder (Context context)
現在,您必須使用此調用:
NotificationCompat.Builder (Context context, String channelId)
如果您不想指定通知通道,而是想向應用程序的所有用戶發送常規通知,並且想要在不處理通知通道的情況下接收所有已安裝應用程序中的所有通知,該怎么辦? 或者,如果您想在用戶按下應用程序中的按鈕時在通知欄中創建一個簡單的通知,該怎么辦? 如何在不指定channelId
情況下顯示通知? 我的意思是...就像在api 26之前以及通知通道出現之前一樣工作。
如果未在官方文檔的任何位置指定通知渠道,則無法查看工作方式。
通知頻道在Android 8+上是必需的。 因此,您必須使用NotificationCompat.Builder(Context context, String channelId)
並通過NotificationManager.createNotificationChannel(NotificationChannel channel)
在api 26+上創建通道。
在api <26上,只是不要調用createNotificationChannel,而要使用channel id參數(只是一個字符串)。
val builder = NotificationCompat.Builder(context, "a_channel_id")
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.ic_notif)
.setAutoCancel(true)
...
val notificationManager = NotificationManagerCompat.from(context)
notificationManager.notify(NOTIFICATION_ID, builder.build())
在Api 26+上,在創建以下頻道之前:
val channel = NotificationChannel("a_channel_id", "channel_name", NotificationManager.IMPORTANCE_HIGH)
channel.description = "channel_description"
channel.enableLights(true)
channel.lightColor = Color.RED
channel.enableVibration(true)
val notificationManager = NotificationManagerCompat.from(context)
notificationManager.createNotificationChannel(channel)
當前沒有解決方法。 通知通道最近已經宣布(如果我沒記錯的話,是最后一個I / O),並且(很可能是如果不是絕對的話)可以留在這里。 我所做的就是這樣。
為了遵守新標准,我只實現了通知通道,但僅在需要時才實現。 我還在我的應用程序上使用了FCM,這類似於我所擁有的功能-這在我的Application類中:
private void initFirebase() {
... // other Firebase stuff.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) initNotificationChannels();
}
@TargetApi(Build.VERSION_CODES.O)
private void initNotificationChannels() {
NotificationChannel publicChannel = new NotificationChannel(NOTIFICATION_CHANNEL_PUBLIC,
NOTIFICATION_CHANNEL_PUBLIC, NotificationManager.IMPORTANCE_DEFAULT);
publicChannel.setDescription(NOTIFICATION_CHANNEL_PUBLIC);
NotificationChannel privateChannel = new NotificationChannel(NOTIFICATION_CHANNEL_PRIVATE,
NOTIFICATION_CHANNEL_PRIVATE, NotificationManager.IMPORTANCE_HIGH);
publicChannel.setDescription(NOTIFICATION_CHANNEL_PRIVATE);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (mNotificationManager != null) {
mNotificationManager.createNotificationChannel(publicChannel);
mNotificationManager.createNotificationChannel(privateChannel);
}
}
我的MessagingService
具有以下內容:
private static final String NOTIFICATION_CHANNEL_PRIVATE = "my.app.package.name.private";
private static final String NOTIFICATION_CHANNEL_PUBLIC = "my.app.package.name.public";
private void buildNotification(....(other params),String source, String message) {
String channelId = getChannelId(source);
Intent resultIntent = new Intent(this, MyActivity.class);
resultIntent.putExtra(EXTRAS_PARAM_ID, myVal);
PendingIntent notificationIntent = buildNotificationIntent(channelId, roomId, roomType);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, getChannelId(source))
.setSmallIcon(R.drawable.ic_sample
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND)
.setContentIntent(notificationIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(id, 0, notificationBuilder.build());
}
private String getChannelId(String source) {
switch(source){
case PRIVATE:
return NOTIFIFICATION_CHANNEL_PRIVATE;
default:
return NOTIFICATION_CHANNEL_PUBLIC;
}
}
我不知道這是否回答了問題。 但是,在api 26以下創建任何頻道都可以正常工作,而無需在我的應用上執行任何操作。
1. instantiate notificationCompat with some channel Id
//which is irrelevant for api < 26
2. handle the case of creating notification channel for api 26+
3. bundled it up.
它只是工作。 在api 26以下,配置通知不會產生任何影響。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.