`PushNotificationIOS.getInitialNotification` returns null for Local Notifications on Cold Starts.

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:35 (13 by maintainers)

github_iconTop GitHub Comments

5reactions
joshuapintercommented, Jul 22, 2016

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:

screenshot 2016-07-22 13 24 15

And maybe instead of a breakpoint just NSLog out appProperties.

2reactions
hramoscommented, May 25, 2017

Closing this issue because it has been inactive for a while. If you think it should still be opened let us know why.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found