How Set Firebase Notification Channelid In Android O?
For API level 26 we have to set a channel id as a reference. I learned how to do it without a channelID and below there is my firebase messaging set-up code. But now for the new An
Solution 1:
What I did in an app before was to initialize the Notification Channels as soon as the app starts. So I added an init
function in my Application class, like so:
@TargetApi(Build.VERSION_CODES.O)privatevoidinitNotificationChannels() {
NotificationChannelpublicChannel=newNotificationChannel(
Constants.NOTIFICATION_CHANNEL_PUBLIC,
Constants.NOTIFICATION_CHANNEL_PUBLIC,
NotificationManager.IMPORTANCE_DEFAULT);
publicChannel.setDescription(Constants.NOTIFICATION_CHANNEL_PUBLIC);
NotificationChanneltopicChannel=newNotificationChannel(
Constants.NOTIFICATION_CHANNEL_TOPIC,
Constants.NOTIFICATION_CHANNEL_TOPIC,
NotificationManager.IMPORTANCE_DEFAULT);
topicChannel.setDescription(Constants.NOTIFICATION_CHANNEL_TOPIC);
NotificationChannelprivateChannel=newNotificationChannel(
Constants.NOTIFICATION_CHANNEL_PRIVATE,
Constants.NOTIFICATION_CHANNEL_PRIVATE,
NotificationManager.IMPORTANCE_HIGH);
privateChannel.setDescription(Constants.NOTIFICATION_CHANNEL_PRIVATE);
privateChannel.canShowBadge();
List<NotificationChannel> notificationChannels = newArrayList<>();
notificationChannels.add(publicChannel);
notificationChannels.add(topicChannel);
notificationChannels.add(privateChannel);
NotificationManagermNotificationManager=
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (mNotificationManager != null) {
mNotificationManager.createNotificationChannels(notificationChannels);
}
}
Then in my FirebaseMessagingService
, I made a function that gets the channelId
when building notifications:
privateStringgetChannelId(String source) {
if (!TextUtils.isEmpty(source)) {
if (source.contains(Constants.TOPIC_PREFIX)) {
return (TextUtils.equals((TOPIC_PREFIX + Constants.NOTIFICATION_CHANNEL_PUBLIC), source)) ?
Constants.NOTIFICATION_CHANNEL_PUBLIC : Constants.NOTIFICATION_CHANNEL_TOPIC;
} else {
returnConstants.NOTIFICATION_CHANNEL_PRIVATE;
}
} else {
returnConstants.NOTIFICATION_CHANNEL_PUBLIC;
}
}
This caters to three types of notification that our app needs -- Public, Topic, and Private. You can specify the channels you need on your own.
Baca Juga
Post a Comment for "How Set Firebase Notification Channelid In Android O?"