phonegap / phonegap-plugin-push

Register and receive push notifications
MIT License
1.94k stars 1.91k forks source link

iOS FCM: Plugin initialization invalidates existing device token (with detailed explanation) #2613

Open mreis1 opened 5 years ago

mreis1 commented 5 years ago

Expected Behaviour

After PushNotification.init() token is received in the 'registration' event handler. This token should be valid if we try to send a push to this device.

Actual Behaviour

The token received is returned as InvalidRegistration.

{
    "multicast_id": 844295xxxxxx468,
    "success": 0,
    "failure": 1,
    "canonical_ids": 0,
    "results": [
        {
            "error": "**InvalidRegistration**"
        }
    ]
}

Reproduce Scenario (including but not limited to)

1) Push to token obtained in Swift app using FCM (same bundle id than cordova app)

image

2) Push to token obtained in cordova's app using FCM (same bundle id)

image

3) The push now fails

image

Platform and Version (eg. Android 5.0 or iOS 9.2.1)

IPhone 6 - iOS 11.4

Cordova CLI version and cordova platform version

cordova --version  = 8.1.2                                   
cordova platform version ios = 4.5.5

Plugin version

phonegap-plugin-push 2.2.2 "PushPlugin"
phonegap-plugin-push 2.2.3 "PushPlugin"

Tested in both

Sample Push Data Payload

Visible in the above image.

Initialization code

Update: I forgot to add the init code

const pushConf:any = {
        android: {}, 
        ios: {
          sound: true,
          alert: true,
          badge: true,
          categories: {
            invite: {
              yes: {
                callback: 'accept',
                title: 'Accept',
                foreground: true,
                destructive: false
              },
              no: {
                callback: 'reject',
                title: 'Reject',
                foreground: true,
                destructive: false
              },
              maybe: {
                callback: 'maybe',
                title: 'Maybe',
                foreground: true,
                destructive: false
              }
            },
            delete: {
              yes: {
                callback: 'doDelete',
                title: 'Delete',
                foreground: true,
                destructive: true
              },
              no: {
                callback: 'cancel',
                title: 'Cancel',
                foreground: true,
                destructive: false
              }
            }
          }
        }
      }

      console.log('PushPlugin is being initialized with ', pushConf)
      var push = PushNotification.init(pushConf)
      this.pushPlugin = push;
      push.on('notification', data => {
        console.log('-----NOTIFICATION-----')
        // console.log(data.message);
        // console.log(data.title);
        // console.log(data.count);
        // console.log(data.sound);
        // console.log(data.image);
        // console.log(data.additionalData);
        console.log({notification:data})
        console.log('-----END_NOTIFICATION-----')
      })

      push.on('emailGuests', data => {
        console.log('I should email my guests', {data});
      });

      push.on('snooze', data => {
        console.log('Remind me later', data);
      });

      push.on('registration',  (data:any) => {
        console.log(data.registrationId);
        // FCM or APNS
        console.log(data.registrationType);
      });

      // iOS events
      var iosBtnEvents = [
        'accept',
        'reject',
        'maybe',
        'doDelete',
        'cancel'
      ]
      iosBtnEvents.forEach(event=>{
        ((event)=>{
          console.log('iOS Button Event: ' + event);
          push.on(event, (...args) => {
            console.log(event,{args});
          })    
        })(event)
      })

      push.subscribe('xx', console.log, console.error)

Additional Information:

After the PushNotification.init() invalidate my token, I decided to open xcode and flash once again my native swift iOS app to the device. Here's the log printed out on XCODE console.

2018-11-03 18:44:13.704532+0100 MessagingExample[615:182860]  - <AppMeasurement>[I-ACS036002] Analytics screen reporting is enabled. Call +[FIRAnalytics setScreenName:setScreenClass:] to set the screen name or override the default screen class name. To disable screen reporting, set the flag FirebaseScreenReportingEnabled to NO (boolean) in the Info.plist
2018-11-03 18:44:13.948107+0100 MessagingExample[615:182861] 5.11.0 - [Firebase/Analytics][I-ACS023007] Analytics v.50300000 started
2018-11-03 18:44:13.956341+0100 MessagingExample[615:182861] 5.11.0 - [Firebase/Analytics][I-ACS023008] To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled (see http://goo.gl/RfcP7r)
2018-11-03 18:44:14.004903+0100 MessagingExample[615:182861] 5.11.0 - [Firebase/Messaging][I-FCM001000] FIRMessaging Remote Notifications proxy enabled, will swizzle remote notification receiver handlers. If you'd prefer to manually integrate Firebase Messaging, add "FirebaseAppDelegateProxyEnabled" to your Info.plist, and set it to NO. Follow the instructions at:
https://firebase.google.com/docs/cloud-messaging/ios/client#method_swizzling_in_firebase_messaging
to ensure proper integration.
2018-11-03 18:44:14.070327+0100 MessagingExample[615:182819] FCM registration token: cYA-qIVt6BQ:APA91bE2Bl3XXXXXXXXXXXXXXXXXXXXXXXBAVs_JwMene_QLCIEtaRBnBTNHM1DwO3x_SHjMgYX3L5SazM1uBCiCDQICmWIcGxzbKbkwAXjaba1HpJRUn4M75K
2018-11-03 18:44:14.071354+0100 MessagingExample[615:182819] APNs device token retrieved: <04bdf723 906529c6 b9d03821 d5945161 550d8bae ea04a757 d125fc52 d91ef19f>
2018-11-03 18:44:14.158566+0100 MessagingExample[615:182864] TIC Read Status [1:0x0]: 1:57
2018-11-03 18:44:14.158633+0100 MessagingExample[615:182864] TIC Read Status [1:0x0]: 1:57
2018-11-03 18:44:14.637404+0100 MessagingExample[615:182819] FCM registration token: cYA-qIVt6BQ:APA91bFPTNG-eNZUOZob_EkoFeLvy4Ifnept08CGnsaajn2k6E-bIWuUQyn_YHj36CiFQXXXXXXXXXXXXXXXXXXXXXXXAVX9SCrHW65LEEPFVkOqLQTHGnWqBRw

The swift module used here is part of FCM's quickstart-ios available on github.

mreis1 commented 5 years ago

@macdonst this issue was classified as a "question", but shouldn't it be treated as a bug? If you need additional information please let me know.

Is someone else having the same problem? In order to receive pushes I had to switch to APN but I lose all the benefits of FCM, such as topics.

vienom commented 5 years ago

Same problem here. Here is my payload:

{ "notification":{ "sound": "default", "title": "title", "body": "body", "someKey" : "someValue" }, "registration_ids": ["fIHl-WqXVJU:APA91bHPhBsYo8ejMGIrhLbUxTtSXLt7CAqV2Z74oxZpZs_a41lfxNLja20F1dkPDTdDGKokgJirnQxXhBoWKOgWB7mygpcl8QPkCwzm5_7X3guUmvFmHpAqLiyrNCsYLHCPdZfSWV5_"], "priority": "high", "content_available": true, "mutable_content": true }

The token is copied from the console after the registration event. No problems on Android.

mreis1 commented 5 years ago

For a few days I thought I was the only was facing this issue. I think there's something going wrong when the plugin attempts to get the token from the FCM server. This invalidation makes it impossible to receive the push notification on iOS devices.

RazaGR commented 5 years ago

Same InvalidRegistration here for iOS, any update on this?

mreis1 commented 5 years ago

I don't know @Divxtaman. For now the issue is classified as a question.

mreis1 commented 5 years ago

Anyone else having this issue?

tbs-arpit commented 5 years ago

We are facing this too in 2 of our applications. Since last few days. Ironic part is it works if we install the apps from stores but not for the one which is in TestFlight. at least existing customers are good. but I can't figure out the reason. as far as code goes we didn't changed anything from the one in the store. So it seems like something is changed in Firebase side. Haven't tried it with APN directly.

mreis1 commented 5 years ago

That's really weird, but the problem has to be related with something going wrong when this plugin attempts to hit the FCM server. Otherwise I would be able to use a native FCM swift implementation that worked in non production environment. Now we need to try to figure out what's going wrong in this authorization process.

tbs-arpit commented 5 years ago

Fixed it. It was the pods

installed it and it worked

pod 'Firebase/Core'
pod 'Firebase/Messaging'
mreis1 commented 5 years ago

My Pods are installed. Still it doesn't work.

jeroenmaas commented 5 years ago

An other related issue had the answer for me: https://github.com/phonegap/phonegap-plugin-push/issues/2644#issuecomment-458858321

Try this in Xcode: File > Workspace Settings, change Build System to Legacy Build system