Reputation: 101
I want to send push notification to Android app from Postman. My settings are:
Sending notification works right and Postman shows me “{"message_id":6108453121985358090}” (example). However, Android app doesn’t receive the push notification. In the meantime, if I send notifications from the Firebase console everything will work fine.
My code from FirebaseMessagingService:
class MyFirebaseMessagingService : FirebaseMessagingService() {
private val CURRENT_PUSH = "currentPush"
private var sPref: SharedPreferences? = null
//var preferences: SharedPreferences? = null
override fun onMessageReceived(remoteMessage: RemoteMessage?) {
super.onMessageReceived(remoteMessage)
if(loadCurrentPushNotification()) {
if (remoteMessage!!.data != null)
sendNotification(remoteMessage)
}
}
private fun sendNotification(remoteMessage: RemoteMessage) {
val data = remoteMessage.data
val title = data["title"]
val content = data["content"]
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val NOTIFICATION_CHANNEL_ID = "1234"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@SuppressLint("WrongConstant") val notificationChannel = NotificationChannel(NOTIFICATION_CHANNEL_ID,
"SA Notification",
NotificationManager.IMPORTANCE_MAX)
notificationChannel.description = "SA channel notification"
notificationChannel.enableLights(true)
notificationChannel.lightColor = Color.RED
notificationChannel.vibrationPattern = longArrayOf(0, 1000, 500, 1000)
notificationChannel.enableVibration(true)
notificationManager.createNotificationChannel(notificationChannel)
}
val notificationBuilder = NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.sa_launcher)
.setContentTitle(title)
.setContentText(content)
.setContentInfo("Breaking")
notificationManager.notify(1, notificationBuilder.build())
}
override fun onNewToken(s: String?) {
Log.i(C.T, "NEW_TOKEN" + s!!)
}
private fun loadCurrentPushNotification(): Boolean { //to read the status push notification from SharedPreferences
sPref = getSharedPreferences("pushesOnOff", Context.MODE_PRIVATE)
return sPref!!.getBoolean(CURRENT_PUSH, true)
}
}
Where did I make a mistake?
Upvotes: 1
Views: 555
Reputation: 301
This will only work when your app is in background. If you want notification to work in foreground then try sending payload in "data".
And make sure that you have subscribed to the topic to which you are sending notification
Upvotes: 0
Reputation: 416
Have you tried to use a specific device's token id in your "to" field? You can also try to move the data payload to under the data tag (try with and without the "notification" part), i.e.:
"notification" : {
"body" : "Generic message body",
"title": "Generic title"
},
"data" : {
"body" : "Custom body",
"title": "Custom title",
"content" : "Your custom content"
}
Upvotes: 1