Jazib Khan
Jazib Khan

Reputation: 460

How to get the intent of the Active Notification?

I'm creating an app that has a broadcast receiver, which is registered through the manifests.

It receives an intent from other apps and shows the notification. The notification has a pendingIntent which launches the MainActivity on tap, and pass some values (like packageName of the sender's application) to it using the intent. Now, if the user does not open the MainActivity using the Notification, I'm using NotificationManager.getActiveNotifications() method to get the active notification, but I'm not able to get the intent from it.

Things I've tried so far:

  1. I don't want to use a dynamic broadcast receiver as I want to receive the intent in the background.
  2. I have tried to store the data while creating the notification in the sharedPreference, that I can later get in the MainActivity if there are any active notifications. Not sure if its the best way to do it.
  3. Using service's onBind method to get the data in the MainActivity, but background services can't be started from the broadcast receiver, so it only works when the MainActivity is running.

Is there any way to notify MainActivity about the data from the broadcast receiver, even if the MainActivity is not running, and get the data as soon as MainActivity is launched. Any help will be appreciated.

Upvotes: 0

Views: 327

Answers (1)

Francesco Re
Francesco Re

Reputation: 836

Using NotificationManager.getActiveNotifications() to get the information you stored in the notifications is not a good idea, in my opinion. You already stated in your question some of the reasons why this is a bad idea. Also if the user deletes the notification before opening your app then you lose all the information.

The best way to do this is by storing data on the device memory (the way you store that data depends on the data itself, it could be sharedPreferences, database, file...) when you receive it in broadcast receiver.

In MainActivity then you can retrieve data from memory and process it.

Upvotes: 1

Related Questions