【问题标题】:MismatchSenderID error when trying to send a push notification from PHP尝试从 PHP 发送推送通知时发生 MismatchSenderID 错误
【发布时间】:2016-12-12 02:05:30
【问题描述】:

我正在尝试在我的 Cordova 应用程序中实现推送通知,并且一直在学习一些教程。
我正在使用这段代码将通知发送到应用程序,但由于某种原因我收到了 MismatchSenderID 错误。 我多次检查并重新生成了访问密钥,GCM ID 是我从应用程序中获得的,所以它应该可以工作。
为什么会这样?

<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'xx' );
$registrationIds = 'xx';
// prep the bundle
$msg = array
(
    'message'   => 'here is a message. message',
    'title'     => 'This is a title. title',
    'subtitle'  => 'This is a subtitle. subtitle',
    'tickerText'    => 'Ticker text here...Ticker text here...Ticker text here',
    'vibrate'   => 1,
    'sound'     => 1,
    'largeIcon' => 'large_icon',
    'smallIcon' => 'small_icon'
);
$fields = array
(
    'registration_ids'  => array($registrationIds),
    'data'          => $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;
?>

--编辑:更多信息

我已经尝试了下面链接的线程中给出的解决方案,但是它们似乎不起作用,或者我做错了,因为 Google Console 的 url 格式和布局从那时起就变成了这样的东西:http://imgur.com/a/yosLF 我确实尝试重新安装 phonegap 以获得不同的注册 ID,并尝试了这两个 ID,包括 URL 中的一个和下面的一个。我可以通过终端成功发送推送通知,所以注册ID必须是正确的。

【问题讨论】:

    标签: php android ios cordova google-cloud-messaging


    【解决方案1】:

    根据此blog,如果发件人 ID 和 API_KEY 不匹配,您将收到 MismatchSenderID 错误

    here 所述,在 Google API 控制台中查看您项目的 URL:https://code.google.com/apis/console/#project:xxxxxxxxxxx 其中xxxxxxxxx 是项目 ID,即发件人 ID。确保 API 密钥属于“服务器应用程序密钥(具有 IP 锁定)”。

    MismatchSenderId 也会在您使用同一设备中的不同密钥登录时发生。尝试卸载应用程序并再次运行它。然后更新注册密钥,然后在终端中运行下面的 CURL 脚本。

    curl -X POST \
    -H "Authorization: key=  write here api_key" \
    -H "Content-Type: application/json" \
    -d '{ 
    "registration_ids": [ 
    "write here reg_id generated by gcm"
    ], 
    "data": { 
    "message": "Manual push notification from Rajkumar"
    },
    "priority": "high"
    }' \
    https://android.googleapis.com/gcm/send
    

    成功或失败都会给出消息。

    检查这些相关线程:

    希望这会有所帮助!

    【讨论】:

    • 感谢您的回答,尽管我已经尝试过这些解决方案。为了提高可读性,我将编辑问题而不是在这里发表评论。
    猜你喜欢
    • 2017-02-18
    • 1970-01-01
    • 1970-01-01
    • 2017-12-09
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多