Search code examples
iosxcodereact-nativepush-notificationlocalnotification

LocalNotification not working [IOS] - PushNotificationIOS


I'm trying to implementing local notification in IOS using this @react-native-community/push-notification-ios package.

I followed all the documentation properly. Still, LocalNotification is not working.

This is my environment config: - react-native : 0.61.4 - @react-native-community/push-notification-ios --save : 1.0.5

I did the following things,

  1. npm i @react-native-community/push-notification-ios --save
  2. cd ios && pod install
  3. Updated AppDelegate.m as per described here
  4. Made build : react-native run-ios --device "iPhone X"
  5. Then calling function in my js like this,
import PushNotificationIOS from "@react-native-community/push-notification-ios";
.
.
.
componentDidMount(){
  PushNotificationIOS.addEventListener('localNotification', this._onNotification);

  PushNotificationIOS.requestPermissions();
  PushNotificationIOS.presentLocalNotification({
    alertBody: 'Test Notification'
  });
}

_onNotification(notification) {
  console.log(notification._alert);
}
.
.
.
  • By the way, It requests for permissions when the first-time app opens & also I'm getting console.log of notification but not getting any local notification.

Solution

  • Well, when the app running in the foreground, you won't be able to see the notification. You can call the local schedule function and quick hide app to the background, then you will see the notification.

    Source Code:-

    PushNotificationIOS.localNotificationSchedule({
          message: "Local push notification",   //mandatory
          number: 1,
          date: new Date(Date.now() + (5 * 1000)) // Schedule in 5 secs
        });