不能够通过REGID发送给使用notification_key一组通知,即使它是可能的 [英] Not able to send notification to a group using notification_key even though it is possible through the regId

查看:186
本文介绍了不能够通过REGID发送给使用notification_key一组通知,即使它是可能的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一些PHP code,我能够将通知发送给使用REGID设备。

我也设法创建一组REGID的成功接收的描述上的谷歌文档

但我不能够发送通知到组。我试图用同样的方法来发送通知,但不是使用REGID我使用创建组时,我从GCM收到notification_key,但这种方法不奏效,它给我的NotRegistered错误。
如果我尝试用相同的notification_key_name GCM注册说它已经被注册。

我不知道我是否有通过其他方法来发送,或者如果我做错了什么。


  • 在我使用REGID发送通知我收到此消息
    从GCM:

    {\"multicast_id\":517...442,\"success\":2,\"failure\":0,\"canonical_ids\":0,\"results\":[{\"message_id\":\"0:140...ecd\"},{\"message_id\":\"0:140...ecd\"}]}


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

    {notification_key:APA91 .... nz9Q}


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

    {\"multicast_id\":80...63,\"success\":0,\"failure\":1,\"canonical_ids\":0,\"results\":[{\"error\":\"NotRegistered\"}]}


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

    {错误:notification_key已经存在}


下面是code我使用。

 < PHP类GCM {
 常量GOOGLE_API_KEY =***我的API密钥***; //把你的谷歌API密钥
 常量PROJECT_KEY =***我的项目KEY ***;
功能__construct(){}/ **
 *发送推送通知
 * /
公共职能send_notification($ registatoin_ids,$消息){    $ URL ='https://android.googleapis.com/gcm/send';    $栏=阵列(
        registration_ids'=> $ registatoin_ids,
        '数据'=> $消息
    );    $头=阵列(
        授权:键='。自:: GOOGLE_API_KEY,
        内容类型:应用程序/ JSON
    );
    //打开连接
    $ CH = curl_init();    //设置URL,POST数瓦尔,POST数据
    curl_setopt($ CH,CURLOPT_URL,$网址);    curl_setopt($ CH,CURLOPT_POST,真正的);
    curl_setopt($ CH,CURLOPT_HTTPHEADER,$头);
    curl_setopt($ CH,CURLOPT_RETURNTRANSFER,真正的);    //禁用SSL证书支持,即暂时
    curl_setopt($ CH,CURLOPT_SSL_VERIFYPEER,FALSE);    curl_setopt($ CH,CURLOPT_POSTFIELDS,json_en code($域));
    $结果= curl_exec($ CH);
    如果($结果=== FALSE){
        死('卷曲失败:'curl_error($ CH));
    }    //关闭连接
    curl_close($ CH);
    回声$结果;
}公共职能requestNotificationKeyFromGCM($ registatoin_ids,$用户名){
    //谷歌云通讯GCM-API网址
    $ URL ='https://android.googleapis.com/gcm/notification';
    $请求=阵列(
        '操作'=> '创建',
        notification_key_name'=> $用户名,
        registration_ids'=> $ registatoin_ids,
    );    $头=阵列(
        授权:键='。自:: GOOGLE_API_KEY,
        PROJECT_ID:。自:: PROJECT_KEY,
        内容类型:应用程序/ JSON
    );    $ CH = curl_init();
    curl_setopt($ CH,CURLOPT_URL,$网址);
    curl_setopt($ CH,CURLOPT_POST,真正的);
    curl_setopt($ CH,CURLOPT_HTTPHEADER,$头);
    curl_setopt($ CH,CURLOPT_RETURNTRANSFER,真正的);
    // curl_setopt($ CH,CURLOPT_SSL_VERIFYHOST,0);
    curl_setopt($ CH,CURLOPT_SSL_VERIFYPEER,FALSE);
    curl_setopt($ CH,CURLOPT_POSTFIELDS,json_en code($要求));
    $结果= curl_exec($ CH);    如果($结果=== FALSE){
        死('卷曲失败:'curl_error($ CH));
    }
    curl_close($ CH);
    回声$结果;
}
}?>


解决方案

我有类似的问题,同时试图发送一个GCM与通知的关键。还有就是文档中的一个问题。
而不是使用的:

 卷曲-vvv -X POST --header内容类型:应用程序/ JSON--headerPROJECT_ID:<您的项目-ID>中--header授权:键=<您的项目 - 秘密密钥>中--data @  - https://android.googleapis.com/gcm/send<< EOF
{
   registration_ids:<注册-ID-FROM-装置>],
   数据:{},
}
EOF

您必须使用:

 卷曲-vvv -X POST --header内容类型:应用程序/ JSON--headerPROJECT_ID:<您的项目-ID>中--header授权:键=<您的项目 - 秘密密钥>中--data @  - https://android.googleapis.com/gcm/send<< EOF
{
   要:<通知-ID>中,
   数据:{},
}
EOF

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

I made some php code and I was able to send notifications to devices using the regId.

I also managed to create a group of regId successfully receiving the notification_key as describe on the google documentation.

But I'm not able to send notifications to the group. I tried to use the same method to send the notification but instead of using the regId I am using the notification_key I received from GCM when creating the group, but this approach did not work, it gives me the NotRegistered error. If i try to register with the same notification_key_name GCM say it is already registered.

I am not sure if I have to send it through another method or if I am doing something wrong.

  • When I send the notifications using the regId I receive this message from GCM:

    {"multicast_id":517...442,"success":2,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:140...ecd"},{"message_id":"0:140...ecd"}]}

  • When I create the group I receive this message from GCM:

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

  • When I try to send the message to the group using the notification_key i received on the message above I receive this message from GCM:

    {"multicast_id":80...63,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"NotRegistered"}]}

  • When I try to create the group again with the same notification_key_name I receive this message from GCM:

    {"error":"notification_key already exists"}

Below is the code I am using.

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

?>

解决方案

I had similar issues while trying to send a gcm's with notification key. There is an issue in the documentation. Instead of using:

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

You have to use:

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

You can see more in my blog post: https://medium.com/appunite-edu-collection/d7df385b0ff4

这篇关于不能够通过REGID发送给使用notification_key一组通知,即使它是可能的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆