typescriptfirebaseionic-frameworkfirebase-cloud-messagingcordova-plugin-fcm

Save FCM Notification Data in Ionic App


I am able to send notifications to my app. If my app is running then it is able to save the Incoming Notifications data

If my app is closed and i click on the notification on the notifications tray it opens my app but doesnt save the notification data Any suggestions on how we can save the notifications data when the app is closed? below is the code

    platform.ready().then(() => {


  fcm.onNotification().subscribe( data => {
    this.con.notification_title = data.notification_title;
    this.con.notification_message = data.notification_message;
      if(localStorage.getItem('oldmsg'))
      {
        this.temparr = JSON.parse(localStorage.getItem('oldmsg')); 
        this.temparr.push({"notification_title":this.con.notification_title,"notification_message": this.con.notification_message}); 
        localStorage.setItem('oldmsg',JSON.stringify(this.temparr));
        this.presentToast("You Recieved A Message!");

      }
      else{

        this.temparr.push({"notification_title":this.con.notification_title,"notification_message": this.con.notification_message}); 
        localStorage.setItem('oldmsg',JSON.stringify(this.temparr));
        this.presentToast("You Recieved A Message!");

      }
    });

 statusBar.styleDefault();
  splashScreen.hide();

  this.storage.get('introShow').then((result) => {

    if(result){

      this.navCtrl.setRoot(LoginPage);
      this.storage.set('introShow', true);
    } else {

      this.navCtrl.setRoot(IntroPage);
      this.storage.set('introShow', true);
    }

    this.listenToLoginEvents();
  });


});

I have removed the data.wasTapped check to save the message no matter what.


Solution

  • Hopefully this helps someone

            this.Onesignal.startInit(this.oneSignalAppId, this.sender_id);
        this.Onesignal.inFocusDisplaying(this.Onesignal.OSInFocusDisplayOption.Notification);
        this.Onesignal.handleNotificationReceived().subscribe(// when its received
            data => {
              if (localStorage.getItem('notifications')) {
                this.temparr = JSON.parse(localStorage.getItem('notifications'));
                this.temparr.push({body: data.payload.body,
                title: data.payload.title,
                type: data.payload.additionalData.type});
                localStorage.setItem('notifications', JSON.stringify(this.temparr.reverse()));
    
                this.events.publish('user:noti_done', {
                  user: 'done',
                  time: new Date()
                });
    
              } else {
                this.temparr.push({body: data.payload.body,
                title: data.payload.title,
                type: data.payload.additionalData.type});
                localStorage.setItem('notifications', JSON.stringify(this.temparr));
    
                this.events.publish('user:noti_done', {
                  user: 'done',
                  time: new Date()
                });
              }
            });
        
        this.Onesignal.handleNotificationOpened().subscribe(data => {
          console.log('handleNotificationOpened');
          console.log(data);
          
        });
        this.Onesignal.endInit();
    }