Handling 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
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
NSLog
outappProperties
.Just wanted to leave a +1 for @dwilt & @joshuapinter . Helped me immensely!
Closing this issue because it has been inactive for a while. If you think it should still be opened let us know why.