zo0r / react-native-push-notification

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

onNotification not called until notification is opened #1789

Closed CTOverton closed 3 years ago

CTOverton commented 3 years ago

Bug

Notifications display to the user both local and remote, but the onNotification handler is not called until the user clicks on the notification to open it.

Environment info

react-native info output:

System:
    OS: macOS 10.15.4
    CPU: (16) x64 Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz
    Memory: 2.49 GB / 32.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 12.16.3 - /usr/local/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 6.14.4 - /usr/local/bin/npm
    Watchman: Not Found
  Managers:
    CocoaPods: 1.9.1 - /usr/local/bin/pod
  SDKs:
    iOS SDK:
      Platforms: iOS 14.2, DriverKit 20.0, macOS 11.0, tvOS 14.2, watchOS 7.1
    Android SDK: Not Found
  IDEs:
    Android Studio: 4.1.1 4.1.1
    Xcode: 12.2/12B45b - /usr/bin/xcodebuild
  Languages:
    Java: 11.0.7 - /usr/bin/javac
    Python: 2.7.16 - /usr/bin/python
  npmPackages:
    @react-native-community/cli: Not Found
    react: 16.13.1 => 16.13.1 
    react-native: 0.63.3 => 0.63.3 
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

Library version: 6.1.3

Steps To Reproduce

  1. Receive remote of local notification
  2. Notice no logs are made
  3. Click on the notification, notice logs for handling notification are now available

Describe what you expected to happen:

  1. Receive remote of local notification
  2. console should output NOTIFICATION: {...notification info}

Reproducible sample code

index.js

...imports

PushNotification.configure({
  // (optional) Called when Token is generated (iOS and Android)
  onRegister: function ({ token, os }) {
    console.log("TOKEN:", token);
    store.dispatch(setDeviceToken(token))
  },

  // (required) Called when a remote is received or opened, or local notification is opened
  onNotification: function (notification) {
    console.log("NOTIFICATION:", notification);

    // process the notification

    // (required) Called when a remote is received or opened, or local notification is opened
    notification.finish(PushNotificationIOS.FetchResult.NoData);
  },

  // (optional) Called when Registered Action is pressed and invokeApp is false, if true onNotification will be called (Android)
  onAction: function (notification) {
    console.log("ACTION:", notification.action);
    console.log("NOTIFICATION:", notification);

    // process the action
  },

  // (optional) Called when the user fails to register for remote notifications. Typically occurs when APNS is having issues, or the device is a simulator. (iOS)
  onRegistrationError: function(err) {
    console.error(err.message, err);
  },

  // 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
   * - if you are not using remote notification or do not have Firebase installed, use this:
   *     requestPermissions: Platform.OS === 'ios'
   */
  requestPermissions: false,
});

PushNotification.createChannel(
  {
    channelId: "journey-channel", // (required)
    channelName: "Journey channel", // (required)
    channelDescription: "A channel to categorise Journey notifications", // (optional) default: undefined.
    soundName: "default", // (optional) See `soundName` parameter of `localNotification` function
    importance: 4, // (optional) default: 4. Int value of the Android notification importance
    vibrate: true, // (optional) default: true. Creates the default vibration patten if true.
  },
  (created) => console.log(`createChannel returned '${created}'`) // (optional) callback returns whether the channel was created, false means it already existed.
);

AppRegistry.registerComponent(appName, () => App);
Dallas62 commented 3 years ago

Hi @CTOverton

Can you please first search in issue history ?

Regards.

CTOverton commented 3 years ago

Hi @CTOverton

Can you please first search in issue history ?

Regards.

Hey @Dallas62, you’re right I don’t know if my issue is enough different from what has been posted. I did look through the other issues first, I wasn’t able to find a fix with what was posted. I was wondering if my setup might just have been incorrect or if my system particularly might be an issue.

I’ll keep looking around to see if I can find a fix but if you could link the specific issue you are referring to here I’d really appreciate it!

Dallas62 commented 3 years ago

The issue just before your issue, regards.