ionic-frameworkpush-notificationcapacitor

Ionic 6 - Can’t get background push notifications in Ionic Angular App


I have an Ionic Angular (v.6) Android and iOS apps and I want to send push notifications to them. I am using Capacitor Push Notifications library. My capacitor version is 3.

When my app is open, I can get notifications from pushNotificationReceived listener. But when my app is killed/background I can’t get any notifications to system tray. I’ve both tried sending push notifications from my API and FireBase console but no luck. What am I missing?

constructor(private platform: Platform) {
    if (this.platform.is('ios') || this.platform.is('android')) {
        let permissions = await PushNotifications.checkPermissions();

        if (permissions.receive == "granted") {
            await this.setupFirebase();
        } else {
            let permissions = await PushNotifications.requestPermissions();

            if (permissions.receive == "granted") {
                await this.setupFirebase();
            } 
        }
    }
}

private async setupFirebase() {
    await PushNotifications.addListener("registration", (token) => {
        this.h.Toast(this.toastController, token.value);
    });

    await PushNotifications.addListener("registrationError", (error) => {
        this.h.Toast(this.toastController, error.error);
    });

    await PushNotifications.addListener("pushNotificationReceived", (notification) => {
        this.h.Toast(this.toastController, JSON.stringify(notification));
    });

    await PushNotifications.addListener("pushNotificationActionPerformed", (notification) => {
        this.h.Toast(this.toastController, JSON.stringify(notification));
    });

    PushNotifications.register().then(() => {
        PushNotifications.createChannel({id: "test", name: "test", importance: 5});
    });
}

Solution

  • I've solved my problem by implementing these two lines:

    implementation "com.google.firebase:firebase-iid:21.1.0"
    implementation "com.google.firebase:firebase-messaging:22.0.0"