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

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?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 1
  • Comments: 18

Most upvoted comments

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.

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.

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.

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.

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