我已经使用Firebase Cloud Messaging实现了推送通知,除了几个自定义问题之外,其他所有功能都正常运行:
当我从Notification Composer工具发送测试通知时,清单中设置的小图标就很好了。 但是,如果我收到一个“有机”通知,该通知是通过应用程序中的实际操作生成的,则小图标只是一个灰色的圆形圆圈。 我已经在menifset和“通知”构建器功能中设置了图标。
无论通知是从作曲家发送的,还是有机发送的通知,收到通知时都不会发出声音,也不会产生振动。 用户只有检查托盘才能知道。 我根本没有尝试过自定义此功能,只是默认设置不起作用。
这是我用来构建通知的代码:
private fun sendNotification(messageBody: String) {
val intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT)
val channelId = getString(R.string.new_reservations_notification_channel_id)
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_logo)
.setContentTitle(getString(R.string.new_order))
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
val notificationManager = getSystemService(Application.NOTIFICATION_SERVICE) as NotificationManager is this a good context? "Appplication? It was "Context" before and unresolved
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_HIGH)
channel.lightColor = Color.GRAY
channel.enableLights(true)
channel.description = "Notifications for new reservations"
val audioAttributes = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build()
channel.setSound(defaultSoundUri, audioAttributes)
notificationManager.createNotificationChannel(channel)
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build())
}
我的清单中有我的应用程序标签中的内容:
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_logo" />