android - 邮递员工作正常,但 Android 应用程序没有收到通知
问题描述
我想从 Postman 向 Android 应用发送推送通知。我的设置是:
发送通知工作正常,邮递员向我显示“{“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)
}
}
我在哪里做错了?
解决方案
您是否尝试在“to”字段中使用特定设备的令牌 ID?您还可以尝试将数据有效负载移动到数据标签下(尝试使用和不使用“通知”部分),即:
"notification" : {
"body" : "Generic message body",
"title": "Generic title"
},
"data" : {
"body" : "Custom body",
"title": "Custom title",
"content" : "Your custom content"
}
推荐阅读
- bash - bash while read 循环在第一行之后停止(根据 StackExchange 帖子进行更正后)
- c++ - 如何在 C++ 中获取开始时间和 now() 之间的毫秒差?
- node.js - 我使用的是哪个版本的节点?
- excel - 当工作表受到保护时,不应标记/取消标记复选框
- c# - 以下方法或属性错误之间的调用不明确-c#visual studio 2019
- python - 精灵在一定的距离限制内遵循另一种灵活的方式
- c - 如何“暂停”应用程序让用户按下按钮?GTK
- c++ - 如何在我的 C++ 程序中使用下标数字?
- corda - Corda:如何在 valult DB 中将即时状态变量保存为 UTC?
- r - rbind(x, y) 与 rbind(y, x)