React-native-push-notification: onRegister is not being called

Created on 3 Aug 2016  路  18Comments  路  Source: zo0r/react-native-push-notification

Hi there,
I am using react-native 0.30.0 and react-native-push-notification 2.0.2 and an Android device and have set up everything like in the README but when I call

var PushNotification = require('react-native-push-notification');

PushNotification.configure({
    onRegister: function(token) {
        console.log( 'TOKEN:', token );
    },
    onNotification: function(notification) {
        console.log( 'NOTIFICATION:', notification );
    },
    senderID: "MY GCM SENDER ID",
});

nothing happens (neither in logcat nor anything else).

Do you know what the issue may be or how to debug this?

Most helpful comment

Hi i am using ReactNative 0.31 and com.google.android.gms:play-services-gcm:8.1.0 with a valid project id but the onRegister callback is not triggering neither on the real device nor on emulator. This is what i have so far

PushNotification.configure({
    onRegister: function(token) {
        ToastAndroid.show(`TOKEN : ${token}`, ToastAndroid.SHORT);
        console.log( 'TOKEN:', token );
    },
    onNotification: function(notification) {
        console.log( 'NOTIFICATION:', notification );
    },
    senderID: "12345",
    popInitialNotification: true,
    requestPermissions: false
});
PushNotification.requestPermissions();

Am i missing some thing ? BTW I am upgrading the application from 0.25 to 0.31 and it is working in the old application.

All 18 comments

Mi example worked when I actually added the GCM SENDER ID (although the docs say is optional). Also you should test on a real device not an emulator.
You can get your Project ID at https://console.cloud.google.com

My fault: I wrote senderId instead of senderID

Hi i am using ReactNative 0.31 and com.google.android.gms:play-services-gcm:8.1.0 with a valid project id but the onRegister callback is not triggering neither on the real device nor on emulator. This is what i have so far

PushNotification.configure({
    onRegister: function(token) {
        ToastAndroid.show(`TOKEN : ${token}`, ToastAndroid.SHORT);
        console.log( 'TOKEN:', token );
    },
    onNotification: function(notification) {
        console.log( 'NOTIFICATION:', notification );
    },
    senderID: "12345",
    popInitialNotification: true,
    requestPermissions: false
});
PushNotification.requestPermissions();

Am i missing some thing ? BTW I am upgrading the application from 0.25 to 0.31 and it is working in the old application.

+1

onRegister isn't triggering for me either. Confirmed with a fresh project init in react-native 0.31.0 and react-native-push-notification 2.0.2.

And with react-native 0.29.0 and react-native-push-notification 2.0.1 onRegister successfully triggers but onNotification does not.

Ok disregard that. Apparently a token cannot be granted until your application's root component has been constructed. @sajjad26 @BigPun86 Make sure you are calling PushNotification.requestPermissions(YOUR_SENDER_ID) after that. You'll need to pass through the sender id even if you set it in PushNotification.configure.

Seems to be working inside componentDidMount of the App. Thanks @jamealg

same issue here, is not working with react-native 0.36.1 and latest version of this plugin.

componentDidMount() {
        //alert('entro!')
        PushNotification.configure({

            // (optional) Called when Token is generated (iOS and Android)
            onRegister: function(token) {
                //alert(token+' => HOLA!')
                console.log( 'TOKEN:', token );


            },

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

            // ANDROID ONLY: GCM Sender ID (optional - not required for local notifications, but is need to receive remote push notifications) 
            senderID: "MYGCM", //mismo que el de IONIC

            // 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: false,
        });
        PushNotification.requestPermissions("MYGCM");
    }

It worked like a charm on the iOS.
But for Android, on the real device of course, I struggled to get the device token. As @juanpasolano said senderID is not optional. When I put the value of senderID in place, I achieved to generate the device token. I observed that generated device token differs according to the value of senderID. It somehow needs that senderID value to generate a device token.

HELPPPP ^^

@milkers did you manage to make it work on Android?

Yes, it works on Android with RN 0.39.2. I was lost about what value should I put as senderId. Because I thought that device token should be same independent from the senderId which is obviously a wrong assupmtion. And also instead of GCM you should use FirebaseCM to obtain a senderId nowadays.

I can confirm that onRegister is called on iOS but not on Android, although i've set the SenderID and tested on real device.

But i face the same issue using OneSignal. Maybe the root cause is outside of this library and somewhere in firebase or messed up senderIDs or something like this ...

We kept it in a constructor which worked on iOS but caused issues on Android. Moving the PushNotification.configure to componentDidMount seems to fix it.

I had the same issue today. Ultimately, the issue was that I needed to find the right id number in FCM. Note that your sender ID should be a long number, so if it isn't, then chances are you have the wrong id number.

I followed these instructions to get my sender id: https://dev.tapjoy.com/faq/how-to-find-sender-id-and-api-key-for-gcm/

@azapps so you mean setting the senderID makes the onRegister() being triggered for you?

Thank you @jacobbeasley ! Thanks to your comment I realised that my sender ID was wrong.

Updating it with the right sender ID makes the inRegister() being triggered on Android like it is done on iOS. 馃憤

The local notification is called, but neither my connected device nor the virtual device calls onRegister. I have read the troubleshooting section - that's why I use a connected device now. Do I need to mention that I changed the sender ID for public display?

Calling requestPermissions not only seems redundant but it doesn't fix anything, but I did try that.

What fixes this?

PushNotification.configure({
  // (optional) Called when Token is generated (iOS and Android)
  onRegister: function(token) {
    // ^^^^ This is never called ^^^^
    console.log("TOKEN:", token);
  },

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

  senderID: "1234567890123",

  // 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.localNotification({
  // 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_launcher", // (optional) default: "ic_launcher"
  smallIcon: "ic_notification", // (optional) default: "ic_notification" with fallback for "ic_launcher"
  bigText: "My big text that will be shown when notification is expanded", // (optional) default: "message" prop
  subText: "This is a 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: "private", // (optional) set notification visibility, default: private
  importance: "high", // (optional) set notification importance, default: high

  // iOS and Android properties
  title: "My Notification Title", // (optional)
  message: "My Notification Message", // (required)
  playSound: false, // (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
});

PS C:\Users\me\project> react-native --version 
react-native-cli: 2.0.1
react-native: 0.61.4

"react-native-push-notification": "^3.1.9",

The fact that the calls are made in componentDidMount has no effect.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

uendar picture uendar  路  3Comments

atteia picture atteia  路  4Comments

cookienawer picture cookienawer  路  3Comments

GastonEDiaz picture GastonEDiaz  路  3Comments

DanDance picture DanDance  路  3Comments