一次发送到多个设备时,GCM会收到无效令牌 [英] GCM get invalid tokens when sending to multiple devices at once

查看:182
本文介绍了一次发送到多个设备时,GCM会收到无效令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我通过以下方式发送: $ b $

我试图从广告数据库中删除无效的GCM令牌。 b

  payload = {
registration_ids:gcm_keys,
data:messageData
}
headers = {
'Content-type':'application / json',
'Authorization':Message_Broker.api_key
}

尝试:
返回请求。 post(Message_Broker.host,data = json.dumps(payload),headers = headers)

可以说我尝试发送信息到:

  gcm_keys = ['daöodaeöoijiö','12345','fwiorjpfwj']#让我们假装第二个是一个有效的令牌

从响应对象的内容我得到一个这样的字典:

  response_results = [{u'error':u'InvalidRegistration'},{u'registration_id':u'1234567', u'message_id':u'0:14339323424213768%54 0eeb39f9fd7aed'},{u'error':u'InvalidRegistration'}] 

要找出哪些令牌失败我做了一个列表理解的集合减法:

  failed_keys = list(set(gcm_keys) -  set([r.get ('registration_id')for r in response_results]))

这应该只让我回想起那些令牌产生了一个错误。



我的第一个问题是,是否有更常见的方法或任何类型的最佳做法?

第二个问题是,您可以在 response_results gcm_keys 中看到,有效标记是没有返回。相反,类似的令牌会被退回。
我没有在这发现任何。为什么我会得到一个不同的令牌?



- >刚发现如果我得到一个不同的令牌,我应该替换旧的令牌。这导致了另一个问题。当我发送到多个令牌时,如何才能找出请求中返回的令牌所属的gcm令牌?
结果中的顺序总是与请求中的顺序相同吗?

解决方案

响应主体的结果参数应该有以与请求相同的顺序列出的对象数组。请参阅此处


I'm trying to remove invalid GCM Tokens from my database after they failed in a broadcast request.

I send it via:

payload = {
            "registration_ids": gcm_keys,
            "data": messageData
        }
headers = {
    'Content-type': 'application/json',
    'Authorization': Message_Broker.api_key
}

try:
    return requests.post(Message_Broker.host, data=json.dumps(payload), headers=headers)

Lets say I try to send a message to:

gcm_keys = ['daöodaeöoijiö','12345','fwiorjpfwj'] # Lets pretend the second one is a valid token

From the content of the response object I get a dict like this:

response_results = [{u'error': u'InvalidRegistration'}, {u'registration_id': u'1234567', u'message_id': u'0:14339323424213768%540eeb39f9fd7aed'}, {u'error': u'InvalidRegistration'}]

To find out which tokens failed I made a set substraction with a list comprehension:

failed_keys = list(set(gcm_keys) - set([r.get('registration_id') for r in response_results]))

This should give me back only the tokens which produced an error.

My first question is, is there a more common way to do so or any kind of best practice?

Second question is, as you can see in the response_results and the gcm_keys, the valid token is not returned. Instead a kinda similar token is given back. I did not find any on this. Why do I get a different token back?

-> Just found out that in case I get a different token back, I should replace the old one. That leads to another question. When I send to several tokens, how can I find out to which gcm token in the request this returned token belongs? Is the order in the results always the same as in the request?

解决方案

The response body's results parameter should have an array of objects that are listed in the same order as the request. Please refer here.

这篇关于一次发送到多个设备时,GCM会收到无效令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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