是否可以在 boardcastReceiver 中识别通知详细信息(或通知 ID)?
Is it possible to identity notification details (or notification id) inside boardcastReceiver?
我有多个通知由不同的接收者(供应商)发送。我实现了一个回复操作按钮来回复消息。目前,我在本地存储通知详细信息(共享首选项)。现在,回复操作按钮适用于单个接收者通知。是否可以在 boardcastReceiver 中识别通知详细信息(或通知 ID)?
可以从这里到达吗?
override fun onReceive(context: Context, intent: Intent?) {
val remoteInput: Bundle? = RemoteInput.getResultsFromIntent(intent)
val contentString = remoteInput.getCharSequence("NotificationReply").toString()
完整代码
消息服务
class MyFirebaseMessagingService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
if (remoteMessage.data.isNotEmpty()) {
sendNotification(NotificationModel.from(remoteMessage.data))
}
}
private fun sendNotification(notificationModel: NotificationModel) {
val channelId = getString(R.string.default_notification_channel_id)
val intent = Intent(this, MainActivity::class.java)
val sharedPreferences: SharedPreferences = this.getSharedPreferences("yellochatspfile", Context.MODE_PRIVATE)
val editor: SharedPreferences.Editor = sharedPreferences.edit()
editor.putString("message_data", notificationModel.messageData)
editor.apply()
editor.commit()
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
PendingIntent.getBroadcast(
this,
MainActivity.REQUEST_CODE_APPROVE,
Intent(this, NotificationReceiver::class.java)
.putExtra(MainActivity.KEY_INTENT_APPROVE, MainActivity.REQUEST_CODE_APPROVE),
PendingIntent.FLAG_UPDATE_CURRENT
)
val notificationBuilder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.yc_logo)
.setContentTitle(notificationModel.title)
.setContentText(notificationModel.body)
.setGroup(notificationModel.groupKey)
.setAutoCancel(true)
.setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.yc_logo))
.setSound(defaultSoundUri)
.setPriority(2)
.setPriority(1)
.setDefaults(2)
.setContentIntent(pendingIntent)
val replyLabel = "Enter your reply here"
val remoteInput: RemoteInput = RemoteInput.Builder(MainActivity.NOTIFICATION_REPLY)
.setLabel(replyLabel)
.build()
val action: NotificationCompat.Action =
NotificationCompat.Action.Builder(android.R.mipmap.sym_def_app_icon,
"Reply", pendingIntent)
.addRemoteInput(remoteInput)
.build()
notificationBuilder.addAction(action)
var notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.cancel(MainActivity.TAG_VALIDATION, 1)
MainActivity.NOTIFICATION_ID = r.nextInt()
notificationManager.notify(MainActivity.TAG_NORMAL, MainActivity.NOTIFICATION_ID, notificationBuilder.build())
}
}
广播接收器
class NotificationReceiver : BroadcastReceiver() {
val db = Firebase.firestore
override fun onReceive(context: Context, intent: Intent?) {
val remoteInput: Bundle? = RemoteInput.getResultsFromIntent(intent)
if (remoteInput != null) {
val contentString = remoteInput.getCharSequence("NotificationReply").toString()
val sharedPreferences: SharedPreferences = context.getSharedPreferences("yellochatspfile", Context.MODE_PRIVATE)
val messageData = sharedPreferences.getString("message_data", "")
if (messageData != null && messageData != "") {
// some logics to send message
}else{
notificationProcess(sharedPreferences, false, context)
}
}else{
notificationProcess(sharedPreferences, false, context)
}
}
}
private fun notificationProcess(sharedPreferences: SharedPreferences, isSuccess: Boolean, context: Context) {
}
}
我用 putExtra
与 broadcastReceiver
共享数据
消息服务
val i = Intent(this, NotificationReceiver::class.java)
i.putExtra("MESSAGE_DATA", notificationModel.messageData)
PendingIntent.getBroadcast(
this,
MainActivity.REQUEST_CODE_APPROVE,
i,
PendingIntent.FLAG_UPDATE_CURRENT
)
广播接收器
val messageData = intent?.getStringExtra("MESSAGE_DATA")
println("messageData is : $messageData")
我有多个通知由不同的接收者(供应商)发送。我实现了一个回复操作按钮来回复消息。目前,我在本地存储通知详细信息(共享首选项)。现在,回复操作按钮适用于单个接收者通知。是否可以在 boardcastReceiver 中识别通知详细信息(或通知 ID)?
可以从这里到达吗?
override fun onReceive(context: Context, intent: Intent?) {
val remoteInput: Bundle? = RemoteInput.getResultsFromIntent(intent)
val contentString = remoteInput.getCharSequence("NotificationReply").toString()
完整代码
消息服务
class MyFirebaseMessagingService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
if (remoteMessage.data.isNotEmpty()) {
sendNotification(NotificationModel.from(remoteMessage.data))
}
}
private fun sendNotification(notificationModel: NotificationModel) {
val channelId = getString(R.string.default_notification_channel_id)
val intent = Intent(this, MainActivity::class.java)
val sharedPreferences: SharedPreferences = this.getSharedPreferences("yellochatspfile", Context.MODE_PRIVATE)
val editor: SharedPreferences.Editor = sharedPreferences.edit()
editor.putString("message_data", notificationModel.messageData)
editor.apply()
editor.commit()
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
PendingIntent.getBroadcast(
this,
MainActivity.REQUEST_CODE_APPROVE,
Intent(this, NotificationReceiver::class.java)
.putExtra(MainActivity.KEY_INTENT_APPROVE, MainActivity.REQUEST_CODE_APPROVE),
PendingIntent.FLAG_UPDATE_CURRENT
)
val notificationBuilder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.yc_logo)
.setContentTitle(notificationModel.title)
.setContentText(notificationModel.body)
.setGroup(notificationModel.groupKey)
.setAutoCancel(true)
.setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.yc_logo))
.setSound(defaultSoundUri)
.setPriority(2)
.setPriority(1)
.setDefaults(2)
.setContentIntent(pendingIntent)
val replyLabel = "Enter your reply here"
val remoteInput: RemoteInput = RemoteInput.Builder(MainActivity.NOTIFICATION_REPLY)
.setLabel(replyLabel)
.build()
val action: NotificationCompat.Action =
NotificationCompat.Action.Builder(android.R.mipmap.sym_def_app_icon,
"Reply", pendingIntent)
.addRemoteInput(remoteInput)
.build()
notificationBuilder.addAction(action)
var notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.cancel(MainActivity.TAG_VALIDATION, 1)
MainActivity.NOTIFICATION_ID = r.nextInt()
notificationManager.notify(MainActivity.TAG_NORMAL, MainActivity.NOTIFICATION_ID, notificationBuilder.build())
}
}
广播接收器
class NotificationReceiver : BroadcastReceiver() {
val db = Firebase.firestore
override fun onReceive(context: Context, intent: Intent?) {
val remoteInput: Bundle? = RemoteInput.getResultsFromIntent(intent)
if (remoteInput != null) {
val contentString = remoteInput.getCharSequence("NotificationReply").toString()
val sharedPreferences: SharedPreferences = context.getSharedPreferences("yellochatspfile", Context.MODE_PRIVATE)
val messageData = sharedPreferences.getString("message_data", "")
if (messageData != null && messageData != "") {
// some logics to send message
}else{
notificationProcess(sharedPreferences, false, context)
}
}else{
notificationProcess(sharedPreferences, false, context)
}
}
}
private fun notificationProcess(sharedPreferences: SharedPreferences, isSuccess: Boolean, context: Context) {
}
}
我用 putExtra
与 broadcastReceiver
消息服务
val i = Intent(this, NotificationReceiver::class.java)
i.putExtra("MESSAGE_DATA", notificationModel.messageData)
PendingIntent.getBroadcast(
this,
MainActivity.REQUEST_CODE_APPROVE,
i,
PendingIntent.FLAG_UPDATE_CURRENT
)
广播接收器
val messageData = intent?.getStringExtra("MESSAGE_DATA")
println("messageData is : $messageData")