zo0r / react-native-push-notification

React Native Local and Remote Notifications
MIT License
6.77k stars 2.05k forks source link

What Callback to run any statement when notification is show in iOS #2411

Open muslimmuda15 opened 6 months ago

muslimmuda15 commented 6 months ago

I try to implementation like this:

class NotifHandler {
  onNotification(notification) {
    console.log('onNotifHandler:', notification);

    if (typeof this._onNotification === 'function') {
      this._onNotification(notification);
    }
  }

  onRegister(token) {
    // console.log('NotifHandler:', token);

    if (typeof this._onRegister === 'function') {
      this._onRegister(token);
    }
  }

  onRemoteFetch(notificationData) {
    console.log('onRemoteFetchNotifHandler:', notificationData);
  }

  attachRegister(handler) {
    this._onRegister = handler;
  }

  attachNotification(handler) {
    this._onNotification = handler;
  }
}

const handler = new NotifHandler();

PushNotification.configure({
  // (optional) Called when Token is generated (iOS and Android)
  onRegister: handler.onRegister.bind(handler),

  // (required) Called when a remote or local notification is opened or received
  onNotification: handler.onNotification.bind(handler),

  // IOS ONLY (optional): default: all - Permissions to register.
  permissions: {
    alert: true,
    badge: true,
    sound: true,
  },

  // Should the initial notification be popped automatically
  // default: true
  popInitialNotification: true,

  /**
   * (optional) default: true
   * - Specified if permissions (ios) and token (android and ios) will requested or not,
   * - if not, you must call PushNotificationsHandler.requestPermissions() later
   */
  requestPermissions: true,
});
PushNotification.localNotificationSchedule({
    date: new Date(Date.now() + 10 * 1000),
    title: "Title",
    message: "Message",
    playSound: true,
    number: 10,
    soundName: 'default'
})

But after 10 seconds notification is work well, but onNotification seems not working when notification is showing. How to get callback notification when showing, not by tapping?