我希望从 firebase notification 接收大量消息,并在下载消息时编辑其内容。 主要是关于紧身衣。例如。 内容:“你好,#Name” 然后替换 #Name 值。
public override void OnMessageReceived(RemoteMessage message)
{
message.GetNotification().Body = "";
base.OnMessageReceived(message);
if (message.Data.TryGetValue("action", out var messageAction))
NotificationActionService.TriggerAction(messageAction);
}
这可能吗?也许有更好的群发通知解决方案,您可以分享一下您的经验。 请提供代码示例。谢谢您并致以诚挚的问候。
发送数据消息时,在消息正文中包含带有占位符的键值对,例如:
{
"to": "<device_token>",
"data": {
"title": "Greetings",
"body": "Hello, #Name",
"name": "John" // You can provide dynamic values for placeholders
}
}
Android 客户端(使用 FirebaseMessagingService):
public override void OnMessageReceived(RemoteMessage message)
{
// Ensure the message contains data payload
if (message.Data.ContainsKey("body"))
{
// Get the notification body with placeholders
string body = message.Data["body"];
// Replace placeholders in the body (e.g., replace #Name with the actual name from the data)
if (message.Data.ContainsKey("name"))
{
string name = message.Data["name"];
body = body.Replace("#Name", name);
}
// Build and display the notification with the customized message body
ShowCustomNotification(message.Data["title"], body);
}
// Trigger any additional actions if needed
if (message.Data.TryGetValue("action", out var messageAction))
{
NotificationActionService.TriggerAction(messageAction);
}
}
// Method to display the custom notification
private void ShowCustomNotification(string title, string body)
{
var notificationBuilder = new NotificationCompat.Builder(this, "default_channel_id")
.SetSmallIcon(Resource.Drawable.ic_stat_ic_notification) // Set your icon
.SetContentTitle(title)
.SetContentText(body)
.SetPriority(NotificationCompat.PriorityHigh)
.SetAutoCancel(true);
var notificationManager = NotificationManagerCompat.From(this);
notificationManager.Notify(0, notificationBuilder.Build());
}