cordova - 如何使用 FCM 在 phonegap 中获取推送通知值?

标签 cordova firebase push-notification firebase-cloud-messaging phonegap-pushplugin

我在 PhoneGap 工作。我想获取从 FCM 获取的推送通知值。现在,我收到了通知,但我无法在我的应用程序中接收通知值。

PHP 端 FCM 代码:

    <?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'AIzaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
$registrationIds = array( $_GET['id'] );
// prep the bundle
$msg = array
(
    'body'  => $_GET['body'],
    'title'     => $_GET['title'],
    'vibrate'   => 1,
    'sound'     => 1,
);
var_dump($msg);
$fields = array
(
    'registration_ids'  => $registrationIds,
    'notification'          => $msg
);

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

$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 );
curl_close($ch);
echo $result;
?>

这是我接收通知的脚本

 <script type="text/javascript">
  function onLoad() {
  document.addEventListener("deviceready", Fire, false);
                       }

    function Fire() {



        FCMPlugin.getToken(
          function (token) {

               var name = document.getElementById("fireup");
                 name.value = token;
                 localStorage.setItem("key",token);
          },
          function (err) {
                   alert("Error: " + 'error retrieving token: ' + err);
          }
        );

        FCMPlugin.onNotification(function(data){
                if(data.wasTapped){
                    alert( JSON.stringify(data.title ) );
                }else{
                  alert( JSON.stringify(data.title ) );
                }
              },
              function(msg){
                  alert('onNotification callback successfully registered: ' + msg);
              },
              function(err){
                  alert('Error registering onNotification callback: ' + err);
              }
            );

    };


</script>

最佳答案

这对我有帮助

<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'AIzaSyCpN9xxxxxxxxxxxxxxxxxxxxxxx' );
$registrationIds = array( $_GET['id'] );
// prep the bundle
$msg = array
(
    'body'  => $_GET['body'],
    'title'     => $_GET['title'],
    'vibrate'   => 1,
    'sound'     => 1,
);

$fields = array
(
    'registration_ids'  => $registrationIds,
    'data'                  => $msg,
        'notification'      => $msg
);

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

$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 );
curl_close($ch);
//echo $result;
echo json_encode(array("data"=>$msg));
?>

关于cordova - 如何使用 FCM 在 phonegap 中获取推送通知值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41137079/

相关文章:

blackberry - 如何在 blackberry phonegap 中设置方向?

jquery - PhoneGap Ajax 请求返回 null

android - CSS 过渡或动画在 phonegap 中不起作用

firebase - 在 webpack vue 项目中添加 firebase 配置变量的位置

firebase - 如何过滤firebase cloud firestore上的id列表?

c# - 推送通知无法使用 asp.net 到达 ios 设备?

cordova - 适用于 Phonegap/Cordova 应用的 Firebase 分析

python - 为什么 firebase_admin 在运行多进程时无法解析

ruby-on-rails - Rails 中的 Gmail 推送通知

java - 带有tomcat服务器的jsp页面中的实时通知