Я работаю над приложением, основанным на чате, и пытаюсь реализовать прямой ответ из уведомления, но оно работает только на переднем плане, когда я получаю уведомление pu sh от 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>