FCM Ionic Android notification not received when app is force closed

407 Views Asked by At

I am currently generating a data only FCM to my ionic application which will then create a local notification on the mobile application (android).

It works fine when the app is in foreground/background however when the app is closed the mobile application doesn't receive the notification.

Is there any solution to this issue? Preferably without having to send notification in the payload.

        $data = [
            "registration_ids" => [$user->device_token],
            "data" => [
                "targetPage" => "manualTicketPage",
                "ticketID" => $ticket_id,
                "ticketClassification" => $ticket_classification,
                "title" => "New Task",
                "body" => "Hi " . $user->name . ",\nYou have a new " . $ticket_description ." task to work on.",
                "badge" => 99,
                "content-available"=> "1",
                "force-start"=>"1"
            ],
            "priority"=>"high"
            ]

Ionic Code

 this.platform.ready().then(() => {
      this.rootPageParams = {'notificationReceived' : false };
      if(this.device.platform != "browser"){
        if(this.device.platform != "Android"){
          this.requestNotificationPermission();
        }
        this.fcm.getToken().then(token => {
          console.log("INSIDE GET TOKENNNNNNNN");
          this.fcm.getInitialPushPayload().then( data => {
            console.log('*********');
            console.log("RECEIVE FCM NOTIFICATION");
            console.log("DATA = "+ JSON.stringify(data));
            if(data) {
              if(data.wasTapped) {
                if(data['targetPage'] == 'manualTicketPage'){
                  console.log("SET ROOT PAGE PARAMS");
                  this.rootPageParams = {'notificationWasTapped' : true,'targetPage' : 'manualTicketPage' };
                }
              }
            }
          })
        });
        this.fcm.onNotification().subscribe(async notificationData => {
          console.log("NOTIFICATION RECEIVED ");
          console.log("DATA = "+ JSON.stringify(notificationData));
          if (notificationData.wasTapped) {
            console.log("NOTIFICATION WAS TAPPED");
            this.storage.get('url').then((url) => {
              this.storage.get('id').then(data => {
                let headers = new HttpHeaders();
                headers.append('Content-Type', 'application/json');
                headers.append('Authorization', 'my-auth-token');
                let params = new HttpParams().set("user_id", data);
                this.http.get(url + "/api/checkUserStatus", {headers: headers, params: params}).subscribe(data => {
                  if (data['status'] == "Active") {
                    if(notificationData['targetPage'] == 'manualTicketPage'){
                      console.log("SET ROOT PAGE PARAMS");
                      // this.rootPageParams = {'targetPage' : 'manualTicketPage' };
                      this.nav.push(TicketListPage);
                    }
                    else{
                      this.nav.push(ShowAuditPage);
                    }
                  
                  }
                })
              })
            })
          } else {
            console.log('Received in foreground');
          }
        });
      }
0

There are 0 best solutions below