首页 > 解决方案 > 收到后台通知时不显示直接回复(推送通知)

问题描述

我正在开发一个基于聊天的应用程序,我正在尝试实现通知的直接回复,但它仅在前台工作,当我收到来自 firebase 的推送通知时,它显示为正常通知,因为它在manifest.xml文件中配置,不像我们所知道的onMessageReceived函数。

通知功能

private fun sendNotification(notification: RemoteMessage.Notification) {
    val intent = Intent(this, PaymentActivity::class.java).apply {
        flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    }

    val pendingIntent = PendingIntent.getActivity(
        this,
        0 /* Request code */,
        intent,
        PendingIntent.FLAG_UPDATE_CURRENT
    )

    val chatIntent = Intent(applicationContext, ChatReceiver::class.java)
    val chatPendingIntent: PendingIntent = PendingIntent.getBroadcast(
        applicationContext,
        CONVERSATION_ID,
        chatIntent,
        PendingIntent.FLAG_UPDATE_CURRENT)

    var replyLabel = "Replay to your elf."
    var remoteInput = RemoteInput.Builder(KEY_TEXT_REPLY).run {
        setLabel(replyLabel)
        build()
    }

    var action: NotificationCompat.Action =
        NotificationCompat.Action.Builder(
            R.drawable.ic_reply_24dp,
            replyLabel,
            chatPendingIntent
        )
            .addRemoteInput(remoteInput)
            .setAllowGeneratedReplies(true)
            .build()
    val wearableExtender = NotificationCompat.WearableExtender().addAction(action)

    val notificationBuilder = NotificationCompat.Builder(this, CHAT_CHANNEL_ID).apply {
        setSmallIcon(R.drawable.ic_logo_blue)
        setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.ic_logo_blue))
        setContentTitle(notification.title)
        setContentText(notification.body)
        setAutoCancel(true)
        priority = NotificationCompat.PRIORITY_HIGH
        addAction(action)
        setContentIntent(pendingIntent)
        setGroup(GROUP_KEY_CHAT)
        extend(wearableExtender)
        if (notification.channelId != null) {
            setChannelId(notification.channelId!!)
        } else {
            setChannelId(CHAT_CHANNEL_ID)
        }
    }

    val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build())
}

聊天接收者

class ChatReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        context?.toast("Got it.")

        ContextCompat.getSystemService(context!!, NotificationManager::class.java)?.cancelAll()
    }
}

AndroidManifest.xml

...
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/ic_internal_notification" />
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/colorAccent" />

        <service android:name=".common.utils.MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />    
            </intent-filter>
        </service>
        <receiver
            android:name=".common.receiver.ChatReceiver"
            android:enabled="true"
            android:exported="true">

        </receiver>
    </application>

</manifest>

前景 背景

标签: androidpush-notificationfirebase-cloud-messagingchat

解决方案


数据消息 使用您的自定义键值对设置适当的键,以将数据负载发送到客户端应用程序。例如,这是与上述相同的 IM 应用程序中的 JSON 格式的消息,其中信息被封装在公共数据中键和客户端应用程序应解释内容:

{“消息”:{“令牌”:“bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1 ...”,“数据”:{“尼克”:“马里奥”,“身体”:“伟大的比赛!”,“房间”:“葡萄牙VSDenmark”} } }

有关详细信息,请访问 https://firebase.google.com/docs/cloud-messaging/concept-options#notification-messages-with-optional-data-payload


推荐阅读