Android 广播接收器 && 应用 运行 状态

Android broadcast receiver && App running status

我在我的应用程序中使用了第三方推送服务,当从服务器推送消息时,它的后台服务会发送一个我必须收听的广播,这个过程工作正常。在函数 onReceive() 中,我可以显示通知或静静地做一些工作。是否显示通知将取决于应用状态,即:

if(application_is_running){
    if(message_type == chat_message){
        if(chat_activity_is_present){
            /** notification not allowed **/
        }else{
            /** show notification, open the corresponding chat activity on clicking the notification */
        }
    }else{
        /** store the message silently **/
    }
}else{
    /** show notification, and start the app on clicking the notification**/
}

当广播接收器从服务器收到推送消息时通知我的应用程序的最佳做法是什么?

--编辑--

我所说的最佳实践是指将消息(不更改我的应用程序中的消息处理逻辑)发送到我的应用程序进行处理的最佳方式。

这会有所帮助。

  1. 使用服务:推荐,但我不太喜欢。
  2. 意图:你可以启动你的应用程序并设置标志,例如单顶,它将检查你的应用程序是否在后台 运行,如果是,它只是将它带到应用程序。如果您需要默默地执行此操作,您可以启动一个单独的 activity,它基本上显示一个透明的背景,并在启动时处理意图,然后自行关闭。但是我的服务当然会让用户体验更好但是我对服务的了解不多