I'm trying to start an activity when I receive a push notification from firebase but the problem is works when the app is foreground but it does not work when the app is the background or killed.
could someone help me with the problem Thanks in advance.
how can I achieve that with a service or broadcast receivers
is this can be achieved using notification listerners
like showing the calling UI with two buttons to accept or reject a call like whatsapp or skype
My FirebaseMessagingService calls look like this.
class MyFirebaseMessagingService:FirebaseMessagingService() {
val TAG= String::class.java.simpleName
override fun onMessageReceived(remoteMessage: RemoteMessage) {
super.onMessageReceived(remoteMessage)
Log.d(TAG, "From: ${remoteMessage?.from}")
// Check if message contains a data payload.
remoteMessage?.data?.isNotEmpty()?.let {
Log.d(TAG, "Message data payload: " + remoteMessage.data)
// Compose and show notification
if (!remoteMessage.data.isNullOrEmpty()) {
Log.d(TAG, "Message data payload: " + remoteMessage.data)
try {
val data = JSONObject(remoteMessage.data as Map<*, *>)
val jsonMessage = data.getString("extra_information")
Log.d(
TAG, "onMessageReceived: Extra Information: $jsonMessage".trimIndent()
)
} catch (e: JSONException) {
e.printStackTrace()
}
}
}
// Check if message contains a notification payload.
if (remoteMessage.notification != null) {
val title = remoteMessage.notification!!.title //get title
val message = remoteMessage.notification!!.body //get message
val click_action =
remoteMessage.notification!!.clickAction //get click_action
Log.d(TAG, "Message Notification Title: $title")
Log.d(TAG, "Message Notification Body: $message")
Log.d(TAG, "Message Notification click_action: $click_action")
sendNotification(title!!, message!!, click_action!!)
}
}
override fun onNewToken(token: String) {
Log.d(TAG, "Refreshed token: $token")
// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
// sendRegistrationToServer(token)
MyPreferences.getInstance(this).registrationToken=token
}
private fun sendNotification( title:String, messageBody:String,click_action:String) {
val receisveCallAction =
Intent(this, LocationActivity::class.java)
receisveCallAction.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
val pendingIntent = PendingIntent.getActivity(this, 0, receisveCallAction, PendingIntent.FLAG_ONE_SHOT)
val channelId = getString(R.string.default_notification_channel_id)
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.fitcurelogo)
.setContentTitle(title)
.setContentText(messageBody)
.setPriority(Notification.PRIORITY_MAX)
.setWhen(0)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// Since android Oreo notification channel is needed.
// https://developer.android.com/training/notify-user/build-notification#Priority
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(channelId, "Channel human readable title", NotificationManager.IMPORTANCE_HIGH)
notificationManager.createNotificationChannel(channel)
}
notificationManager.notify(0, notificationBuilder.build())
startActivity(receisveCallAction)
}
}