【问题标题】:Google Cloud Message API Notification Data doesn't appear - PHPGoogle Cloud Message API 通知数据未出现 - PHP
【发布时间】:2016-03-15 14:31:48
【问题描述】:

我使用 PHP 向我的客户发送通知。这是我的 PHP 代码

<?php

define( 'API_ACCESS_KEY', 'AIzaSyAYq7KpB5J7x3p3zYyna...' );
$registrationId = array( $_GET['id'] );

$msg = array
(
    'message'   => 'here is a message. message',
    'title'     => 'This is a title. title',
    'subtitle'  => 'This is a subtitle. subtitle'
);
$fields = array
(
    'registration_ids'  => $registrationId,
    'notification'          => $msg
);

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

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/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;

当我使用我的注册 ID 执行脚本时,会有这样的通知,Notification

标题是 推送消息 身体是 讯息

如您所见,通知标题和消息正文不等于地雷。我该如何解决这个问题? 谢谢。

【问题讨论】:

    标签: php curl push-notification google-cloud-messaging


    【解决方案1】:

    $msg 应该是一个 JSON 对象,如 Messaging Concepts and Options 中所示

    { “到”:“bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1 ...”, “数据” : { “尼克”:“马里奥”, "body" : "绝配!", “房间”:“葡萄牙VS丹麦” }, }

    有关下游消息传递的更详细讨论,您可以参考它的documentation page,它讨论了发送消息的各个方面。

    【讨论】:

    • json 参数“to”不起作用。 “registration_ids”工作正常。我尝试用 json 编码消息,但仍然是同样的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多