zo0r / react-native-push-notification

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

onRegister and onNotification not being called for remote notification #1757

Open shivenmian opened 3 years ago

shivenmian commented 3 years ago

Bug

I'm building an application wherein notifications can come up at random times of the day. Since local notifications wouldn't work when the application is closed, I'm using remote push notifications solely for this purpose (i.e have the notifications come up even if the application is closed).

For remote push notifications I'm using this library and I have registered my app on Firebase (also enabled push notification capability for iOS) and followed the steps on this repo, however it still isn't working. I just checked again I see that onRegister and onNotification are not being called, so I don't think the remote notification is being fired. Also, in the cloud messaging section of Firebase console, it does not report any activity.

Environment info

react-native info output:

System:
    OS: macOS 11.0.1
    CPU: (4) x64 Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz
    Memory: 356.09 MB / 8.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 15.2.0 - /usr/local/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 7.0.10 - /usr/local/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.9.3 - /Users/shivenmian/.rvm/rubies/ruby-2.7.0/bin/pod
  SDKs:
    iOS SDK:
      Platforms: iOS 14.2, DriverKit 20.0, macOS 11.0, tvOS 14.2, watchOS 7.1
    Android SDK:
      API Levels: 14, 22, 23, 24, 25, 26, 27, 28, 29, 30
      Build Tools: 23.0.2, 25.0.0, 25.0.2, 25.0.3, 26.0.1, 26.0.2, 27.0.3, 28.0.3, 29.0.2, 30.0.0, 30.0.1, 30.0.2
      System Images: android-27 | Google APIs Intel x86 Atom
      Android NDK: 21.3.6528147
  IDEs:
    Android Studio: 4.0 AI-193.6911.18.40.6626763
    Xcode: 12.2/12B45b - /usr/bin/xcodebuild
  Languages:
    Java: 14.0.1 - /usr/bin/javac
    Python: 3.8.5 - /Users/shivenmian/.pyenv/shims/python
  npmPackages:
    @react-native-community/cli: Not Found
    react: 16.13.1 => 16.13.1 
    react-native: ^0.63.2 => 0.63.3 
  npmGlobalPackages:
    *react-native*: Not Found
 // paste it here

Library version: 6.1.3

Steps To Reproduce

The notification code is below (I have blanked out the senderID but have added it in the code):


class NotificationController {
  cancelNotifications() {
    PushNotification.cancelAllLocalNotifications();
  }

  showNotification(promptTitle, promptMessage) {

    PushNotification.createChannel(
      {
        channelId: "alvachannel", // (required)
        channelName: "ALVA Remote Notification Channel", // (required)
        channelDescription: "ALVA Notification Channel", // (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.
    );

    PushNotification.getChannels(function (channel_ids) {
      console.log(channel_ids); // ['channel_id_1']
    });

    PushNotification.localNotification({
      channelId: "alvachannel",
      date: new Date(),
      /* Android Only Properties */
      id: "0", // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID
      ticker: "My Notification Ticker", // (optional)
      autoCancel: true, // (optional) default: true
      largeIcon: "ic_notification", // (optional) default: "ic_launcher"
      smallIcon: "ic_notification", // (optional) default: "ic_notification" with fallback for "ic_launcher"
      //bigText: "big text ", // (optional) default: "message" prop
      //subText: "subText", // (optional) default: none
      color: "red", // (optional) default: system default
      vibrate: true, // (optional) default: true
      vibration: 300, // vibration length in milliseconds, ignored if vibrate=false, default: 1000
      tag: "some_tag", // (optional) add tag to message
      group: "group", // (optional) add group to message
      ongoing: false, // (optional) set whether this is an "ongoing" notification
      priority: "high", // (optional) set notification priority, default: high
      visibility: "public", // (optional) set notification visibility, default: private
      importance: "high", // (optional) set notification importance, default: high
      ignoreInForeground: false,
      allowWhileIdle: "true",
      /* iOS only properties */
      //      alertAction: // (optional) default: view
      //      category: // (optional) default: null
      //      userInfo: // (optional) default: null (object containing additional notification data)

      /* iOS and Android properties */
      title: promptTitle, // (optional)
      message: promptMessage, // (required)
      playSound: true, // (optional) default: true
      soundName: "default", // (optional) Sound to play when the notification is shown. Value of 'default' plays the default sound. It can be set to a custom sound such as 'android.resource://com.xyz/raw/my_sound'. It will look for the 'my_sound' audio file in 'res/raw' directory and play it. default: 'default' (default sound is played)
      //number: 10 // (optional) Valid 32 bit integer specified as string. default: none (Cannot be zero)
      //repeatType: 'day', // (optional) Repeating interval. Check 'Repeating Notifications' section for more info.
      //actions: '["Yes", "No"]',  // (Android only) See the doc for notification actions to know more
    });
  }

  configureNotification() {
    PushNotification.configure({
      // (optional) Called when Token is generated (iOS and Android)
      onRegister(token) {
        logger.info(
          codeFileName,
          "configureNotification",
          "Configuring notification. TOKEN: " + token
        );
      },

      // (required) Called when a remote or local notification is opened or received
      onNotification(notification) {
        logger.info(
          `${codeFileName}`,
          "onNotification",
          "App opened from notification: " + JSON.stringify(notification)
        );

        // required on iOS only (see fetchCompletionHandler docs: https://facebook.github.io/react-native/docs/pushnotificationios.html)

        notification.finish(PushNotificationIOS.FetchResult.NoData);
        onAppOpen();
      },

      onAction: function (notification) {
        console.log("ACTION:", notification.action);
        console.log("NOTIFICATION:", notification);

        // process the action
      },

      onRegistrationError: function(err) {
        console.error(err.message, err);
      },

      // ANDROID ONLY: GCM or FCM Sender ID (product_number) (optional - not required for local notifications, but is need to receive remote push notifications)
      senderID: xxxxxxx,

      // 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: false,

      /**
       * (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: Platform.OS === 'ios'
      requestPermissions: true,

    });
  }
}

Describe what you expected to happen:

  1. Notifications should fire up when I close the app
  2. Even if 1 does not work, then the notification should come up on the Firebase console (since notifications work when app is open). However, even for foreground notifications, it does not show up on Firebase.

Reproducible sample code

shivenmian commented 3 years ago

Also, just wanted to confirm:

  1. There is no need of using rnfirebase for setting up push notifications right? I think I saw in one of the past issues that it's been integrated into this library.

  2. Is remote notifications the right way of going about this? I'm specifically talking about my intended feature of having the notifications come up when the application is closed. I think people some also use 'data-only' notifications to wake up the app and then use local notification to show the notification, but remote notifications should serve the same purpose right?

Thanks in advance for your help!

Dallas62 commented 3 years ago

Hi,

How NotificationController.configureNotification() is called ?

Refer to the documentation:

DO NOT USE .configure() INSIDE A COMPONENT, EVEN App If you do, notification handlers will not fire, because they are not loaded. Instead, use .configure() in the app's first file, usually index.js.

  1. RNFirebase can be used for remote notifications and this library for triggering local notification.
  2. using data-only to trigger a local notification is a normal use case.

Regards,

ogous commented 3 years ago

@Dallas62 I am trying suggested workout: use data-only message to wake up the app, then with onNotification event show a local notification. With a data only message app is waking but on the first time local notification is not firing, only on second and after messages showing with local notification. Is this a expected behavior on ios? Is there a way to wake up the app with first message and use it also on showing a local notification?

vinayjags commented 3 years ago

Hi,

How NotificationController.configureNotification() is called ?

Refer to the documentation:

DO NOT USE .configure() INSIDE A COMPONENT, EVEN App If you do, notification handlers will not fire, because they are not loaded. Instead, use .configure() in the app's first file, usually index.js.

  1. RNFirebase can be used for remote notifications and this library for triggering local notification.
  2. using data-only to trigger a local notification is a normal use case.

Regards,

Hello @Dallas62 , the attached sample code in the git repo itself is not following the documentation. The Config is called in App. I am also facing an issue when a remote notification opens the app, but the onNotification is not triggered.

Dallas62 commented 3 years ago

Hi @vinayjags

The exemple project doesn't provide something called "NotificationController". And in the example project and in an other issue, your comment has been answered: No, .configure() is not called inside the App component, in the exemple project.

Dallas62 commented 3 years ago

1772

vinayjags commented 3 years ago

@Dallas62 thanks for the response. Sorry my bad. But the problem is that, I am using redux and Redux saga to sync my fcm token to server. now with this approach, the app cannot access the redux store which is needed to sync the token. Not sure if this is related to the library.. just wanted to make sure, I am not doing something.

shivenmian commented 3 years ago

@Dallas62 Thanks for the reply.

  1. I checked up rnfirebase and I'm trying to use that for data-only notification. Would I need a separate server of my own to trigger firebase to send the app a data-only notification to wake the app up? Or can I get firebase to send the app a data-only notification within the app logic itself?

  2. The readme says that this library can be used for "remote notification". I assumed that "remote notification" involved communicating with the Firebase server for getting notifications. But does this remote notification not wake up the app (like data-only notification)? I'm just trying to understand the difference between data-only and remote notifications.

shivenmian commented 3 years ago

@Dallas62 what do you think? ^