16

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

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

Niraj
  • 5,270
  • 2
  • 13
  • 19
sam_k
  • 5,983
  • 14
  • 76
  • 110

4 Answers4

18

What I do, is extending the application class (don't forget to update the app's manifest with the class name) and create the notifications channels once in the onCreate method. This guarantees the notification channels are always created when building a notification.

IMHO it is a waste of CPU cycles to (try to) create the notification channels for each notification over and over again.

As a side note: I always log the app version this way too, which is quite useful when somebody sends a logcat.

M66B
  • 1,164
  • 1
  • 8
  • 16
  • 3
    I am doing that way right now. Creating them in the `onCreate()` method of MyApplication class. Is that the correct way ? – sam_k Sep 21 '17 at 23:57
  • In my case it takes approx 14 msec to create 3 channels btw. – ror Jun 02 '19 at 20:09
  • I agree, and thanks I didn't know that we could also extend the application class. This is going to be pretty useful. – jasxir Aug 30 '19 at 10:14
  • 2
    isn't it a waste of CPU cycles to create a notification channel on every app start? – YTerle Dec 18 '19 at 13:07
  • 1
    @YTerle See this quote from [the docs](https://developer.android.com/training/notify-user/channels) - `Creating an existing notification channel with its original values performs no operation, so it's safe to call this code when starting an app.` – Vadim Kotov May 07 '20 at 08:35
  • if i did the setup in my application onCreate for channels after checking if it exists or not, would that affect my lunchtime? – Moustafa EL-Saghier Jan 23 '22 at 12:11
11
  1. Ideally, you should create channel while posting notification to it. It is safe to call createNotificationChannel with same id used previously, it will not be recreated.
  2. Your application won't post this notification. System might show warning toast that your app is not allowed to post this notification.
Arnav M.
  • 2,590
  • 1
  • 27
  • 44
  • I didn't get your first answer here. Suppose, I have multiple channels (A,B,C,D), I am creating media player notification under channel B, so is it a good idea that uses `notificationManager. createNotificationChannel(B)` every time I populate media notification? What's the advantage of doing that instead of one time when Application Launches? – sam_k Sep 20 '17 at 19:06
  • 1
    the system simply ignores it if the channel already exist. you can either check before creating the channel NotificationChannel notificationChannel = notificationManager.getNotificationChannel(channel); if (notificationChannel != null); . If you create all channels in advance, the user can disable all the channels in advance too. – Arnav M. Sep 21 '17 at 04:27
  • 1
    I guess you didn't get my actual question here. I am more interested to know the correct pattern to create the notification channels. To create channel every time from 100 places in the app is not the good idea. – sam_k Sep 21 '17 at 23:56
  • you should create one general method to post notification, not to re-write it 100 places. – Arnav M. Sep 22 '17 at 04:57
  • 6
    But that general method calls 100 times, and call createChannel 100 times isn't a good idea. – sam_k Sep 22 '17 at 21:37
7

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

Ricard
  • 1,223
  • 1
  • 13
  • 17
  • 1
    The [Official Documentation](https://developer.android.com/training/notify-user/channels#CreateChannel) where it is mentioned. Thank you. – Kathir Mar 10 '20 at 17:33
2

I think there are two sensible ways:
1. Create all channels in Application subclass,
2. Create all channels in your main Activity, which first starts with the app.

Matt
  • 35
  • 8