5

我有具有 PHP 服务器端和 Android 客户端应用程序的系统。Android 通过 web 服务发送参数,PHP 处理 GCM 端。PHP 发送推送通知,在发送之前,它会从数据库中获取所有注册 ID。问题是,同一个设备可能有两个或多个registrationid。因此,推送通知会向同一设备发送两次或更多次。有没有解决这个问题的方法?

4

8 回答 8

4

GCM 可以更改registrationId,您必须在服务器端对其进行更新。

  • 当您发送消息时,结果可能包含将用于设备的新registrationId,因此您必须为新的registrationId 更新旧registrationId。

在解释成功响应部分查看此链接

如果设置了 message_id,请检查registration_id:如果设置了registration_id,则将原始ID 替换为服务器数据库中的新值(规范ID)。注意原始ID不是结果的一部分,所以需要从请求中传入的registration_ids列表中获取(使用相同的索引)。

于 2013-05-29T08:09:39.620 回答
3

有时 Google 会更改注册 ID,您会关联多个 ID。发送通知的服务器(您的服务器)必须使用新 ID 更新数据库。

有关更多信息,请查看此文档:

http://developer.android.com/google/gcm/adv.html

他们说:

这是一个规范的 ID

在服务器端,只要应用程序运行良好,一切都应该正常工作。但是,如果应用程序中的错误触发同一设备的多个注册,则可能很难协调状态,并且您最终可能会收到重复的消息。

GCM 提供了一种称为“规范注册 ID”的工具,可以轻松地从这些情况中恢复。规范注册 ID 定义为您的应用程序请求的最后一次注册的 ID。这是服务器在向设备发送消息时应使用的 ID。

如果稍后您尝试使用不同的注册 ID 发送消息,GCM 将照常处理请求,但它将在响应的 registration_id 字段中包含规范注册 ID。确保使用此规范 ID 替换存储在您服务器中的注册 ID,因为最终您使用的 ID 将停止工作。

于 2014-08-03T22:57:26.003 回答
2

您只需为数据库中的每个设备存储一个唯一 ID。然后,您可以只为每台设备发送一个推送通知。

我从来没有看到同一设备的多个注册 ID。奇怪的事情。

您是否正确管理了注册 ID 更新?

于 2013-05-29T08:00:28.443 回答
2

这是我对这个问题的解决方案。当您发送 gcm 通知时,您会收到这样的回复

Array
    (
        [multicast_id] => 12345678910
        [success] => 8
        [failure] => 3
        [canonical_ids] => 4
        [results] => Array
            (
                [0] => Array
                    (
                        [error] => NotRegistered
                    )

                [1] => Array
                    (
                        [message_id] => 0:1412242641156904%3dc89e2df9fd7ecd
                    )

                [2] => Array
                    (
                        [registration_id] => APA91bH3WdWwqFCVKbvZXsf-gj28iWU5oYbSCRZYFp987CHasxwT_HOiE7dp8212XID0FMGVG2n4NLohFsEGYJ-LEA07xsgsKfT00xModQcx5QgTBmJtxlWgeaWFpz29z-iCPYbvOHEhqfwHmN-HIps7DiWntcs-Qg
                        [message_id] => 0:1412242641155639%3dc89e2df9fd7ecd
                    )

                [3] => Array
                    (
                        [registration_id] => APA91bH3WdWwqFCVKbvZXsf-gj28iWU5oYbSCRZYFp987CHasxwT_HOiE7dp8212XID0FMGVG2n4NLohFsEGYJ-LEA07xsgsKfT00xModQcx5QgTBmJtxlWgeaWFpz29z-iCPYbvOHEhqfwHmN-HIps7DiWntcs-Qg
                        [message_id] => 0:1412242641155809%3dc89e2df9fd7ecd
                    )


                [4] => Array
                    (
                        [message_id] => 0:1412242641157971%3dc89e2df9fd7ecd
                    )


                [5] => Array
                    (
                        [registration_id] => APA91bGXo_gnfBZsvPoqJTYy1BWz0FQIkwlD1EmBtcxgWWfceYvd0ehWqVCtfd8n56VGYrvXCS2v48kTiA69BD7Sci0BA9a9bKTIg_MUEnDd79ssCK-miPG88DDGL4oKtB14cPbh-_xbgVRZllMOzwNZf_w5uJGR8g
                        [message_id] => 0:1412242641157969%3dc89e2df9fd7ecd
                    )


                [6] => Array
                    (
                        [registration_id] => APA91bGXo_gnfBZsvPoqJTYy1BWz0FQIkwlD1EmBtcxgWWfceYvd0ehWqVCtfd8n56VGYrvXCS2v48kTiA69BD7Sci0BA9a9bKTIg_MUEnDd79ssCK-miPG88DDGL4oKtB14cPbh-_xbgVRZllMOzwNZf_w5uJGR8g
                        [message_id] => 0:1412242641157967%3dc89e2df9fd7ecd
                    )

            )

    )

在数组中有一个索引canonical_ids为 4,这意味着你有 4 个用户已经生成了新的 gcm id,现在你需要做的就是用你在上面数组中收到的新的 gcmid 替换他们的 gcmid,下次当你发送 gcm 通知时.

我的 php 应用程序建立在 codeigniter 框架上,所以这就是我所做的。我有一个名为 login 的模型,在那里我有一个函数从数据库返回 gcmids 以及用户的用户 ID 作为数组的索引

function getAllRegIds(){
    $query = "SELECT gcmid,id FROM users_app WHERE gcmid IS NOT NULL AND gcmid != '' GROUP BY gcmid"; //group by incase a user loggedin with multiple userids in your a
    $query = $this->db->query($query);

    if($query->num_rows() > 0){
        $result = $query->result_array();
        $regids = array();

        foreach($result as $row){
            $regids[$row['id']] = $row['gcmid'];
        }
        return $regids; 
    }else{
        return false;
    }
}

数组看起来像这样

Array(
  [29] => APA91bEYda3DVb...
  [1] => APA91bF0yfdZjX4...
  [12] => APA91bG-9fsBGT-...
  [11] => APA91bGNRh_VWF...
  [3] => APA91bGXo_gnfBZ...
  [2] => APA91bH3WdWwqFC...
  [26] => APA91bHn8Ufwe4...
)

index 是值为 gcmid 的用户 ID

这个函数在控制器函数内部调用,这里是实现

public function sendtoall(){
    $this->load->model('app/login');

    $result = $this->login->getAllRegIds(); //here i got all gcmids with userids as index

    $message = $this->input->post('message');
    $chunks = array_chunk($result,1000); //broken the array into chunks of 1000 ids because gcm can only send message to 1000 ids at a time

    $status = $this->sendNotification($chunks,$message);


    if(!empty($status) && is_array($status)){
        foreach($status as $key=>$row){

            if(array_key_exists('canonical_ids',$row) && $row['canonical_ids'] > 0){
                $canonical_ids = $row['canonical_ids'];

                if(array_key_exists('results',$row) && !empty($row['results']) && is_array($row['results'])){
                    foreach($row['results'] as $k=>$v){
                        if(array_key_exists('registration_id',$v)){

                            $userid = array_search($chunks[$key][$k], $result);
                            $newgcmid  = $v['registration_id'];

                            $this->login->updateGCMId($userid,$newgcmid);
                        }
                    }
                }
            }
        }
    }

    echo json_encode($status);
}

正如你在评论中我已经将数组分成 1000 个 id 的块,因为 gcm 可以一次向 1000 个 id 发送消息,并调用一个函数sendNotification来返回 gcm 接收到的响应,这是我在上面粘贴的具有所有规范的数组身份证。

然后我检查状态是否不为空并且是一个数组,foreach 状态因为它可能有多个响应数组,因为我以 1000 个 id 的块发送通知,请参见sendNotification下面的实现

然后我检查它是否有索引canonical_ids并且大于0,这意味着有需要替换的id,然后我检查它是否有索引result,不为空并且是一个数组。

foreachresult并获取其中包含索引的那些索引的键和值registration_id,这意味着需要在我们的数据库中替换这些 id

通过搜索数组来获取需要替换userid的用户的,用户的oldgcm id(,第一个参数是你需要的块的键,第二个是那个)gcmidresult$chunks[$key][$k]gcmidindexgcmid

gcmidresults数组中获取新的

调用模型函数updateGCMId,传递useridand newgcmid

这里是实现sendNotificationupdateGCMId功能。

private function sendNotification($regids,$message){
    $status = array();
    $result = $regids;
    if($result){
        foreach($result as $thousandids){
            $registatoin_ids=$thousandids;

            $msg=array("message"=>$message);



            $url='https://android.googleapis.com/gcm/send';
            $fields=array
             (
              'registration_ids'=>$registatoin_ids,
              'data'=>$msg
             );
            $headers=array
             (
              'Authorization: key=AIza..............',
              '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_VERIFYPEER,false);
            curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($fields));
            $result=curl_exec($ch);
            curl_close($ch);


            $result = json_decode($result,1);

            $status[] = $result;

        }
    }

    return $status;
}

update功能:

public function updateGCMId($userid,$gcmid){
      $query = 'UPDATE users_app SET 
                  gcmid = "'.$gcmid.'" 
                WHERE id = "'.$userid.'"';
      $this->db->query($query);
}
于 2014-10-02T10:54:41.600 回答
0

终于得到了Duplicate Registration Id的工作解决方案。这一切都是关于使用规范 id 更新现有注册 id。

于 2015-04-23T09:11:25.713 回答
0

我认为您的问题是如何从设备获取注册 ID。您必须确保它会发送一次 regID,并且当 GCM 服务器请求刷新时,您必须将 regID 重新发送到您的网络服务器并存储它,覆盖旧的 redID。请参阅android参考的这一部分。

于 2013-05-29T08:08:47.960 回答
0

您将在推送响应中获得规范的 Id。您需要使用新的规范 ID 更新旧 ID。

还有一种解决方案。更像是一个补丁。您可以在 GCM 请求 JSON 中传递参数“dry_run”。当我们将其设置为 true 时,它​​将向设备 ID 发送虚假消息并生成响应。

设备不会收到消息,但您会收到回复,因此您可以检查哪些设备 Id 在其结果中具有registration_ids 并将它们从数据库中删除。

        $fields = array(
                    'registration_ids'  => $deviceId,
                    'data' => array( "message" =>'fake_message'),
                    'dry_run'=>true
                    );

希望能帮助到你。

于 2014-09-17T05:29:03.343 回答
0

从 Android Studio 中的模板生成的后端附带的 Google 示例代码会为您执行此操作。查看 MessageEndpoint 类,您会注意到他们正在检查规范 id,如果存在,则假设 regid 已更改,因此理想情况下需要针对该特定设备进行更新。

public void sendMessage(@Named("message") String message) throws IOException {
    if (message == null || message.trim().length() == 0) {
        log.warning("Not sending message because it is empty");
        return;
    }
    // crop longer messages
    if (message.length() > 1000) {
        message = message.substring(0, 1000) + "[...]";
    }
    Sender sender = new Sender(API_KEY);
    Message msg = new Message.Builder().addData("message", message).build();
    List<RegistrationRecord> records = ofy().load().type(RegistrationRecord.class).limit(10).list();
    for (RegistrationRecord record : records) {
        Result result = sender.send(msg, record.getRegId(), 5);
        if (result.getMessageId() != null) {
            log.info("Message sent to " + record.getRegId());
            String canonicalRegId = result.getCanonicalRegistrationId();
            if (canonicalRegId != null) {
                // if the regId changed, we have to update the datastore
                log.info("Registration Id changed for " + record.getRegId() + " updating to " + canonicalRegId);
                record.setRegId(canonicalRegId);
                ofy().save().entity(record).now();
            }
        } else {
            String error = result.getErrorCodeName();
            if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
                log.warning("Registration Id " + record.getRegId() + " no longer registered with GCM, removing from datastore");
                // if the device is no longer registered with Gcm, remove it from the datastore
                ofy().delete().entity(record).now();
            } else {
                log.warning("Error when sending message : " + error);
            }
        }
    }
}
于 2015-10-10T18:31:27.147 回答