zo0r / react-native-push-notification

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

Push notication onregister method not working #480

Closed gerardus1995 closed 2 years ago

gerardus1995 commented 7 years ago

In android everything is working fine, but in ios i don't get the token (onregister not called), eventought i get localnotifications and the onnotification method is called. I have xcode capabilities enabled and everything setup correctly i think. Here is my configure code:

registerPushNotification(){ PushNotification.configure({

        // (optional) Called when Token is generated (iOS and Android)
        onRegister: function(token) {
          console.log(token);
        },

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

        // ANDROID ONLY: (optional) GCM Sender ID.
        senderID: "940602572864",

        // 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: true,
    });

}

shrutic commented 7 years ago

Did you follow the steps in the https://facebook.github.io/react-native/docs/pushnotificationios.html

gerardus1995 commented 7 years ago

Yes! I did everything.

gerardus1995 commented 7 years ago

I'm getting the notifications to pop up, and the onnotification method is working.

shrutic commented 7 years ago

So can this be closed? Given that the onNotification method is working?

gerardus1995 commented 7 years ago

Well the bug here is that onregister is not getting called so i can't get the token.

Amila17 commented 7 years ago

@gerardus1995 if your running on the simulator, the onRegister method won't get called. You need to run on an actual device. I had the same issue the first time.

nicotroia commented 7 years ago

I can't seem to get the onRegister handler to fire either. I'm running on a device and requestPermissions is set to true

Amila17 commented 7 years ago

@nicotroia I had the same issue when I was first setting it up. I took the following steps to make sure everything was ok:

1) Make sure you have generated a certificate for Push-Notifications for your apple developer app id.

2) Make sure the updated provisioning file is attached to your iOs project.

3) Make sure the capabilities for your iOs X-Code project is set to On/Enabled for Push Notifications

4) Make sure your create a NotificationController component that get's called at the root level of your app.

Hopefully that helps you :).

LA commented 7 years ago

None of this works for me for trying to just get my APNS token on iOS

I installed and linked everything along with deleting and reinstalling the app, but onRegister is never being called on my device even though I get the notifications permission request. I accept them and then onRegister is not called.

This is really disappointing as all I want is the device token and I can't get it.

I was trying to use react-native-fcm, but they don't allow you to grab APNS token. Is it possible there's some kid of conflict?

LA commented 7 years ago

In my case, it was in fact react-native-fcm AppDelegate methods that were conflicting with PushNotificationIOS AppDelegate methods. Removing the react-native-fcm methods fixed my issue.

ignivahermenpreet commented 6 years ago

I have same issue but in android not in IOS. onRegister method is not called in android.

roysG commented 6 years ago

Same problem in android, Any update?

hugoh59 commented 6 years ago

Same problem on Android, it works for iOS but not being called in Android at all.

roysG commented 6 years ago

Any update?

hugoh59 commented 6 years ago

@roysG I just managed to make it work. Here is how: Go to https://console.firebase.google.com and create a project, then find your Sender ID following this guide https://dev.tapjoy.com/faq/how-to-find-sender-id-and-api-key-for-gcm/ then copy paste your Sender ID inside the PushNotification.configure method like so:

PushNotification.configure({
      senderID: '***************',
      ...
})

Make sure the above code is inside the componentDidMount function of your app.js file or however you called your main file.

roysG commented 6 years ago

Checking..

roysG commented 6 years ago

i did it, no errors, but also the device is not asking for me to allow the permissions for the notifications.(android galaxy s5)

hugoh59 commented 6 years ago

can you check with console.log("TOKEN", token) to see if you get it? I'm on Moto 4 and it's not asking for permission on Android apparently but still works

roysG commented 6 years ago

i put some akert in the current event (onRegister) also on event onError, none of them cb fired

This is drive me crazy, i will try now also in other device.

hugoh59 commented 6 years ago

Make sure you're not inserting senderID inside the onRegister function, it has to be outside, like so:

PushNotification.configure({
senderID: '***********',
onNotification: function(notification) {
 ....
},
onRegister: function(token) {
console.log("TOKEN", token)
....
}
})
roysG commented 6 years ago

Mazal Tov :))) I did not wrote the name of the componentDidMount As needed

danielzzz commented 6 years ago

it worked for me in both emulator and the devices when I did the manual install in android, especially the permissions in AndroidManifest.xml

receiving notifications also worked in an android emulator (make sure you're using an google api system and you're logged in with your account)

giankotarola commented 6 years ago

I had the same problem (onRegister not called) on Android emulator and devices and it was fixed adding this line on AndroidManifest.xml:

<application ....>
    .........
    <service android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationRegistrationService"/>
    .........
</application>
kevinNejad commented 6 years ago

onRegister takes almost 1 minute to be fired on iOS.

vygandas commented 6 years ago

I was messing a lot with configuration and when I added line that was in @giankotarola comment it generated a token successfully! On Android. đŸ™‚

react-native 0.55.2 react-native-push-notification 3.1.1

losheredos commented 6 years ago

++ I have same issue, I have tried everything which is written here and no reaction..

1nfinity-5starZ commented 5 years ago

@giankotarola that solved it for me ! thanks

leemlwando commented 5 years ago

@giankotarola thanks buddy, your solution worked for me....was facing a similar problem

https://github.com/leemlwando/reactNative-redux-boilerplate.git

bernhardt1 commented 5 years ago

@leemlwando or @1nfinity-5starZ, would you mind posting what your PushNotification.onRegister call looks like?

I can't get it to trigger for the life of me. Tried everything in this thread and others.

leemlwando commented 5 years ago

@leemlwando or @1nfinity-5starZ, would you mind posting what your PushNotification.onRegister call looks like?

I can't get it to trigger for the life of me. Tried everything in this thread and others.

check AndroidManifest.xml of https://github.com/leemlwando/reactNative-redux-boilerplate.git. i was working on that project

1nfinity-5starZ commented 5 years ago

I have a file called NotifService.js `import PushNotification from 'react-native-push-notification';

export default class NotifService {

constructor(onRegister, onNotif) { this.configure(onRegister, onNotif);

this.lastId = 0;

}

configure(onRegister, onNotif) { PushNotification.configure({ // (optional) Called when Token is generated (iOS and Android) onRegister: onRegister,

  // (required) Called when a remote or local notification is opened or received
  onNotification: onNotif,

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

  // 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: true,
});

} }`

and then on App.native.js:

`import React, { Component } from 'react'; import { Provider } from 'react-redux'; import { PushNotificationIOS, Platform } from 'react-native'; import { PersistGate } from 'redux-persist/lib/integration/react';

import Template from './src/Template'; import FullScreenLoading from './src/components/common/FullScreenLoading'; import { persistor, store } from './src/states';

import NotifService from './NotifService';

export default class App extends Component{ constructor(props){ super(props);

this.notif = new NotifService(this.onRegister, this.onNotif);

this.state = {
  deviceToken: null,
}    

}

onNotif = (notif) => { console.log(notif); if(notif.userInteraction || notif.foreground && Platform.OS == 'ios') { this.setState({ additionalData: { navigateTo: notif.navigateTo || (notif.data || {}).navigateTo } }) } notif.finish(PushNotificationIOS.FetchResult.NoData); }

onRegister = (token) => { console.log({token}) this.setState({ deviceToken: token }); }

render() { return(

} persistor={persistor}>