How do you use a custom channel with a local notification?

SDK Version: 39.

Platform: Android.

I’m able to create a custom channel, ‘messages’, using “setNotificationChannelAsync”:

if (Platform.OS === 'android') {
    Notifications.setNotificationChannelAsync('messages', {
      name: 'Messages',
      importance: Notifications.AndroidImportance.MAX,
      vibrationPattern: [0, 250, 250, 250],
      lightColor: '#FF231F7C',
    });
  }

But then how do I use that custom channel?

The documentation gives the following simple example for using “scheduleNotificationAsync”:

async function schedulePushNotification() {
  await Notifications.scheduleNotificationAsync({
    content: {
      title: "You've got mail! 📬",
      body: 'Here is the notification body',
      data: { data: 'goes here' },
    },
    trigger: { seconds: 2 },
  });
}

According to the documentation, the only argument that “scheduleNotificationAsync” takes is “NotificationRequestInput,” which in turn can include “NotificationContentInput”. However, I did not see any mention of channelId.

I do see that some of the documentation for the now-depreciated “presentNotificationAsync” etc. makes mention of channelId:

android ( optional ) ( object ) – notification configuration specific to Android.

  • channelId ( optional, but recommended ) ( string ) – ID of the channel to post this notification to in Android 8.0+. If null, defaults to the “Default” channel which Expo will automatically create for you. If you don’t want Expo to create a default channel, make sure to always specify this field for all notifications.

But what is the current way to use a custom channel with a local notification?

Hey @sam2019,

I’ll have to double check but I believe you will use a ChannelAwareTriggerInput as your trigger value. https://docs.expo.io/versions/v39.0.0/sdk/notifications/#channelawaretriggerinput

Cheers,
Adam

@sam2019 the docs were definitely confusing on this one. I’m still in the process of switching to this package from legacy Notifications as part of upgrading to Expo 39. But if you follow the specs you’ll see that scheduleNotificationsAsync takes a single argument of type NotificationRequestInput. That has a NotificationContentInput and a NotificationTriggerInput. The latter can take different shapes, including ChannelAwareTriggerInput (as @adamjnav mentioned), DateTriggerInput, etc. All of the NotificationTriggerInput’s include a channelId. That’s presumably where you specify the custom channel id for Android.

https://docs.expo.io/versions/v39.0.0/sdk/notifications/#notificationtriggerinput

Thanks, @adamjnav and @stevenpal!

I see how the NotificationTriggerInput types make using the channelId possible.

Expanding upon the example for scheduleNotificationAsync, the simplest use of channelId would be

  async function schedulePushNotification() {
    await Notifications.scheduleNotificationAsync({
      content: {
        title: "You've got mail! 📬",
        body: "Here is the notification body",
        data: { data: "goes here" },
      },
      trigger: {
        seconds: 2,
        channelId: "messages",
      },
    });
  }

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.