简体   繁体   中英

Android broadcast receiver && App running status

I used third party push service in my application, when a message is push from server, it's background service will send a broadcast which I have to listen to, this process just works fine. In the function onReceive() ,I can either show notification or just do some work silently. Whether show notification or not will depend on the app status, that is:

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**/
}

What is the best practice to inform my application when the broadcast receiver gets the push message from server?

--EDIT--

By best practice, I mean the best way to send the message(without changing the message handling logic in my app) to my application to deal with.

This could help.

  1. Use services: it's recommended but I don't like it much.
  2. Intents: U could start your application and set flags such as single top which will check if your app is running in the baground and if yes it simply brings it to the app. And if you need to do this silently you could start a separate activity which basically displays a transparent baground and on launch it handles the intent and then closes it self. But of course I services will make the user experience better but I don't have much knowledge in services.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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