Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: PushNotifications registration & registrationError listeners not fired #2247

Closed
1 of 4 tasks
Livijn opened this issue Dec 13, 2019 · 10 comments
Closed
1 of 4 tasks

Comments

@Livijn
Copy link

Livijn commented Dec 13, 2019

Bug Report

Capacitor Version

npx cap doctor output:

Installed Dependencies:
@capacitor/cli 1.4.0
@capacitor/core 1.4.0
@capacitor/ios 1.4.0
@capacitor/android 1.4.0
[success] Android looking great! 👌
Found 0 Capacitor plugins for ios:
success] iOS looking great! 👌

Affected Platform(s)

  • iOS
  • Android
  • Electron
  • Web

Current Behavior

First I remove the app and on start up, the "permission alert" appears asking for notification permissions. Whether I accept or decline, no listener is fired on my real iOS device (iPhone Xs 13.1.3). However, when testing using the iOS Simulator, the registrationError listener is indeed fired.

When I click on the "permission alert", this is printed in the console:

APP ACTIVE
⚡️ TO JS {"isActive":true}
⚡️ [log] - onAppStateChange {"isActive":true}

So for some reason, this listener is fired instead.

Expected Behavior

PushNotifications listeners should fire after accepting/declining permissions.

Sample Code or Sample Application Repo

I am using Ionic + Vue.js.

    PushNotifications.register();

    PushNotifications.addListener('registration', token => {
      console.log('Push registration success, token: ' + token.value);
    });

    PushNotifications.addListener('registrationError', error => {
      console.log('Error on registration: ' + JSON.stringify(error));
    });

    PushNotifications.addListener('pushNotificationReceived', notification => {
      console.log('Push received: ' + JSON.stringify(notification));
    });

    PushNotifications.addListener('pushNotificationActionPerformed', notification => {
      console.log('Push action performed: ' + JSON.stringify(notification));
    });

Other Technical Details

npm --version output: 6.13.0

node --version output: v12.9.1

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

@Livijn
Copy link
Author

Livijn commented Dec 13, 2019

I just realized that it was this part of the guide that created the error. It seems to work if I skip the step below.

If you would like to recieve the firebase FCM token from iOS instead of the raw APNS token, you will need to also change your AppDelegate.didRegisterForRemoteNotificationsWithDeviceToken code to look like this:

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        Messaging.messaging().apnsToken = deviceToken
        InstanceID.instanceID().instanceID { (result, error) in
            if let error = error {
                NotificationCenter.default.post(name: Notification.Name(CAPNotifications.DidFailToRegisterForRemoteNotificationsWithError.name()), object: error)
            } else if let result = result {
                NotificationCenter.default.post(name: Notification.Name(CAPNotifications.DidRegisterForRemoteNotificationsWithDeviceToken.name()), object: result.token)
            }
        }
    }

However, I am interested in the FCM token rather than the APNS token.

@bryplano
Copy link
Contributor

I would wager it is related to this PR (the doc update came out in 1.4.0 and there was a small code change or two around the push code on iOS): #2078

@Livijn
Copy link
Author

Livijn commented Dec 13, 2019

@bryplano Yes, you are correct. I added print() to debug what was going on in PushNotifications.swift and suddenly it worked. Does Xcode cache certain files on build? Anyway, it works now. Thanks.

@Livijn Livijn closed this as completed Dec 13, 2019
@Livijn Livijn reopened this Dec 13, 2019
@Livijn
Copy link
Author

Livijn commented Dec 13, 2019

As soon as I run PushNotifications.register(); the app registers the FCM Token. Shouldn't this only happen when the user presses "Allow"? How do I know if the user allowed/declined the permission?

@Livijn
Copy link
Author

Livijn commented Dec 13, 2019

Shouldn't this line of code fire an event so we know what the user choice was?

https://github.com/ionic-team/capacitor/blob/master/ios/Capacitor/Capacitor/CAPUNUserNotificationCenterDelegate.swift#L28

@lfa-vasconcellos
Copy link

@bryplano Yes, you are correct. I added print() to debug what was going on in PushNotifications.swift and suddenly it worked. Does Xcode cache certain files on build? Anyway, it works now. Thanks.

Had this exact problem, even after reinstalling packages. Writing to the file solved it. Thanks!

@pixelhomie
Copy link

@bryplano Yes, you are correct. I added print() to debug what was going on in PushNotifications.swift and suddenly it worked. Does Xcode cache certain files on build? Anyway, it works now. Thanks.

Had this exact problem, even after reinstalling packages. Writing to the file solved it. Thanks!

Same here. Added print() in PushNotifications.swift and it works

@santekotturi
Copy link

Can someone post what their PushNotifications.swift file looks like with the print statements? Thanks!

@Livijn
Copy link
Author

Livijn commented Mar 15, 2021

This is still happening for me sometimes. Product -> Clean Build Folder fixed it this time.

@ionitron-bot
Copy link

ionitron-bot bot commented Nov 11, 2022

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.

@ionitron-bot ionitron-bot bot locked and limited conversation to collaborators Nov 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
6 participants