question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

onRegister and onNotification not being called for remote notification

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
vinayjagscommented, Dec 17, 2020

@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.

1reaction
Dallas62commented, Nov 24, 2020

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,

Read more comments on GitHub >

github_iconTop Results From Across the Web

onNotification never gets called when sending Remote Push ...
I am using RN 0.49 and this library: https://github.com/zo0r/react-native-push-notification for handling push notifications. My component ...
Read more >
Push Notifications - Working with API - React Native
Breakdown of using the Push Notifications API in Amplify - React Native - AWS ... You can use onNotification , onRegister and onNotificationOpened...
Read more >
Push Notifications - Optimizely
onRegister callback will not be called until ... onNotification callback is called when a push notification gets to the phone. By default, the...
Read more >
Implementing React Native Push Notifications in Android Apps
Push notifications are still one of the best ways to get mobile app ... Called when Token is generated (iOS and Android) onRegister: ......
Read more >
react-native-push-notification - npm
React Native Local and Remote Notifications for iOS and Android ... (optional) Called when Token is generated (iOS and Android) onRegister: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found