首页 > 解决方案 > 邮递员工作正常,但 Android 应用程序没有收到通知

问题描述

我想从 Postman 向 Android 应用发送推送通知。我的设置是:

图1 图2

发送通知工作正常,邮递员向我显示“{“message_id”:6108453121985358090}”(示例)。但是,Android 应用程序不会收到推送通知。与此同时,如果我从 Firebase 控制台发送通知,一切都会正常工作。

我来自 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)
}

}

我在哪里做错了?

标签: androidfirebasepostmanandroid-push-notification

解决方案


您是否尝试在“to”字段中使用特定设备的令牌 ID?您还可以尝试将数据有效负载移动到数据标签下(尝试使用和不使用“通知”部分),即:

"notification" : {
     "body" : "Generic message body",
     "title": "Generic title"
 },
 "data" : {
     "body" : "Custom body",
     "title": "Custom title",
     "content" : "Your custom content"
 }

推荐阅读