ionic-team / capacitor

Build cross-platform Native Progressive Web Apps for iOS, Android, and the Web ⚡️
https://capacitorjs.com
MIT License
11.44k stars 977 forks source link

bug: PushNotifications.register() does not fire 'registration' event #3042

Closed DoctorsInTech closed 4 years ago

DoctorsInTech commented 4 years ago

Bug Report

After updating capacitor to latest (2.1.2) the registration event no longer fires on device registration.

Capacitor Version

npx cap doctor output:

💊   Capacitor Doctor  💊 

Latest Dependencies:

  @capacitor/cli: 2.1.2

  @capacitor/core: 2.1.2

  @capacitor/android: 2.1.2

  @capacitor/electron: 2.1.2

  @capacitor/ios: 2.1.2

Installed Dependencies:

  @capacitor/electron not installed

  @capacitor/cli 2.1.2

  @capacitor/android 2.1.2

  @capacitor/core 2.1.2

  @capacitor/ios 2.1.2

[success] Android looking great! 👌
  Found 4 Capacitor plugins for ios:
    cordova-plugin-androidx-adapter (1.1.1)
    cordova-plugin-camera (4.1.0)
    cordova-plugin-inappbrowser (4.0.0-dev)
    cordova-plugin-video-editor (1.1.3)
[success] iOS looking great! 👌

Affected Platform(s)

Occuring on iOS platform.

Current Behavior

Promise from requestPermission() is fired with { "granted": true } in result param. However the registration event is never fired.

Expected Behavior

Registration event should fire in order for me to register device with back end (Azure Notification Hub)

Sample Code or Sample Application Repo

Registration Code:

public connectToPushNotifications(userId: string) {
    this.userId = userId;
    // Request permission to use push notifications
    // iOS will prompt user and return if they granted permission or not
    // Android will just grant without prompting
    PushNotifications.requestPermission().then(result => {
      if (result.granted) {
        console.log('The Result Is: ' + JSON.stringify(result));
        // Register with Apple / Google to receive push via APNS/FCM
        PushNotifications.register();
      } else {
        // Show some error
      }
    });

    // On success, we should be able to receive notifications
   // THIS FUNCTION IS NOT FIRED:
    PushNotifications.addListener('registration',
      (token: PushNotificationToken) => {
        alert('The token is: ' + JSON.stringify(token));
        this.registerWithNotificationHub(token.value, this.userId);
      }
    );

    // Some issue with our setup and push will not work
    PushNotifications.addListener('registrationError',
      (error: any) => {
        alert('Error on registration: ' + JSON.stringify(error));
      }
    );

    // Show us the notification payload if the app is open on our device
    PushNotifications.addListener('pushNotificationReceived',
      (notification: PushNotification) => {
        // this.handleNotification(notification);

        // Need to present notification in app on android I think.
        this.handleForegroundNotification(notification);
      }
    );

    // Method called when tapping on a notification
    PushNotifications.addListener('pushNotificationActionPerformed',
      (notification: PushNotificationActionPerformed) => {
        this.handleNotification(notification.notification);
      }
    );
  }

Output in XCode console:

To Native ->  PushNotifications requestPermission 89845416
⚡️  To Native ->  PushNotifications addListener 89845417
⚡️  To Native ->  PushNotifications addListener 89845418
⚡️  To Native ->  PushNotifications addListener 89845419
⚡️  To Native ->  PushNotifications addListener 89845420
APP INACTIVE
⚡️  TO JS {"isActive":false}
2020-06-03 01:26:45.919879+0100 App[4377:1125630] [ProcessSwapping] 0x110d081a8 - ProvisionalPageProxy::didFailProvisionalLoadForFrame: pageProxyID=15 webPageID=21, frameID=3, navigationID=2
2020-06-03 01:26:45.921916+0100 App[4377:1125630] webView:didFailProvisionalNavigation - -1004: Could not connect to the server.
2020-06-03 01:26:46.030914+0100 App[4377:1125630] [ProcessSuspension] 0x105ad9470 - ProcessAssertion::processAssertionWasInvalidated()
⚡️  [log] - the user doc: [object Object]
⚡️  To Native ->  App addListener 89845421
⚡️  [error] - Unhandled Promise rejection: The file “undefined.svg” couldn’t be opened because there is no such file. ; Zone: <root> ; Task: Promise.then ; Value: {} undefined
⚡️  [error] - Unhandled Promise rejection: The file “undefined.svg” couldn’t be opened because there is no such file. ; Zone: <root> ; Task: Promise.then ; Value: {} undefined
2020-06-03 01:26:46.457399+0100 App[4377:1125630] [ProcessSuspension] 0x105adb120 - ProcessAssertion::processAssertionWasInvalidated()
2020-06-03 01:26:46.457456+0100 App[4377:1125630] [ProcessSuspension] 0x105adb2d0 - ProcessAssertion::processAssertionWasInvalidated()
⚡️  To Native ->  Storage set 89845422
⚡️  TO JS {}
⚡️  TO JS {"granted":true}
⚡️  To Native ->  PushNotifications register 89845423
⚡️  [log] - The Result Is: {"granted":true}
⚡️  TO JS {}
APP ACTIVE

Other Technical Details

npm --version output: npm 6.4.1

node --version output: v10.15.0

pod --version output (iOS issues only): 1.9.1

jcesarmobile commented 4 years ago

I can’t reproduce, can you provide a sample app?

kapten96 commented 4 years ago

I am facing the same problem.

DoctorsInTech commented 4 years ago

It is very difficult to recreate. Yesterday it just started working again without me actually changing anything. And then now today, the event has stopped firing.

I don't know if it is something to do with caching of files, but the firing of the event seems to be quite hit and miss, at least when ran locally on a device from XCode.

jcesarmobile commented 4 years ago

I've tested multiple times, with different apps, during different days and in different devices and I can't reproduce on any of them. Make sure your device is connected to internet, and not behind some proxy that could be blocking the connection to APNS.

DoctorsInTech commented 4 years ago

Okay, it's good to know you can't reproduce it, I wonder if it has something to do with my provisioning profile. Though only seems to happen when run locally. I'll let you know if I find the problem. Thanks.

yassinejebli commented 4 years ago

I had the same issue, restarting my iPhone fixed it, weird I know :D

mdgs commented 3 years ago

I have the same issue, and currently cannot resolve it. onRegistration listener is never triggered. It was working, but then no trigger happen. Did nothing previously. Tried upgrade capacitor from 2.1.0 to 2.2.1, but still nothing happen.

Any solution?

Livijn commented 3 years ago

I am experiencing the same issue. It was working for a while, then all of a sudden it stopped working. Is there perhaps some kind of flooding protection from Apple?

I remember experiencing this with another app earlier in this issue.

ryaa commented 3 years ago

I also had the problem registering push notification/getting the token, I tried many options discussed on the internet and none seemed to work.

Finally, based on the below text from Apple site (https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/HandlingRemoteNotifications.html) I inserted a valid SIM card with active data plan (there was either none used or not active SIM before because this is my development/test iPhone device) and got the registration token successfully.

If a cellular or Wi-Fi connection is not available, neither the application:didRegisterForRemoteNotificationsWithDeviceToken: method nor the application:didFailToRegisterForRemoteNotificationsWithError: method is called. For Wi-Fi connections, this sometimes occurs when the device cannot connect with APNs over the configured port. If this happens, the user can move to another Wi-Fi network that isn’t blocking the required port. On devices with a cellular radio, the user can also wait until the cellular data service becomes available.

Please also see No Delegate Callbacks section at https://developer.apple.com/library/archive/technotes/tn2265/_index.html

After that I removed SIM card and everything still works even though I'm connected to WiFi only. I'm getting registration token and push notification messages.

Livijn commented 3 years ago

I am using a valid phone, valid SIM card with an active data plan but still experiencing this. As I said earlier, it was working for a while. I have read through this StackOverflow issue and tried every answer with no luck. I still feel like there is some kind of throttling going on. Yesterday, when I waited a couple of hours, it suddenly worked again, but only once.

Not sure if this might be related but the App seems to change the state after the permission request.

⚡️  To Native ->  PushNotifications requestPermission 115047851
APP INACTIVE
⚡️  [log] - onAppStateChange {"isActive":false}
⚡️  TO JS {"granted":true}
⚡️  To Native ->  PushNotifications register 115047860
APP ACTIVE
⚡️  [log] - onAppStateChange {"isActive":true}

EDIT: Now all of a sudden I received 4 simultaneous registration events. This appears to be an issue with APNS and not Capacitor. We should probably close this issue but inform about this in the documents section.

ryaa commented 3 years ago

This appears to be an issue with APNS and not Capacitor. We should probably close this issue but inform about this in the documents section.

Based on my experience, there have been always much more problems with push notification development/testing on iOS devices compared to Android. It usually works out of the box for the latter and there are often problems with the former :(

ionitron-bot[bot] commented 1 year ago

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.