Ionic 6 - Getting push notifications in Ionic Angular App foreground

Discussão em 'Angular' iniciado por YSFKBDY, Setembro 11, 2024.

  1. YSFKBDY

    YSFKBDY Guest

    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.

    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?

    Push Notification Functions:

    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 Function:

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

    //Some method for removing FCM Token from database

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

    Continue reading...

Compartilhe esta Página