1

I want to send push notification to Android app from Postman. My settings are:

Pic1 Pic2

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?

Vladimir Gladun
  • 416
  • 2
  • 16
Yevgen
  • 93
  • 1
  • 1
  • 8

2 Answers2

1

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"
 }
Vladimir Gladun
  • 416
  • 2
  • 16
  • Its really works if to specify token of my device. Thank you! But what need to do that all devices with installed app can receive push? What to specify in "to"? – Yevgen Nov 20 '18 at 08:01
  • To send to multiple devices you can either use "registration_ids" with multiple IDs instead of "to". Or use a topic (as you did in your example), but you should also do FirebaseMessaging.getInstance().subscribeToTopic("allDevices") in your app to receive it. – Vladimir Gladun Nov 20 '18 at 08:15
0

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

ahuja007
  • 281
  • 2
  • 14