由于当应用程序同时处于后台和前台时,我必须让推送通知按要求工作,所以我在测试时(2 天内)执行了30-40 条通知,所有通知在到达 android 设备时都正常(我的自定义通知通道也是按预期出现在设备设置中)。
突然,通知继续到达但没有声音(并且自定义通知通道不会像以前那样出现在设置中)。既然这样,我就不可能在通知中恢复声音(无论是背景还是前景)。
没有更改任何涉及的代码。我认为声音已经停止,因为由于某种原因没有创建通知通道。有没有人经历过或者可以帮助我?
案例“后台应用”的关键代码:
1. 表现。
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="my_fcm_default_channel" />
2. 启动活动 - onCreate():
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Create channel to show notifications.
val channelName = "Custom notification channel"
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationManager = getSystemService(NotificationManager::class.java)
val channel = NotificationChannel("my_fcm_default_channel",
channelName,
NotificationManager.IMPORTANCE_HIGH)
channel.setSound(defaultSoundUri, AudioAttributes.Builder().build()) // Not working
channel.enableLights(true)
channel.lightColor = Color.WHITE
channel.enableVibration(true)
channel.vibrationPattern = longArrayOf(100, 200, 100, 200, 100, 200, 100)
notificationManager.createNotificationChannel(channel)
}
3.云函数node.js片段代码:
// Notification details.
const payload = {
notification: {
title: 'Some title',
body: 'Some message',
sound: 'default',
click_action: "OPEN_ACTIVITY_3"
},
data: {
name: 'notification_3'
}
};
更新:
案例“前台应用程序”的关键代码:
1. MyFirebaseMessagingService - onMessageReceived():
val name = remoteMessage.data["name"] ?: ""
var intent: Intent? = null
when (name) {
"notification_1" -> {
intent = Intent(this, Activity1::class.java)
}
"notification_2" -> {
intent = Intent(this, Activity2::class.java)
}
"notification_3" -> {
val clickAction = remoteMessage.notification?.clickAction
clickAction?.let {
intent = Intent(clickAction)
intent?.putExtra("name", name)
}
}
"notification_4" -> {
intent = Intent(this, Activity4::class.java)
}
}
intent?.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT)
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(this, "my_fcm_default_channel")
.setSmallIcon(R.drawable.common_google_signin_btn_icon_dark) // Dummy icon
.setContentTitle(remoteMessage.notification?.title ?: "")
.setContentText(remoteMessage.notification?.body ?: "")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setVibrate(longArrayOf(100, 200, 100, 200, 100, 200, 100))
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_ALL) // this line sets the default vibration and sound for notification
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel("my_fcm_default_channel",
"Custom notification channel",
NotificationManager.IMPORTANCE_HIGH)
channel.setSound(defaultSoundUri, AudioAttributes.Builder().build()) // Not working
channel.enableLights(true)
channel.lightColor = Color.WHITE
channel.enableVibration(true)
channel.vibrationPattern = longArrayOf(100, 200, 100, 200, 100, 200, 100)
notificationManager.createNotificationChannel(channel)
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build())