Getting push notifications in Ionic Angular App foreground

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 4.

After my last question, I’ve solved my problem and now push notifications working as expected. But, I’ve noticed that if user logged out then log in again, user is getting push notifications even mobile app open and foreground. How did this happen, I don’t know. What am I missing here?

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) => {
        let fcm: FCMTokenModel = JSON.parse(localStorage.getItem("FCMTOKEN"));

        if (fcm == null || fcm == undefined) {
            fcm = { fcmToken: token.value };
            
            // Some methods for saving token to database
            localStorage.setItem("FCMTOKEN", JSON.stringify(fcm));
            
        } else {
            if (fcm.fcmToken != token.value) {
                fcm.fcmToken = token.value;

                // Some methods for saving token to database
                
                localStorage.setItem("FCMTOKEN", JSON.stringify(fcm));
            }
        }
    });

    PushNotifications.register().then(() => {
        if (this.platform.is('android')) {
            PushNotifications.createChannel({id: "test", name: "test", importance: 5});
        }
    });
}

logout() {
    localStorage.removeItem("FCMTOKEN");
    PushNotifications.removeAllListeners();
    PushNotifications.removeAllDeliveredNotifications();

    //Some method for removing FCM Token from database

    this.navCtrl.navigateRoot(['/login']);
}

You would need to call unregister. But this method wasn’t added until Capacitor 5. You should upgrade to the latest though which is Capacitor 6.

Is this the reason why push notifications getting foreground?

Is this the reason why push notifications getting foreground?

So what is your end goal? You don’t want notifications when the app is in the foreground any time? Or only if the user logs out and back in? Please explain a little more what you are trying to do.

We can put logout/login part aside. I want notifications work as it should. I dont have any payload, so I am not listening pushNotificationReceived. So, I all want is that get notifications when app closed/background and not get when the app is in the foreground anytime.

I think if you set presentationOptions in your Capacitor config to an empty array you shouldn’t see anything when the app is in the foreground.

You, are, awesome! Thank you @twestrick!

1 Like