android 中来自 SignalR 的推送通知的 Intent 服务

Intent Service for push notifications in android from SignalR

我正在使用 SignalR 向我的 android 客户端发送通知。为此,我正在使用 Intent Service。问题是当我从最近的应用程序中刷出该应用程序时,该服务会停止。它正在发生,因为 IntentService 并非一直 运行 但它在 Intent 基础上工作。

基本上,我想知道我是否应该使用常规服务来不断接收通知,或者是否有任何其他方法可以让意向服务保持活动状态。

提前致谢!

IntentService 旨在完成一项工作然后关闭,因此不能为此使用 Intent 服务。

对于您的任务,您需要使服务具有粘性 return START_STICKY onStartCommand 服务。并且在文档中提到如果进程被杀死系统将再次重新创建

if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then leave it in the started state but don't retain this delivered intent. Later the system will try to re-create the service. Because it is in the started state, it will guarantee to call onStartCommand(Intent, int, int) after creating the new service instance; if there are not any pending start commands to be delivered to the service, it will be called with a null the intent object, so you must take care to check for this.