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


Not able to send notification to a group using notification_key even though it is possible through the regId

我编写了一些php代码,我能够使用regd向设备发送通知。

我还设法创建了一组regd,成功地接收了谷歌文档中描述的notification_key。

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

我不确定我是否必须通过另一种方法发送它,或者我是否做错了什么。

  • 当我使用regd发送通知时,我收到此消息从GCM:

    {"multicast_id":517年…442年,"成功":2,"失败":0,"canonical_ids":0,"结果":[{"message_id":"0:140…ecd"},{"message_id":"0:140…ecd"}]}

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

    {"notification_key":"APA91…nz9Q"}

  • 当我尝试使用上面消息上收到的notification_key向组发送消息时,我从GCM收到此消息:

    {"multicast_id":80年…63年,"成功":0,"失败":1、"canonical_ids":0,"结果":[{"错误":"NotRegistered"}]}

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

    {"error":"notification_key already exists"}

下面是我使用的代码。

<?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;
}
}
?>

我在尝试发送带有通知键的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

我遵循了来自Google文档的文档(我正在推动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"
  }
}

这个方法对我来说很有效,希望能对别人有所帮助。