gpt4 book ai didi

xamarin - 收到通过 xamarin.forms 中的 FCM 发送的通知,但图像未显示在系统托盘中

转载 作者:行者123 更新时间:2023-12-02 03:01:05 25 4
gpt4 key购买 nike

我在 FCM 上设置了一个应用程序,用于在 Xamarin.Forms 中向 Android 设备发送通知,但仅收到通知,但图像不显示在系统托盘中。

这是我的 AndroidManifest.xml

<application android:label="DMSMobileApp.Android">
<receiver
android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver"
android:exported="false" />
<receiver
android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="${applicationId}" />
</intent-filter>
</receiver>
</application>

我创建了 FirebaseMessagingService.cs 来接收通知并将其转换为本地通知。

[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MyFirebaseMessagingService : FirebaseMessagingService
{
const string TAG = "MyFirebaseMsgService";
public override void OnMessageReceived(RemoteMessage message)
{
var body = message.GetNotification().Body;
Log.Debug(TAG, "Notification Message Body: " + body);
SendNotification(body, message.Data);
//new NotificationHelper().CreateNotification(message.GetNotification().Title, message.GetNotification().Body);
}
void SendNotification(string messageBody, IDictionary<string, string> data)
{
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
foreach (var key in data.Keys)
{
intent.PutExtra(key, data[key]);
}

var pendingIntent = PendingIntent.GetActivity(this,
MainActivity.NOTIFICATION_ID,
intent,
PendingIntentFlags.OneShot);


var notificationBuilder = new NotificationCompat.Builder(this, MainActivity.CHANNEL_ID)
.SetSmallIcon(Resource.Drawable.notification_template_icon_bg)
.SetContentTitle("FCM Message")
.SetContentText(messageBody)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);

var notificationManager = NotificationManagerCompat.From(this);
notificationManager.Notify(MainActivity.NOTIFICATION_ID, notificationBuilder.Build());
}
}

通过 REST API,我正在发送通知。

var data = new
{
to = "/topics/ALL", // Recipient device token
notification = new {
title = "Test",
body = "Message",
image = "https://cdn.pixabay.com/photo/2015/05/15/14/38/computer-768608_960_720.jpg"
},
image = "https://cdn.pixabay.com/photo/2015/05/15/14/38/computer-768608_960_720.jpg"
};
var jsonBody = JsonConvert.SerializeObject(data);
bool fcmState;
using (var httpRequest = new HttpRequestMessage(HttpMethod.Post, "https://fcm.googleapis.com/fcm/send"))
{
httpRequest.Headers.TryAddWithoutValidation("Authorization", serverKey);
httpRequest.Headers.TryAddWithoutValidation("Sender", senderId);
httpRequest.Content = new StringContent(jsonBody, Encoding.UTF8, "application/json");

using (var httpClient = new HttpClient())
{
var result = await httpClient.SendAsync(httpRequest);

if (result.IsSuccessStatusCode)
{
fcmState = true;
}
else
{
// Use result.StatusCode to handle failure
// Your custom error handler here
//_logger.LogError($"Error sending notification. Status Code: {result.StatusCode}");
}
}
}
  1. 我能够在后台接收通知,但在前台,我只能收到声音,但系统托盘中没有通知。
  2. 当应用在后台运行时,我在系统托盘中收到通知,但没有收到图像。

最佳答案

我刚刚通过在从 FCM 接收图像数据时下载图像来解决这个问题。这是我的代码。

void SendNotification(string messageBody, IDictionary<string, string> data, RemoteMessage message)
{
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
foreach (var key in data.Keys)
{
intent.PutExtra(key, data[key]);
}

var pendingIntent = PendingIntent.GetActivity(this,
MainActivity.NOTIFICATION_ID,
intent,
PendingIntentFlags.OneShot);

//var test = message.GetNotification().Body;
string title = data["title"];
string body = data["body"];
string imageReceived = data["image"]; //It contains image URL.
GetImageBitmapFromUrl(imageReceived); // This method will download image from URL.
var notificationBuilder = new NotificationCompat.Builder(this, MainActivity.CHANNEL_ID)
.SetSmallIcon(Resource.Drawable.logo)
.SetContentTitle(title)
.SetContentText(body)
.SetStyle(new NotificationCompat.BigPictureStyle().BigPicture(imageBitmap)) //// This will show the image in system tray.
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);

var notificationManager = NotificationManagerCompat.From(this);
notificationManager.Notify(MainActivity.NOTIFICATION_ID, notificationBuilder.Build());
}

如果 Data IDictionary 中的“image”键中有链接,则下载图像的代码。

Bitmap imageBitmap = null;
Bitmap roundedImage = null;
public Bitmap GetImageBitmapFromUrl(string url)
{
using (var webClient = new System.Net.WebClient())
{
var imageBytes = webClient.DownloadData(url);
if (imageBytes != null && imageBytes.Length > 0)
{
imageBitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
roundedImage = Bitmap.CreateScaledBitmap(imageBitmap, 300, 300, false);
//roundedImage = getRoundedShape(resizedImage);
}
webClient.Dispose();
}
return roundedImage;
}

我通过 REST API 发送到 FCM 的数据。感谢@Harikrishnan,首先我使用了通知对象,它有效,但其中没有图像数据。

var data = new
{
to = "/topics/ALL", // Recipient device token
data = new {
title = "Test",
body = "Message",
image = "https://cdn.pixabay.com/photo/2015/05/15/14/38/computer-768608_960_720.jpg"
},
};

关于xamarin - 收到通过 xamarin.forms 中的 FCM 发送的通知,但图像未显示在系统托盘中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59964111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com