`PushNotificationIOS.getInitialNotification` returns null for Local Notifications on Cold Starts.
See original GitHub issueHandling Local Push Notifications when the app is in the background or inactive state works just fine.
When your app is not running (cold start) and is launched from a Local Push Notification, the practice (according to the current docs) is to use PushNotificationIOS.getInitialNotification. However, this always returns null.
I spent a long time trying to get this to work with getInitialNotification but nothing worked. I had to essentially capture the notification in the launchOptions and then pass it in via appProperties.
My solution is captured on this StackOverflow Answer but here is the crux of it.
AppDelegate.m
// Inside of your didFinishLaunchingWithOptions method...
// Create a Mutable Dictionary to hold the appProperties to pass to React Native.
NSMutableDictionary *appProperties = [NSMutableDictionary dictionary];
if (launchOptions != nil) {
// Get Local Notification used to launch application.
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notification) {
// Instead of passing the entire Notification, we'll pass the userInfo,
// where a Record ID could be stored, for example.
NSDictionary *notificationUserInfo = [notification userInfo];
[ appProperties setObject:notificationUserInfo forKey:@"initialNotificationUserInfo" ];
}
}
// Your RCTRootView stuff...
rootView.appProperties = appProperties;
index.ios.js
componentDidMount() {
if (this.props.initialNotificationUserInfo) {
console.log("Launched from Notification from Cold State");
// This is where you could get a Record ID from this.props.initialNotificationUserInfo
// and redirect to the appropriate page, for example.
}
PushNotificationIOS.addEventListener('localNotification', this._onLocalNotification);
}
componentWillUnmount() {
PushNotificationIOS.removeEventListener('localNotification', this._onLocalNotification);
}
_onLocalNotification( notification ) {
if (AppState.currentState != 'active') {
console.log("Launched from Notification from Background or Inactive state.");
}
else {
console.log("Not Launched from Notification");
}
}
Environment:
- React Native 0.28
- iOS Tested Only
- Local Push Notifications Only
- Simulator and Real Device Tested
- Mac El Capitan
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:35 (13 by maintainers)
Top Results From Across the Web
PushNotificationIOS.getInitialNotification() always returns null ...
PushNotificationIOS.getInitialNotification() always returns null when the app is in dead state. I'm using react native 59.
Read more >PushNotificationIOS · React Native
The first caller of popInitialNotification will get the initial notification object, or null . Subsequent invocations will return null. getInitialNotification().
Read more >react-native-community - Bountysource
At iOS, it builds successfully, however, when the app starts, I get this error: Invariant Violation: Native module cannot be null. constructor ...
Read more >Notifications - Expo Documentation
Local path to an image to use as the icon for push notifications. ... getInitialURL(); if (url != null) { return url; }...
Read more >Notifications | React Native Firebase
Looking for an advanced local notifications library which integrates with FCM? Check out Notifee! Displaying a Notification. The Firebase Cloud Messaging SDKs ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Right. In Xcode you can set the Debugger to launch when the app launches (like from a notification). Just edit your Scheme and look for this:
And maybe instead of a breakpoint just
NSLogoutappProperties.Closing this issue because it has been inactive for a while. If you think it should still be opened let us know why.