即使可以通过regId,也无法使用notification_key向组发送通知

时间:2014-07-03 18:17:52

标签: php android notifications google-cloud-messaging

我制作了一些PHP代码,并且能够使用regId向设备发送通知。

我还成功创建了一组regId,成功接收了google documentation中描述的notification_key。

但我无法向群组发送通知。我尝试使用相同的方法发送通知但不是使用regId我使用的是我在创建组时从GCM收到的notification_key,但这种方法不起作用,它给了我NotRegistered错误。 如果我尝试使用相同的notification_key_name注册GCM,则说它已经注册。

我不确定是否必须通过其他方法发送或者我做错了。

  • 当我使用regId发送通知时,我收到此消息 来自GCM:

    {" multicast_id":517 ... 442"成功":2"失效":0," canonical_ids&#34 ;: 0,"结果":[{" MESSAGE_ID":" 0:140 ... ECD"},{" MESSAGE_ID":&# 34; 0:140 ... ECD"}]}

  • 当我创建组时,我从GCM收到此消息:

    {" notification_key":" APA91 .... nz9Q"}

  • 当我尝试使用上面收到的消息收到的notification_key将消息发送到组时,我收到了来自GCM的消息:

    {" multicast_id":80 ... 63,"成功":0,"失效":1," canonical_ids&#34 ;: 0,"结果":[{"错误":" NotRegistered"}]}

  • 当我尝试使用相同的notification_key_name再次创建组时,我从GCM收到此消息:

    {"错误":" notification_key已经存在"}

以下是我正在使用的代码。

<?php

class GCM {
 const GOOGLE_API_KEY= " *** MY API KEY ***"; // Place your Google API Key
 const PROJECT_KEY= " *** MY PROJECT KEY ***";


function __construct() {

}

/**
 * Sending Push Notification
 */
public function send_notification($registatoin_ids, $message) {

    $url = 'https://android.googleapis.com/gcm/send';

    $fields = array(
        'registration_ids' => $registatoin_ids,
        'data' => $message
    );

    $headers = array(
        'Authorization: key=' . self::GOOGLE_API_KEY,
        'Content-Type: application/json'
    );
    // Open connection
    $ch = curl_init();

    // Set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Disabling SSL Certificate support temporarly
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));


    $result = curl_exec($ch);
    if ($result === FALSE) {
        die('Curl failed: ' . curl_error($ch));
    }

    // Close connection
    curl_close($ch);
    echo $result;
}

public function requestNotificationKeyFromGCM($registatoin_ids, $username) {
    //Google cloud messaging GCM-API url
    $url = 'https://android.googleapis.com/gcm/notification';
    $request = array(
        'operation' => 'create',
        'notification_key_name' => $username,
        'registration_ids' => $registatoin_ids,
    );    

    $headers = array(
        'Authorization: key=' . self::GOOGLE_API_KEY,
        'project_id: ' . self::PROJECT_KEY,
        'content-type: application/json'
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request));
    $result = curl_exec($ch);

    if ($result === FALSE) {
        die('Curl failed: ' . curl_error($ch));
    }
    curl_close($ch);
    echo $result;
}
}

?>

2 个答案:

答案 0 :(得分:2)

我在尝试使用通知密钥发送gcm时出现类似问题。文档中存在问题。 而不是使用:

curl -vvv -X POST --header "Content-Type: application/json" --header "project_id: <YOUR-PROJECT-ID>" --header "Authorization: key=<YOUR-PROJECT-SECRET-KEY>" --data @- "https://android.googleapis.com/gcm/send" << EOF
{ 
   "registration_ids": ["<REGISTRATION-ID-FROM-DEVICE>"],
   "data": {},
}
EOF

你必须使用:

curl -vvv -X POST --header "Content-Type: application/json" --header "project_id: <YOUR-PROJECT-ID>" --header "Authorization: key=<YOUR-PROJECT-SECRET-KEY>" --data @- "https://android.googleapis.com/gcm/send" << EOF
{ 
   "to": "<NOTIFICATION-ID>",
   "data": {},
}
EOF

您可以在我的博文中看到更多内容:https://medium.com/appunite-edu-collection/d7df385b0ff4

答案 1 :(得分:2)

我按照Google's Documentation的文档进行操作 (我推动iOS和Android)。

{
  "to": "your_token_ID",
  "data": {
    "hello": "This is a Firebase Cloud Messaging Device Group Message!",
  }
}

但它没有用,所以我尝试了:

{
  "to": "your_token_ID", 
  "notification": {
    "sound": "default",
    "gcmSandbox": "true",
    "badge": 1,
    "title" : "Push Title",
    "body": "Push Body"
  }
}

这种方法对我有用,希望它能帮到别人。