Not can get FCM token in a ionic capacitor ios app

Hi, my ionic app was working on Android correctly and the FCM token can be obtained. But when I wanted to get the fcm token on ios it didn’t work, it didn’t generate it. Thanks to anyone who can help me with this problem.

this is my code


import { HttpClient, HttpHeaders } from ‘@angular/common/http’;
import { Injectable } from ‘@angular/core’;
import { Router } from ‘@angular/router’;
//import { FCM, FCMPlugin } from ‘@capacitor-community/fcm’;
import {
PushNotifications,
PushNotificationSchema,
PushNotificationToken,
ActionPerformed,
Token,
} from ‘@capacitor/push-notifications’;
import { Observable } from ‘rxjs’;
import { environment } from ‘src/environments/environment’;
import { FCM } from ‘src/models/fcm.model’;
import { FcmResponse } from ‘src/models/fcm/fcm.model’;

@Injectable({
providedIn: ‘root’,
})
export class FcmService {
constructor(private http: HttpClient, private router: Router) {
//this.initConfig();
this.initConfig2();
}

getToken(): string {
return JSON.parse(localStorage.getItem(‘session-data’)).access_token;
}

initConfig2() {
PushNotifications.requestPermissions().then((result) => {
if (result.receive === ‘granted’) {
console.log(result);
// Register with Apple / Google to receive push via APNS/FCM
PushNotifications.register();
} else {
// Show some error
}
});

PushNotifications.addListener('registration', (token: any) => {
  localStorage.setItem('token_fcm', token.value);
  let tokenUser = JSON.parse(localStorage.getItem('session-data'));
  let idUser = JSON.parse(localStorage.getItem('usuario'));
  this.postFCM(idUser.id, token.value, tokenUser.access_token).subscribe(
    (result) => {
      console.log(result);
    }
  );
  console.log('Push registration success, token: ' + JSON.stringify(token));
});

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

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

PushNotifications.addListener(
  'pushNotificationActionPerformed',
  (notification: ActionPerformed) => {
    if (this.getToken()) {
      this.router.navigate(['notification-list'], { replaceUrl: true });
    }
    //alert('Push action performed: ' + JSON.stringify(notification));
  }
);

}

postFCM(id, token_fcm, auth): Observable {
const authToken = this.getToken();
const headers = new HttpHeaders({
‘Content-Type’: ‘application/json’,
Authorization: Bearer ${auth},
});
console.log(‘esta es la funcion del token fcm’);
console.log(id);
console.log(token_fcm);
return this.http.post(
${environment.URL}user-update-fcm-token,
{
id: id,
token_fcm: token_fcm,
},
{
headers,
}
);
}
}


And this is my package.json

{
“name”: “project”,
“version”: “0.0.1”,
“author”: “CW”,
“homepage”: “https://ionicframework.com/”,
“scripts”: {
“ng”: “ng”,
“start”: “ng serve”,
“build”: “ng build”,
“test”: “ng test”,
“lint”: “ng lint”,
“e2e”: “ng e2e”
},
“private”: true,
“dependencies”: {
@angular/animations”: “^13.3.8”,
@angular/cdk”: “^13.3.7”,
@angular/common”: “~13.2.2”,
@angular/core”: “~13.2.2”,
@angular/forms”: “~13.2.2”,
@angular/platform-browser”: “~13.2.2”,
@angular/platform-browser-dynamic”: “~13.2.2”,
@angular/router”: “~13.2.2”,
@awesome-cordova-plugins/calendar”: “^5.43.0”,
@awesome-cordova-plugins/camera”: “^5.43.0”,
@awesome-cordova-plugins/chooser”: “^5.43.0”,
@awesome-cordova-plugins/core”: “^5.43.0”,
@awesome-cordova-plugins/document-viewer”: “^5.44.0”,
@awesome-cordova-plugins/file”: “^5.43.0”,
@awesome-cordova-plugins/file-opener”: “^5.44.0”,
@awesome-cordova-plugins/file-path”: “^5.43.0”,
@awesome-cordova-plugins/file-transfer”: “^5.43.0”,
@capacitor-community/fcm”: “^2.0.2”,
@capacitor/android”: “3.4.3”,
@capacitor/app”: “1.1.1”,
@capacitor/browser”: “^1.0.7”,
@capacitor/camera”: “^1.3.1”,
@capacitor/core”: “4.6.1”,
@capacitor/filesystem”: “^1.1.0”,
@capacitor/haptics”: “1.1.4”,
@capacitor/ios”: “^4.7.3”,
@capacitor/keyboard”: “1.2.2”,
@capacitor/push-notifications”: “^4.0.0”,
@capacitor/status-bar”: “1.0.8”,
@fullcalendar/angular”: “^5.11.0”,
@fullcalendar/daygrid”: “^5.11.0”,
@fullcalendar/interaction”: “^5.11.0”,
@ionic-native/core”: “^5.36.0”,
@ionic-native/file-chooser”: “^4.20.0”,
@ionic-native/file-opener”: “^5.36.0”,
@ionic-native/printer”: “^5.36.0”,
@ionic/angular”: “^6.0.0”,
@ionic/pwa-elements”: “^3.1.1”,
@swimlane/ngx-charts”: “^20.1.0”,
@swimlane/ngx-graph”: “^8.0.0”,
“chart.js”: “^3.7.1”,
“com-badrit-base64”: “^0.2.0”,
“cordova-plugin-calendar”: “^5.1.6”,
“cordova-plugin-camera”: “^6.0.0”,
“cordova-plugin-chooser”: “^1.3.2”,
“cordova-plugin-document-viewer”: “^1.0.0”,
“cordova-plugin-file”: “^6.0.2”,
“cordova-plugin-file-transfer”: “^1.7.1”,
“cordova-plugin-filepath”: “^1.6.0”,
“cordova-plugin-simple-file-chooser”: “^3.0.2”,
“cordova-plugin-whitelist”: “^1.3.5”,
“ionic2-calendar”: “^0.6.9”,
“jetifier”: “^2.0.0”,
“moment”: “^2.29.3”,
“ngx-animations”: “^5.0.4”,
“ngx-file-drop”: “^13.0.0”,
“rxjs”: “~6.6.0”,
“tslib”: “^2.2.0”,
“zone.js”: “~0.11.4”
},
“devDependencies”: {
@angular-devkit/build-angular”: “~13.2.3”,
@angular-eslint/builder”: “~13.0.1”,
@angular-eslint/eslint-plugin”: “~13.0.1”,
@angular-eslint/eslint-plugin-template”: “~13.0.1”,
@angular-eslint/template-parser”: “~13.0.1”,
@angular/cli”: “~13.2.3”,
@angular/compiler”: “~13.2.2”,
@angular/compiler-cli”: “~13.2.2”,
@angular/language-service”: “~13.2.2”,
@capacitor/cli”: “4.6.1”,
@ionic/angular-toolkit”: “^6.0.0”,
@types/jasmine”: “~3.6.0”,
@types/jasminewd2”: “~2.0.3”,
@types/node”: “^12.11.1”,
@typescript-eslint/eslint-plugin”: “5.3.0”,
@typescript-eslint/parser”: “5.3.0”,
“eslint”: “^7.6.0”,
“eslint-plugin-import”: “2.22.1”,
“eslint-plugin-jsdoc”: “30.7.6”,
“eslint-plugin-prefer-arrow”: “1.2.2”,
“jasmine-core”: “~3.8.0”,
“jasmine-spec-reporter”: “~5.0.0”,
“karma”: “~6.3.2”,
“karma-chrome-launcher”: “~3.1.0”,
“karma-coverage”: “~2.0.3”,
“karma-coverage-istanbul-reporter”: “~3.0.2”,
“karma-jasmine”: “~4.0.0”,
“karma-jasmine-html-reporter”: “^1.5.0”,
“protractor”: “~7.0.0”,
“ts-node”: “~8.3.0”,
“typescript”: “~4.4.4”
},
“description”: “An Ionic project”
}

Please use proper Markdown code blocks so we can more easily read your code :upside_down_face:

Most likely you need to replace the APN token with the FCM token in the AppDelegate.swift file.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    FirebaseApp.configure()

    return true
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    Messaging.messaging().apnsToken = deviceToken
    Messaging.messaging().token(completion: { (token, error) in
        if let error = error {
            NotificationCenter.default.post(name: .capacitorDidFailToRegisterForRemoteNotifications, object: error)
        } else if let token = token {
            NotificationCenter.default.post(name: .capacitorDidRegisterForRemoteNotifications, object: token)
        }
    })
}
1 Like