I want to launch an app automatically when user receive notification on app there will be no click action on launcher icon.
Asked
Active
Viewed 3,384 times
3
-
[See here](http://stackoverflow.com/a/30090042/681929) – nobalG Mar 23 '17 at 12:33
-
Not working I checked this – Vishvendu Palawat Mar 23 '17 at 12:35
-
Dude follow this https://answers.madewithmarmalade.com/questions/16205/launch-application-from-notification-android-edk.html – Sandeep Devhare Mar 23 '17 at 12:52
-
@nobalG this was for GCM I am asking for FCM – Vishvendu Palawat Mar 23 '17 at 13:48
3 Answers
2
Method onMessageReceived() works fine when app in background if notification body does not contain "notification" parameter. All data should be paste in "data". Like this:
{
"to":"token",
"priority":"high",
"data": {
"title": "Carmen",
"text": "Популярные новости за сегодня!",
etc..
}
}
Then you can parse it in your code and show title and text in notification.
For example:
override fun onMessageReceived(remoteMessage: RemoteMessage?) {
Log.d("FirebaseService", "Receive notification: ${remoteMessage?.data}")
remoteMessage?.let { showNotification(remoteMessage) }
}
private fun showNotification(remoteMessage: RemoteMessage) {
val notificationModel: NotificationModel
= getNotificationModelFromMessageData(remoteMessage.data)
val intent = Intent(this, SplashActivity::class.java)
intent.putExtra(NOTIFICATION_ARGUMENT, notificationModel)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent = PendingIntent.getActivity(
this, NOTIFICATION_RECEIVE_REQUEST_CODE,
intent, PendingIntent.FLAG_ONE_SHOT)
val defaultNotificationSound: Uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notification: Notification
= NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setContentTitle(notificationModel.title)
.setContentText(notificationModel.text)
.setSmallIcon(R.mipmap.ic_launcher)
.setSound(defaultNotificationSound)
.build()
val notificationManager: NotificationManager
= getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(NOTIFICATION_ID, notification)
}
private fun getNotificationModelFromMessageData(jsonData: MutableMap<String, String>): NotificationModel {
return NotificationModel(
jsonData[TITLE_PARAMETER] as String,
jsonData[TEXT_PARAMETER] as String,
jsonData[DAYS_PARAMETER] as String,
jsonData[MESSAGE_ID] as String)
}
Hope it help!

eugene
- 86
- 2
-
Thanks for reply I am getting data from the FCM like this {Key=value} in the onMessageReceived how to achieve the format of JSON which you have shown.. – Vishvendu Palawat Mar 24 '17 at 06:25
-
Finally I solved my problem Thanks It was because I was not using any REST client so format of data was {key = value} which was basically works when app in foreground – Vishvendu Palawat Mar 24 '17 at 07:25
1
In your onMessageReceived()
method, you can try adding your startActivity(intent)
code. That way, when the app receives a FCM message, it launches the app. Like this...
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
startActivity(new Intent(this, MainActivity.class));
}
}

vickmwas
- 56
- 5
-
Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Getting this error – Vishvendu Palawat Mar 23 '17 at 13:02
-
1onMessageReceived doesn't call when app is background so this will anyways not helping me – Vishvendu Palawat Mar 23 '17 at 13:43
-
Thanks for reply man even your thread also the part of my solution :) – Vishvendu Palawat Mar 24 '17 at 07:29
-
0
the user has to accept the permission of SYSTEM_ALERT_WINDOW
here is a link to the solution
SYSTEM_ALERT_WINDOW - How to get this permission automatically on Android 6.0 and targetSdkVersion 23