react-native-push-notification icon indicating copy to clipboard operation
react-native-push-notification copied to clipboard

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

Open muslimmuda15 opened this issue 1 year ago • 0 comments

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?

muslimmuda15 avatar May 15 '24 07:05 muslimmuda15