If you're still using the LegacyNotifications library, it's time for you to migrate over! The new library brings plenty of new features, bug fixes, and is much more reliable and intuitive.
If you're using Expo's push notification service, you don't need to change anything on the server-side for this migration.
Run npx expo install expo-notifications.
If you're using the bare workflow, you'll need to follow these additional instructions.
Replace any occurrences of:
import { Notifications } from 'expo'
in your JS code with:
import * as Notifications from 'expo-notifications';
If you're upgrading to SDK 40 or below, you need to set android.useNextNotificationsApi: true in your app.json file. See here for details.
Some values from your app.json notification key still apply, but some do not:
iconstill applies, do not remove it ✅colorstill applies, do not remove it ✅iosDisplayInForegrounddoes not apply, remove it 🚨- With
expo-notifications, this is controlled viaNotifications.setNotificationHandler()
- With
androidModedoes not apply, remove it 🚨androidCollapsedTitledoes not apply, remove it 🚨- Both
androidModeandandroidCollapsedTitlewill be replaced by setting a key in your notification payload, this feature is in progress.
- Both
Listed below are each of the methods exposed via the LegacyNotifications module, and what you should replace them with.
addListener allows you to provide a callback to be run whenever a notification is received or selected in your app. expo-notifications actually exposes two different methods to replace this: addNotificationReceivedListener() and addNotificationResponseReceivedListener(). The NotificationReceivedListener is triggered when a notification is received while your app is foregrounded. The NotificationResponseReceivedListener is triggered when a user interacts with a notification that was delivered to their device (either by tapping on it, or through the actions exposed via notification categories).
Prefer to use hooks? expo-notifications has you covered- use the useLastNotificationResponseHook() instead of addNotificationResponseReceivedListener.
Both getExpoPushTokenAsync and getDevicePushTokenAsync have the same methods in the new expo-notifications API:
The only difference is that the new methods return an object of the format { type: string, data: string }, where data is the actual push token string. No other changes are necessary unless you eject to bare, in which case you should check the method parameters in the docs.
LegacyNotifications.presentLocalNotificationAsync() & LegacyNotifications.scheduleLocalNotificationAsync()
Both of these have been replaced by Notifications.scheduleNotificationAsync(). To get the same behavior as LegacyNotifications.presentLocalNotificationAsync (AKA- trigger a notification instantly), just pass null in as the NotificationTriggerInput.
There is an identical method in the new expo-notifications module- Notifications.dismissNotificationAsync()- no changes necessary.
There is an identical method in the new expo-notifications module- Notifications.dismissAllNotificationsAsync()- no changes necessary.
There is an identical method in the new expo-notifications module- Notifications.cancelScheduledNotificationAsync()- no changes necessary.
There is an identical method in the new expo-notifications module- Notifications.cancelAllScheduledNotificationsAsync()- no changes necessary.
Replace with Notifications.getBadgeCountAsync.
Replace with Notifications.setBadgeCountAsync.
We’ve added much more comprehensive support for categories in the new expo-notifications module, with plenty more customization options, so the methods and the parameters they accept are a little different. You should read here for a complete guide.
Replace
LegacyNotifications.createCategoryAsyncwithNotifications.setNotificationCategoryAsync, andLegacyNotifications.deleteCategoryAsyncwithNotifications.deleteNotificationCategoryAsync.
We’ve added much more comprehensive support for Android’s notification channels in the new expo-notifications module, with plenty more customization options, so the methods and the parameters they accept are a little different. You should read here for a complete guide.
Replace
LegacyNotifications.createChannelAndroidAsyncwithNotifications.setNotificationChannelAsync, and replaceLegacyNotifications.deleteChannelAndroidAsyncwithNotifications.deleteNotificationChannelAndroidAsync.