3

I want to launch an app automatically when user receive notification on app there will be no click action on launcher icon.

Vishvendu Palawat
  • 566
  • 10
  • 29

3 Answers3

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
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