我的 Android 应用程序如何对推送通知做出反应并在后台下载数据 [英] How can my Android app react to a push notification and download data in the background

查看:57
本文介绍了我的 Android 应用程序如何对推送通知做出反应并在后台下载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望让我的应用在收到通知时自动下载一些数据(大约 10KB),从而提高响应速度.我的两个选择(我认为)是:

I'm looking to make my app more responsive by having it automatically download some data (around 10KB) when it receives a notification. My two options (I think) are:

  1. 尝试将我需要的所有信息打包到 4k 有效负载限制中 - 这可能是可能的,但肯定对 iOS/WP 实现没有帮助,因为它们的限制要低得多.它也非常不可扩展,我需要手工制作消息.
  2. 发送通知,应用会对此做出反应并在后台下载数据.

如果是 2,我可以这样做吗?当应用程序尚未在后台或前台运行时可以完成吗?

If it's going to be 2, can I do that? Can it be done when the app isn't already running in the background or foreground?

对于某些更新,我想向用户显示通知.这可以在同一个通知中完成,还是我需要推送另一个?

For some updates I'd like to show a notification to the user. Can this be done in the same notification, or do I need to push another one?

非常感谢有关 iOS 的任何类似信息,但对于回答问题不是必不可少的!

Any similar info about iOS much appreciated, but not essential for answering the question!

推荐答案

使用 Google Cloud 时消息传递 您创建了一个接收 GCM 推送通知的广播接收器.广播接收器可以自己处理通知,也可以启动意图服务(这更适合从服务器下载数据的逻辑,因为它运行在单独的线程上,并且不会阻塞主 GUI 线程).作为同一通知的结果,您可以显示通知并从您的服务器下载数据.

When using Google Cloud Messaging you create a broadcast receiver which receives the GCM push notification. The broadcast receiver can either handle the notification on its own or start an intent service (which is better suited for your logic that downloads data from the server, since it runs on a separate thread, and doesn't block the main GUI thread). You can display a notification and download data from your server as a result of the same notification.

这篇关于我的 Android 应用程序如何对推送通知做出反应并在后台下载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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