【问题标题】:How to get push notification values in phonegap using FCM?如何使用 FCM 在 phonegap 中获取推送通知值?
【发布时间】:2016-12-14 07:32:43
【问题描述】:

我在 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>

【问题讨论】:

  • 当我点击通知时,我想在应用程序中获取标题和正文,但我没有定义。
  • 它返回未定义
  • 我还想说,当我使用这个测试器cordova-plugin-fcm.appspot.com 时,我会在点击时收到通知以及通知的值
  • 这就是我传递值的方式 /sendnotification.php?id=&title=&body=
  • 这是我收到 {"multicast_id":5557799655301185254,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0 :1481702163032202%96ef985796ef9857"}]}

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


【解决方案1】:

这对我有帮助

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

【讨论】:

    猜你喜欢
    • 2017-12-23
    • 1970-01-01
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多