【发布时间】:2020-04-14 22:18:17
【问题描述】:
我正在尝试实现一个接收 firebase 通知的类,如果“image”键存在,也显示它。它按预期接收通知,但没有图像。如果密钥“图像”存在与否,我尝试在控制台中输出,但没有输出任何内容。如果有人可以提供帮助,我会很高兴。
相关部分代码如下:
[Service]
[IntentFilter(new[] {
"com.google.firebase.MESSAGING_EVENT"
})]
class MyFirebaseMessagingService : FirebaseMessagingService
{
public override void OnMessageReceived(RemoteMessage message)
{
base.OnMessageReceived(message);
SendNotifications(message.GetNotification().Body, message.GetNotification().Title,message.Data);
}
public void SendNotifications(string body, string Header, IDictionary<string, string> data)
{
Notification.Builder builder = new Notification.Builder(this);
builder.SetSmallIcon(Resource.Drawable.favicon96_96);
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
PendingIntent pendingIntent = PendingIntent.GetActivity(this, 0, intent, 0);
builder.SetContentIntent(pendingIntent);
builder.SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.favicon96_96));
builder.SetContentTitle(Header);
builder.SetContentText(body);
builder.SetDefaults(NotificationDefaults.Sound);
builder.SetAutoCancel(true);
if (data.ContainsKey("image"))
{
Console.WriteLine("There is an image => "+data["image"]);
var urlString = data["image"].ToString();
var url = new URL(urlString);
var connection = (HttpURLConnection)url.OpenConnection();
connection.DoInput = true;
connection.Connect();
var input = connection.InputStream;
var bitmap = BitmapFactory.DecodeStream(input);
Notification.BigPictureStyle picStyle = new Notification.BigPictureStyle();
picStyle.BigPicture(bitmap);
picStyle.SetSummaryText(body);
connection.Dispose();
builder.SetStyle(picStyle);
}
else
{
Console.WriteLine("====== THERE ISNT ANY IMAGE ");
}
NotificationManager notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.Notify(1, builder.Build());
【问题讨论】:
-
请尝试将Resource中的本地图片设置为`Notification.BigPictureStyle`,是否可以正常显示?
-
@LeonLu-MSFT 刚刚尝试过,但仍然是同样的问题。
-
我用我的项目测试它。它正常工作。请看这个GIF(我用本地图片做一个测试)。imgur.com/a/rC6dGhP,我的
FirebaseMessagingService代码像这个线程github.com/851265601/XAndroidFCMNotification/blob/master/… -
您是否尝试使用我的代码加载此本地图像?这行得通吗?
-
@LeonLu-MSFT 我无法使用完全相同的代码,因为 Visual Studio 说 MainActivity 不包含属性 NOTIFICATION_ID 和 CHANNEL_ID 的定义。所以我将 NotificationID 更改为 0 并删除了 Channel_ID。没有成功,它一直显示没有图像的通知。重要的是,我将图像更改为现有图像。
标签: android firebase xamarin push-notification firebase-cloud-messaging