FCM.subscribeTo not working on iOS

Describe the bug
The app is not subscribing to any topic on iOS devices. There is no issue with Android devices.

To Reproduce
Steps to reproduce the behavior:

  1. Create a new ionic project (6/7)
  2. Add Capacitor 5 (iOS)
  3. Complete the Ionic Post Notification setup steps
  4. Subscribe to a topic using capacitor-community/fcm

Expected behavior
The app should subscribe to the topics on iOS just like Android.

Code

import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { ActionPerformed, PushNotificationSchema, PushNotifications, Token } from '@capacitor/push-notifications';
import { Preferences } from '@capacitor/preferences';
import { FCM } from '@capacitor-community/fcm';

interface SubscriptionPreferences {
 [key: string]: boolean;
}


@Component({
 selector: 'app-root',
 templateUrl: 'app.component.html',
 styleUrls: ['app.component.scss'],
})
export class AppComponent {

 subscriptionPreferences: SubscriptionPreferences = {};
 topics = ['Announcements', 'Calendar', 'Upcoming-Events', 'Block-Rotation', 'Clubs', 'Sports', 'Cafeteria', 'Contests', 'University-Advising', 'New-Student-Help', 'Parent-Info', 'Staff-Directory', 'ios'];

 constructor(private router: Router) {}

 ngOnInit() {
   console.log('Initializing HomePage');

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


   Preferences.get({ key: 'firstTimeOpen' }).then(result => {
     const isFirstTimeOpen = result.value;

     if (isFirstTimeOpen === null || isFirstTimeOpen === 'true') {
       this.topics.forEach(topic => {
         FCM.subscribeTo({ topic: topic })
           .then(() => {
             console.log(`Subscribed to topic ${topic}`);
             this.subscriptionPreferences[topic] = true;
             Preferences.set({ key: 'firstTimeOpen', value: 'true' });
           })
           .catch((error) => {
             Preferences.set({ key: 'firstTimeOpen', value: 'false' });
           });
       });

       this.subscriptionPreferences = this.topics.reduce((acc: SubscriptionPreferences, topic: string) => {
         acc[topic] = true;
         return acc;
       }, {});

       Preferences.set({ key: 'subscriptionPreferences', value: JSON.stringify(this.subscriptionPreferences) });
     } 
      Preferences.get({ key: 'subscriptionPreferences' }).then(result => {
        const preferencesValue = result.value;
        this.subscriptionPreferences = preferencesValue ? JSON.parse(preferencesValue) : {};
       });
       console.log(this.subscriptionPreferences)
     
   });

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

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

   PushNotifications.addListener(
     'pushNotificationReceived',
     (notification: PushNotificationSchema) => {
       alert('Notification received: ' + notification.title + '\n' + notification.body);
     },
   );

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

       if (notification.notification) {
         const notificationData = notification.notification.data;
         if (notificationData && notificationData.page) {
           this.router.navigateByUrl(notificationData.page);
         }
       }
     },
   );
 }

}


Over in your SO post, you are replacing the APN token with the Firebase token in your AppDelegate.swift. I wonder if that is causing issues with the FCM plugin. I know the guide here has you do that, but I think that assumes you are not using the FCM plugin. The FCM plugin replaces the APN token itself here.

Try matching AppDelegate to what is here - Push Notifications Capacitor Plugin API | Capacitor Documentation.

@twestrick

These are the errors I am getting. I was getting these same errors before making the changes you recommended to the AppDelegate.swift and I am still getting these errors after making the changes you suggested.

When I remove the code for the FCM plugin, and simply follow the Capacitor Push Notification tutorial, I do not get these errors.
I am not quite sure why this is the case especially for the aps-environment error.

If you have any further suggestions, I would greatly appreciate it as this is the last issue preventing me from publishing the app for our July 7 deadline.

Jul  5 16:48:51 App(GoogleUtilities)[12739] <Notice>: 10.11.0 - [FirebaseMessaging][I-FCM012002] Error in application:didFailToRegisterForRemoteNotificationsWithError: no valid \M-b\M^@\M^\aps-environment\M-b\M^@\M^] entitlement string found for application


Jul  5 16:56:07 iPhone App(GoogleUtilities)[3240] <Notice>: 10.11.0 - [FirebaseMessaging][I-FCM002010] The subscription operation failed due to an error getting the FCM token: Error Domain=com.google.fcm Code=505 "No APNS token specified before fetching FCM Token" UserInfo={NSLocalizedFailureReason=No APNS token specified before fetching FCM Token}.

Jul  5 16:56:07 iPhone App(WebKit)[3240] <Error>: kill() returned unexpected error 1

Jul  5 16:56:07 iPhone App(GoogleUtilities)[3240] <Notice>: 10.11.0 - [FirebaseMessaging][I-FCM002022] APNS device token not set before retrieving FCM Token for Sender ID '860157479095'.Be sure to re-retrieve the FCM token once the APNS device token is set.

Jul  5 16:56:07 iPhone App(GoogleUtilities)[3240] <Notice>: 10.11.0 - [FirebaseMessaging][I-FCM002022] Declining request for FCM Token since no APNS Token specified

Jul  5 16:56:07 iPhone App(GoogleUtilities)[3240] <Notice>: 10.11.0 - [FirebaseMessaging][I-FCM002022] APNS device token not set before retrieving FCM Token for Sender ID '860157479095'.Be sure to re-retrieve the FCM token once the APNS device token is set.

Jul  5 16:56:07 iPhone App(GoogleUtilities)[3240] <Notice>: 10.11.0 - [FirebaseMessaging][I-FCM002022] Declining request for FCM Token since no APNS Token specified