接收推送消息时如何设置通知头?

时间:2020-03-12 05:09:22

标签: android push-notification firebase-cloud-messaging android-notifications

我在android中收到推送消息时正在开发通知。所以我这样写url。但我看不到通知标题框。如何解决此代码?我可以在状态栏上看到小图标,但看不到抬头框

清单

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT"/>

FirebaseService

class FirebaseMessagingService : FirebaseMessagingService() {

    override fun onNewToken(token: String) {
        Log.d("TAG", "Refreshed token: $token")

    }

    override fun onMessageReceived(remoteMessage: RemoteMessage) {

        Log.d("TAG", "From: " + remoteMessage.from!!)

        val messageBody = remoteMessage.notification?.body
        val messageTitle = remoteMessage.notification?.title
        val intent = Intent(this, LoginActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        val pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT)
        val channelId ="1000"
        val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
        val inboxStyle = NotificationCompat.InboxStyle()

        val notificationBuilder = NotificationCompat.Builder(this,channelId)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(messageTitle)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setOngoing(true)
            .setSound(defaultSoundUri)
            //.setContentIntent(pendingIntent)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.haii))
            .setColor(getResources().getColor(R.color.colorPrimary))
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setDefaults(NotificationCompat.DEFAULT_ALL)
            .setStyle(inboxStyle)
            .setFullScreenIntent(pendingIntent,true)


        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            val channelName ="Channel Name"

            val channel = NotificationChannel(channelId,channelName,NotificationManager.IMPORTANCE_HIGH)
            channel.enableLights(true)
            channel.lightColor= 0x00FFFF
            channel.setShowBadge(false)
            notificationManager.createNotificationChannel(channel)
        }
        notificationManager.notify(0,notificationBuilder.build())



    }

}

0 个答案:

没有答案