qasmaoui
qasmaoui

Reputation: 103

notification received on background dose not trigger pushObject.on('notification'). when app is clised too

i use php to send notification to ionic app , the pushObject.on('notification') is executed only when app is runing but when app is on background or closed , clicking on the notification dose only open the app and my pushObject.on('notification'). is not called.

i have already made a lot of payload version as searching as in the solution phonegap-plugin-push on("notification") event is not firing when app is in background

my php code is

<?php
#API access key from Google API's Console
define( 'API_ACCESS_KEY', 'xxxxxxx' );
#prep the bundle
$msg = array
(
    "notId"=> "test",
    "body"=> "test",
    "title"=> "Cabinet sahli : قرار جديد",
    "vibrate"=> 1,
    "sound"=>"default",
    "case_number"=> "test",
    "case_title"=>"test",
    "case_id"=> 13,
    "content-available"=> 1,
    "priority"=> "high",
    "additionalData"=>  [
        "surveyID" =>  "ewtawgreg-gragrag-rgarhthgbad",
    ]

);
$fields = array
(

//'registration_ids' => array('d7pT_z1pwYo:APA91bFK2KilUzZon1g4Yu5-bZgaqxeepcP0Owzn2a6QgGuK4HUsI-hQhRbAYANIYfl3xdDYq-k6lAsnNSFyRB4qgEH1wJqDRw6V4s3kKpg_OZHT4UZATZDo6hceZYiwzBS6eSXUfjYh'),
'registration_ids' => array('d1WrqmdONL0:APA91bFRlokdsFFOXygqnRaSuWwD7QnJIhFGoZj1wPGuWzSxFxkaEca4FPDsPxpDp1FmpZSQ6lCoTsepH2PP9KbJ7HbEGQBhT0ofx8gMbr9P-JPL8eZlff4rkY7uS3CWXAzmg0KkbChK'),
'data'  => $msg,
);


$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);

#Send Reponse To FireBase Server
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
echo $result;
curl_close( $ch );

and my ionic code is

 pushObject.on('notification').subscribe((notification: any) => {

          let json = JSON.parse(JSON.stringify(notification.additionalData));

          console.log(json);

          this.storage.set('notifs',json);
         console.log( this.storage.get('notifs'));

          if (notification.additionalData.foreground) {
              console.log('i got fired foreground');

              let youralert = this.alertCtrl.create({
                  title: 'Nouvelle audience',
                  message: notification.message,
                  buttons: [
                      {
                          text: "تخطي التنبيه",

                      },
                      {
                          text: "التفاصيل",
                          handler: () => {
                              this.nav.push(CasedetailPage,{id:json.case_id,titre:json.case_title,numero:json.case_number});
                          }

                      }]
              });
              youralert.present();

          }

          else if(notification.additionalData.background) {
              this.nav.push(CasedetailPage,{id:json.case_id,titre:json.case_title,numero:json.case_number});

              console.log('back');

          }

      });

right now of the app is running and forground the if (notification.additionalData.foreground) is excuted and if the app is on background or is closed clicking on the notification only open the app.

Upvotes: 1

Views: 189

Answers (0)

Related Questions