Phonegap Firebase 推送通知在应用程序打开或应用程序处于后台时不会触发事件侦听器

Phonegap Firebase Push Notification not firing event listener when app opens, or when app is in background

我正在使用 Phonegap 和 Firebase (fcm) 接收推送通知。 我正在使用 Android 8 进行测试。

如果应用程序 运行 在前台,我希望我的设备收到推送通知(作为应用程序中的弹出消息)。 如果应用程序关闭或在后台,我希望消息显示为通知,当我单击通知时,它会在前台打开应用程序并调用通知事件函数。

这是到目前为止发生的事情:

有很多关于此问题的帮助请求,我已经处理了其中的许多请求,但其中 none 为我的应用程序提供了可行的解决方案(大多数是在 2016 年到 2018 年之间,而不是 2019 年) .

这主要需要为 Android 工作。因此,我尝试删除通知有效负载并仅使用数据有效负载。然而这并不能解决问题。

这是我当前的有效载荷:

$notification = array
(
'body'   => 'test',
'title'     => 'test',
"icon" =>  'notification_icon',
"content_available" => true,
);

$data = array
(
"title" => "test",
"body" => "test",
"icon" => "notification_icon",
"content_available" => true,
);

$fields = array
(
'registration_ids'  => $registrationIds,
'notification'      => $notification,
'data'          => $data,
"priority" => "high",
"content_available" => true 
);

据我了解,有 3 种发送通知的方式:

1) 为"notification"。如果应用程序当前为 运行,则此功能在前台工作;如果应用程序已关闭或最小化,则在顶部栏的后台工作(并且应用程序的图标上方有一个小徽章,表示一条消息)但是当在后台,当消息被点击时,应用程序加载但通知功能未被调用。

2) 如"data"。这似乎只能在前台工作,没有顶部栏消息,应用程序的图标上面也没有小徽章。如果应用程序在后台,则根本没有推送通知。

3) 同时 "notificaton" 和 "data"。在后台时,应用程序图标上方有一个徽章。单击顶部栏消息时,将加载应用程序,但不会调用通知功能。

此外,即使应用程序在后台,如果它收到推送通知并将应用程序置于前台,也不会显示通知消息。仅当收到通知时应用程序已在前台时才有效。

这是我的事件监听器:

var app = {
initialize: function() {
    this.bindEvents();
},
bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady: function() {
  console.log('Received Device Ready Event');
  app.setupPush();
},
setupPush: function() {
    var push = PushNotification.init({
      android: {
        senderID: 'XXXXXXXXXX',  
        iconColor: "#ccc",
        alert: true,
        badge: true,
        icon: 'notification_icon',
        sound: true,
        vibrate: true,
      },
      browser: {},
      ios: {
        sound: true,
        vibration: true,
        badge: true
      },
      windows: {}
    });
    console.log('after init');

    push.on('registration', function(data) {
         //[edit - script not displayed for this example, but it registers a token to enable push notifications to the device. tested and working]
    });

    push.on('error', function(e) {
      console.log('push error = ' + e.message);
    });

    push.on('notification', function(data) {
       window.alert(data.additionalData);              //Test line

      if(data.additionalData.foreground){
        window.alert(data.title+'\n'+data.message);    //Test line
      }
      if(data.additionalData.coldstart){
        window.alert("coldstart");                     //Test line
      }
      showPopup(data,"info");

    });


  }
};

//Using Sweet Alert to display messages

function showPopup(data,type){
   Swal.fire({
     type: type,
     title: data.title,
     text: data.message,
     showConfirmButton: true,
     imageUrl: data.image,
     imageWidth: 400,
     imageHeight: 200,
    imageAlt: data.title,
    animation: true,
  });
}

是否有人对上述问题有适用于 Android 7 和 8 的答案?

您的通知发送逻辑是可靠的,但您的顾虑也是有道理的。如果您想阅读有关该主题的更多信息,google notification vs data notifications,有一些很好的文章对此进行了解释。

推送通知有一个特殊的本机打开接收器,您的 WebView 必须注册这些事件。但是,如果您查看 source,则不会处理通知打开事件。除了分叉存储库,您无能为力。

此外,当应用程序被终止时,.on('notification') 事件不会被您的 WebView 接收到,因为……好吧,您的应用程序被终止了。您的应用程序在被杀死时不会收到通知,Android OS 收到通知并决定如何处理它。

关于您在 Android 8 上注意到此问题的原因,Androids 的最新版本更积极地在后台终止应用程序以节省电量:Doze and Adaptive battery。较新的 android 应用程序将很快被淘汰。

如果可能的话,我建议您切换到 cordova-plugin-firebase,一个更 actively-maintained 的插件。或者,学习一些 android 并编写自己的插件。

我的最终答案是,要让推送消息同时用作 Android 设备(包括 Android 8 和 9)的通知消息和应用内消息,请执行以下操作:

从 config.xml 添加此 to/modify:

<preference name="phonegap-version" value="cli-9.0.0" />  
<platform name="android">
    <resource-file src="google-services.json" target="app/google-services.json" />
</platform>
<platform name="ios">
    <resource-file src="GoogleService-Info.plist" />    
</platform>
<plugin name="phonegap-plugin-push" spec="2.1.3">
    <variable name="SENDER_ID" value="xxxxxxxxxxxxx" />
</plugin>    

另外,在发送数据包时,发送 "data" 参数和 "notification" 参数,并包含 "content_available" 标志,例如...

  $notification = [
                'body'   => $message,
                'title'     => $titlenotification,                    
                "icon" =>  'notification_icon',
                "content_available" => "1", 
  ];


 $data = [
             "title"             =>$titlenotification,  
             "message"           => $message,
             "content_available" => "1",
 ];

 $fields =  [
            'registration_ids'   => $registrationtokens,    
             'notification'      => $notification,        
            'data'               => $data,                         
            "priority"           => "high",
            "content_available"  => true,
 ];

 //send field data like curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );

...最后,一定要将您的 google-services.json 添加到根文件夹,还有 www 文件夹。否则,当 Phonegap Build 正在处理它时,您将收到错误 "A plugin requires google-services.json. The Google Services Plugin cannot function without it"。

如果应用程序关闭或在后台,这将显示一条通知消息。当您单击通知时,它将打开应用程序并再次显示消息。如果你的应用程序在后台,当你把它带到前台时也会显示消息。如果您的应用已关闭并且您没有点击通知消息,那么您将永远不会在您的应用上看到该消息。