繁体   English   中英

推送通知不适用于 Android 与 php

[英]Push notification not working Android with php

我正在尝试从PHPAndroid发送推送通知,我检查了我的项目编号、API 密钥和设备 ID/设备令牌,所有凭据都正确,但出现以下错误:

{"multicast_id":7219223859365110681,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"MismatchSenderId"}]}

这是我的代码:

define( 'API_ACCESS_KEY', 'xxxxxxxxxxxxxxxxxx' );
$registrationIds = array( 'xxxxxxxxxxxxxxxxxx' ); //Replace this with your device token

$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'  => $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;

我的代码有什么问题?

尝试改变这个

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

$fields = array
(
'to'  => 'XXXXXXXXXXX',//device token
'data'=> json_encode($msg)
);

和这个

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

到:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM