首页 > 解决方案 > 在后台收到的通知不会触发 pushObject.on('notification')。当应用程序也被关闭时

问题描述

我使用 php 向 ionic 应用程序发送通知,pushObject.on('notification') 仅在应用程序运行时执行,但当应用程序处于后台或关闭时,单击通知仅打开应用程序和我的 pushObject.on( '通知')。不叫。

我已经制作了很多有效负载版本,如解决方案 phonegap-plugin-push on("notification") event is not fire when app is in background

我的 php 代码是

<?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 );

我的离子代码是

 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');

          }

      });

现在应用程序正在运行并且在 if (notification.additionalData.foreground) 前台执行,如果应用程序在后台或已关闭,请单击通知仅打开应用程序。

标签: ionic-frameworkphonegap-pushplugin

解决方案


推荐阅读