Google Cloud Messaging(GCM)数据始终为null

时间:2017-11-06 07:29:42

标签: javascript php google-cloud-messaging service-worker

我正在尝试使用PHP + GCM +服务工作者向我的网站(非应用)的用户发送推送通知。

//service-worker.js
self.addEventListener('push', function(event) {
    console.log('Received a push message');
    console.log(event.data.text());
});

当我转到Chrome控制台>>申请>>服务人员>>按控制台显示以下内容:

Received a push message
Test push message from DevTools.

我正在尝试使用PHP脚本完成同样的事情:

$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'  => $ids,
    'data'          => $msg,
);
$headers = array
(
    'Authorization: key=' . $apiKey,
    'Content-Type: application/json'
);
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 );

推送通知会通过,但是,我得到以下内容:

Received a push message
Uncaught TypeError: Cannot read property 'text' of null

我试图在没有运气的情况下挖掘event的其他属性。我读过的大部分帮助文章都是针对Android而不是针对Web开发的。我错过了什么?

0 个答案:

没有答案