简体   繁体   中英

Android Conditional PendingIntent in Notification?

My application currently has two activities: a splash activity which displays the app logo and loads the user's data from a remote database, and then the main activity that houses all my fragments. I've reached a bit of an impasse right now with notifications. I'm using FCM to deliver the notifications and have that working fine, but don't really know what to do with the pending intents of the notifications. If the app is closed, I don't want it to launch the main activity since I load the data from the DB in the splash activity. Thus the main activity would have missing data (it currently crashes due to null pointer exceptions everywhere). But if the app is still running, I don't want the splash screen to be launched again. Is there some type of way to have a conditional intent baked into the notification?

Alternatively, is this just bad practice to do what I'm doing with launching the activities from the notifications? I'm by no means an android expert so there might indeed be a better way. I would prefer to keep my current activity flow however where the splash screen is responsible for the initial loading of data. I was hoping there was some way to just broadcast a message on the click of the notification and if any activities have an active listener then it would just consume that data.

The solution I have come up with is to have a single activity whose sole purpose is to process notifications and then pass them to the appropriate activity either through a new intent (if app is terminated) or through a broadcast (if app is resumed). Not sure if this is bad practice (would appreciate anyone telling me if this violates any type of software principle).

It's working like a charm so far and is honestly really nice to have all that logic in one activity anyway - all the service has to do is pass in the data from the notification and the action and the activity does the rest.

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