-
Notifications
You must be signed in to change notification settings - Fork 92
Open
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:
- Create a new ionic project (6/7)
- Add Capacitor 5 (iOS)
- Complete the Ionic Post Notification setup steps
- 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);
}
}
},
);
}
}
Metadata
Metadata
Assignees
Labels
No labels