capacitor-community / capacitor-community/fcm

FCM.subscribeTo not working on iOS

Open
#147 9 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
272
Forks
96
PR merge metrics
No merged PRs in 30d

Description

**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);
}
}
},
);
}

}

```

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the issue in a new Ionic 6/7 project with Capacitor 5 on iOS, using the inline AppComponent example and its FCM.subscribeTo calls. Compare the iOS behavior with Android and trace the plugin entry point used by subscribeTo; done means topic subscription succeeds on iOS as well as Android and the reproduction steps no longer fail.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, firebase, ios, typescript
Domain
mobile
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.