Postman 工作正常,但 Android 应用没有收到通知
Postman work correctly but Android app don’t receive notification
我想从 Postman 向 Android 应用程序发送推送通知。我的设置是:
发送通知正常,Postman 显示“{"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" 部分),即:
"notification" : {
"body" : "Generic message body",
"title": "Generic title"
},
"data" : {
"body" : "Custom body",
"title": "Custom title",
"content" : "Your custom content"
}
这仅在您的应用处于后台时有效。如果您希望通知在前台工作,请尝试在 "data".
中发送有效负载
并确保您已订阅要发送通知的主题
我想从 Postman 向 Android 应用程序发送推送通知。我的设置是:
发送通知正常,Postman 显示“{"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" 部分),即:
"notification" : {
"body" : "Generic message body",
"title": "Generic title"
},
"data" : {
"body" : "Custom body",
"title": "Custom title",
"content" : "Your custom content"
}
这仅在您的应用处于后台时有效。如果您希望通知在前台工作,请尝试在 "data".
中发送有效负载并确保您已订阅要发送通知的主题