GCM IntentService how to display a pop up on notification receive

前端 未结 2 973
旧巷少年郎
旧巷少年郎 2021-01-15 08:09

I would like to, on receive of a GCM, display a Popup on my current Activity if my app is active.

I wanted to access my current activity in GcmIntentService but I do

2条回答
  •  情深已故
    2021-01-15 08:26

    Well, you can use LocalBroadcast. See LocalBroadcast Manager. For how to implement is, there is a great example on how to use LocalBroadcastManager?.

    LocalBroadcast Manager is a helper to register for and send broadcasts of Intents to local objects within your process. The data you are broadcasting won't leave your app, so don't need to worry about leaking private data.`

    Your activity registers for this local broadcast. From the service you send a LocalBroadcast from within the onMessage (saying that hey, I received a message. Show it activity). Then inside your Activity you can listen to the broadcast. This way if the activity is in the forefront/is active, it will receive the broadcast otherwise it won't. So, whenever you receive that local broadcast, you may do the desired action if activity is open.

    If you want to do for the whole app, then you can make all your activities extend an abstract activity. And inside this abstract activity class you can register it for this 'LocalBroadcast'. Other way is to register for LocalBroadcast inside all your activities (but then you'll have to manage how you'll show the message only once).

    Hope it helps you.

提交回复
热议问题