How to receive messages from firebase console in android in the background?

1

I have an app that sends notifications from the firebase console and my notifications do not arrive on the phone when they are in the background but when they are in the foreground with the app open the notification arrives normally. I would appreciate your support in solving my problem.

package br.com.guiacistore

import android.app.NotificationManager
import android.app.PendingIntent
import android.app.PendingIntent.getActivity
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.media.RingtoneManager
import android.os.Bundle
import android.support.v4.app.NotificationCompat
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage

/**
 * Created by faro on 9/7/17.
 */

class NotificationFirebaseMessagingService : FirebaseMessagingService() {

    override fun onMessageReceived(remoteMessage: RemoteMessage?) {
        if (remoteMessage?.notification != null) {
          sendNotifications(remoteMessage?.notification?.body, remoteMessage.data)
        }

    //        if (remoteMessage?.notification != null) {
    //            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    //        }

    }


    private fun sendNotifications(mensagem: String?, dados: Map<String, String>) {


        val intent = Intent(this, StorageActivity::class.java)
        val bundle = Bundle()

        for (key in dados.keys) {
          val valor = dados[key].toString()
          bundle.putString(key, valor)

        }


        intent.putExtras(bundle)

        intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
        val pendingIntent = getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)
        val uriSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
        val notificationCompatBuilder = NotificationCompat.Builder(this)


        notificationCompatBuilder.setSmallIcon(R.mipmap.guiaci)
        notificationCompatBuilder.setContentTitle("Guiaci")
        notificationCompatBuilder.setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.guiaci))

        notificationCompatBuilder.setContentText(mensagem)
        notificationCompatBuilder.setAutoCancel(true)
        notificationCompatBuilder.setContentIntent(pendingIntent)
        notificationCompatBuilder.setSound(uriSound)

        notificationCompatBuilder.priority = NotificationManager.IMPORTANCE_HIGH
        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.notify(0, notificationCompatBuilder.build())

    }

}
    
asked by anonymous 15.06.2018 / 13:47

0 answers