Reputation: 460
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:
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.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
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