1
I have an app that sends notifications from the firebase console and my notifications don’t arrive on my phone when they’re in the background but when they’re in the foreground with the app open the notification usually arrives. I would like your support to solve 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())
}
}
What data are you putting into the console? By default, when you send a message through the console, if the app is on background Firebase automatically displays notifications, so you wouldn’t need to implement the method
onMessageReceived()
. This method is only needed to handle messages when your app is on foreground– regmoraes
I put title and message, basically!
– Faro