0

I have Firebase implemented at application level to receive push messages. In the library project, I have a BroadcastReceiver to intercept push messages. I want to build notification from the library and not from the application.
Incase of app in foreground state, when I create a Pending intent, the MainActivity context available at the app level is passed in the pending intent, upon which if the notification is tapped, I am redirected to MainActivity. Now if the app is in killed state and push message is received, my broadcast receiver in the library intercepts the incoming message, but not able to create notification because the MainActivity context is null as the MainActivity at app level is not available in the stack trace. Can anyone assist how to achieve this?

Below is my code :

val mIntent = Intent(applicationContext,activityContext::class.java)
val pendingIntent = PendingIntent.getActivity(applicationContext, System.currentTimeMillis().toInt(),
mIntent,PendingIntent.FLAG_UPDATE_CURRENT)

where, activityContext in the Intent is the context of MainActivity received inside the library project from app level.

When app is in foreground state, activityContext is available and notification gets generated. But when app is in killed state, activityContext remains null leading to failure in building the notification.

KENdi
  • 7,576
  • 2
  • 16
  • 31
Prabhtej Singh
  • 335
  • 3
  • 7
  • Doesn't Firebase automatically generate and display notification when your application is not in foreground? – Dhaval Feb 04 '19 at 09:20
  • @Dhaval Firebase only triggers push messages to Android devices. Its dependent on whether the notification is of message type or data type. For message type, Android OS handles itself in notification tray and generates them but with this we cannot extract the data payload sent along. With data type notification, we can get the payload but have to create our notifications ourselves. Since we are following later approach, so stuck with the problem. – Prabhtej Singh Feb 04 '19 at 10:56

1 Answers1

0

You don't need the Activity's Context, because if the app isn't running, it doesn't exist.

You should have the app pass the library the name of the MainActivity (ie: fully qualified class name). The library can then use the name of the MainActivity to set the component parameters in the Intent.

This means instead of this:

val mIntent = Intent(applicationContext,activityContext::class.java)

You want to do something like this (my Kotlin syntax may be wrong, sorry):

val mIntent = Intent()
mIntent.setClassName(applicationContext, nameOfMainActivity)
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • I see. It looks like you need a hook, so that the library code can call a method that is provided by the application, which will return the name of the `MainActivity`. Your `BroadcastReceiver` would then call that method in `onReceive()` to determine the name of the `MainActivity`. Another way to do this would be to add some "meta-data" to the manifest, that your `BroadcastReceiver` could read to determine the name of the `MainActivity`. Alternatively you could store the name of the `MainActivity` in `SharedPreferences` – David Wasser Feb 07 '19 at 10:50