Search code examples
iosfirebaseionic-frameworkfirebase-cloud-messagingcapacitor

FCM Push Notification are not working in IOS using Ionic Capacitor


Using CAPACITOR and IONIC Everything is Perfectly Works on Android but in IOS i am having Following Error:

APNS device token not set before retrieving FCM Token for Sender ID 'mySenderId'. Notifications to this FCM Token will not be delivered over APNS.Be sure to re-retrieve the FCM token once the APNS device token is set.

💊   Capacitor Doctor  💊 

Latest Dependencies:

  @capacitor/cli: 3.2.2
  @capacitor/core: 3.2.2
  @capacitor/android: 3.2.2
  @capacitor/ios: 3.2.2

Installed Dependencies:

  @capacitor/cli: 3.2.2
  @capacitor/core: 3.2.2
  @capacitor/android: 3.2.2
  @capacitor/ios: 3.2.2

I can see FCM Token in my Xcode Logs but notification are not working.

References:

My Issue

Simmilar Issue


Solution

  • The solution is to register for APNS token before FCM token.

     PushNotifications.requestPermissions().then((permission) => {
        if (permission.receive == "granted") {
          // Register with Apple / Google to receive push via APNS/FCM
          if(Capacitor.getPlatform() == 'ios'){
            PushNotifications.register().then((res)=>{
              console.log('From Regisiter Promise', res)
            })
            PushNotifications.addListener('registration', (token: Token)=>{            
              FCM.getToken().then((result) => {
                this.remoteToken = result.token;
              }).catch((err) => console.log('i am Error' , err));
            })
          }else{
            FCM.getToken().then((result) => {
              this.remoteToken = result.token;
            }).catch((err) => console.log('i am Error' , err));
          }
        } else {
          // No permission for push granted
          alert('No Permission for Notifications!')
        }
      });
    

    For Latest Version of Plugin:

    PushNotifications.requestPermissions().then((permission) => {
            if (permission.receive == "granted") {
    
              PushNotifications.addListener('registration', async ({ value }) => {
                let token = value // Push token for Android
              
                // Get FCM token instead the APN one returned by Capacitor
                if (Capacitor.getPlatform() === 'ios') {
                  const { token: fcm_token } = await FCM.getToken()
                  token = fcm_token
                }
                // Work with FCM_TOKEN
                
                console.log(token);
              })
            } else {
              // No permission for push granted
              alert('No Permission for Notifications!')
            }
          });