简体   繁体   English

将GCM通知发送到脱机设备

[英]Send GCM notification to an offline device

If I am sending a notification to a device, and that device is offline I get something like: 如果我向设备发送通知,并且该设备处于脱机状态,我会得到以下内容:

Error: Unavailable 错误:不可用

And I have to resend. 我必须重新发送。

My question is: 我的问题是:

Will the GCM server keep these notifications in a queue and automatically resend when the device is online? GCM服务器是否会将这些通知保留在队列中并在设备联机时自动重新发送? Or it must be completely handled by me. 或者必须由我完全处理。

Because if the GCM server is going to send them automatically(once the device is online), until it actually sends the notifications, my server assumes they are already sent. 因为如果GCM服务器要自动发送它们(一旦设备在线),直到它实际发送通知,我的服务器假定它们已经被发送。 How to track the time when the notifications are resent successfully? 如何跟踪成功重新发送通知的时间?

I might mark on my server side that the notifications are not sent by looking at the Unavailable error message but cannot make out how to mark them as sent once the GCM successfully sends the notifications. 我可能会在服务器端标记通过查看“ Unavailable error message未发送通知,但无法确定如何在GCM成功发送通知后将其标记为已发送。

Thank You 谢谢

A/c to documentation--- When a 3rd-party server posts a message to GCM and receives a message ID back, it does not mean that the message was already delivered to the device. A / c to documentation ---当第三方服务器向GCM发送消息并收回消息ID时,并不意味着消息已经传递给设备。 Rather, it means that it was accepted for delivery. 相反,它意味着它被接受交付。 What happens to the message after it is accepted depends on many factors. 消息被接受后会发生什么取决于许多因素。

If the device is connected but idle, the message will still be delivered right away unless the delay_while_idle flag is set to true. 如果设备已连接但空闲,则除非delay_while_idle标志设置为true,否则仍会立即传递消息。 Otherwise, it will be stored in the GCM servers until the device is awake. 否则,它将存储在GCM服务器中,直到设备处于唤醒状态。 And that's where the collapse_key flag plays a role: if there is already a message with the same collapse key (and registration ID) stored and waiting for delivery, the old message will be discarded and the new message will take its place (that is, the old message will be collapsed by the new one). 而且这就是collapse_key标志起作用的地方:如果已经存在一条消息,其中存储了相同的折叠密钥(和注册ID)并等待传递,则旧消息将被丢弃,新消息将取代它(即,旧消息将被新消息折叠。 However, if the collapse key is not set, both the new and old messages are stored for future delivery. 但是,如果未设置折叠键,则会存储新旧邮件以供将来传递。

Note: There is a limit on how many messages can be stored without collapsing. 注意:如果没有折叠,可以存储的消息数量有限制。 That limit is currently 100. If the limit is reached, all stored messages are discarded. 该限制当前为100.如果达到限制,则丢弃所有存储的消息。

What I did was to separate the push indication from the payload . 我所做的是将推送指示有效载荷分开。 In my GCM message I only include a URI to the payload, and I store the payload in a database table accessible through the URI in the message. 在我的GCM消息中,我只包含有效负载的URI,并将有效负载存储在可通过消息中的URI访问的数据库表中。

When the client receives a message, it could eg look like this, with HATEOAS style links: 当客户端收到消息时,它可能看起来像这样,使用HATEOAS样式链接:

{
  _links: {
    message: {
      rel: 'message',
      href: 'https://my-server.com/push/<messageId>'
    }
  }
}

The client then goes to GET the message payload from the URI, at which point the server knows that it's been delivered and can update accordingly. 然后客户端从URI GET消息有效负载,此时服务器知道它已经被传递并且可以相应地更新。 Fetching the payload also deletes it. 获取有效负载也会删除它。

If GCM re-delivery is not robust enough this also means that the client can choose to manually fetch all pending messages, eg when network connectivity is resumed after being offline, by having an endpoint that returns all messages for a given ANDROID_ID or similar. 如果GCM重新传递不够健壮,这也意味着客户端可以选择手动获取所有未决消息,例如,在离线后恢复网络连接时,通过使端点返回给定ANDROID_ID或类似的所有消息。 If then later the GCM message is delivered, the client would get a 404 for the URI in that message and treat that as a no-op, ie, message already handled. 如果再到后来的GCM 消息传递,客户端将得到的URI 404在该消息,并将其视为无操作,即消息已经处理。

If this is overkill, a light-weight approach to just achieve server awareness of message delivery is to have an endpoint that simply ACKs the reception of a message with a given ID, such as 如果这是过度杀伤,那么仅仅实现服务器对消息传递的意识的轻量级方法是使端点简单地确认接收具有给定ID的消息,例如

POST https://my-server.com/push/notifyReceived

{
  messageId: <messageId>
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM