从后台接收通知(推送通知)时不显示直接回复

问题描述 投票:1回答:1

我正在开发一个基于聊天的应用程序,并且我尝试实现通知的直接回复,但是它只能在前台使用,当我收到来自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())
}

ChatReceiver

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>

ForgroundBackground

android push-notification firebase-cloud-messaging chat
1个回答
0
投票

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

{“信息”:{“令牌”:“ bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1 ...”,“数据”:{“ Nick”:“ Mario”,“ body”:“非常匹配!”,“ Room”:“ PortugalVSDenmark”}}}

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

© www.soinside.com 2019 - 2024. All rights reserved.