Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to create Notification Channels from Android O Api

The notification channels which introduced from Android O (API 26) version.I read about it from the following links:

  • Managing notification channels
  • Google Sample for Creating Channel

Questions:

  1. If I have multiple numbers of notification then Is it a good idea to create notification channels when the application starts and keep it at ApplicationScope?

    public void addNotificationChannels(Context context) {
    
        List<NotificationChannel> channels = new ArrayList<>();
        channels.add("channel_1");
        channels.add("channel_2");
        .
        .
        channels.add("channel_7");
    
        NotificationManager notificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannels(channels);
    
    }
    
  2. What will happen if I try to execute this line new Notification.Builder(getApplicationContext(), PRIMARY_CHANNEL) before adding channels to the notification manager

like image 591
sam_k Avatar asked Sep 19 '17 07:09

sam_k


1 Answers

A) At the same time you create the notification:

As the documentation says:

Creating an existing notification channel with its original values performs no operation, so it's safe to call this code when starting an app.

So you can safety create the notification channel at the same time you create the notification itself, there is no need to check if the channel is already created.

B) Inside Application or any Activity/Fragment.

Note: In case you are using raw FCM, is interesting to create the channel before the SDK publishes the notification for you because according the push payload param android_channel_id, you can associate that push to a specific channel already created in your app.

Fcm payloads: https://firebase.google.com/docs/cloud-messaging/http-server-ref

like image 128
Ricard Avatar answered Sep 30 '22 12:09

Ricard