firebase - FCM 前台通知 Java.Lang.IllegalArgumentException 错误
问题描述
我能够收到消息,但只显示在日志中。我想在屏幕顶部显示为通知。
这是我的代码:MainActivity.cs
static readonly string TAG = "MainActivity";
internal static readonly string CHANNEL_ID = "my_notification_channel";
internal static readonly int NOTIFICATION_ID = 100;
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
UserDialogs.Init(this);
CreateNotificationChannel();
CrossMedia.Current.Initialize();
Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity = this;
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App(FirebaseInstanceId.Instance.Token));
}
void CreateNotificationChannel()
{
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
{
// Notification channels are new in API 26 (and not a part of the
// support library). There is no need to create a notification
// channel on older versions of Android.
return;
}
var channel = new NotificationChannel(CHANNEL_ID,
"FCM Notifications",
NotificationImportance.Default)
{
Description = "Firebase Cloud Messages appear in this channel"
};
var notificationManager = (NotificationManager)GetSystemService(Android.Content.Context.NotificationService);
notificationManager.CreateNotificationChannel(channel);
}
}
在MyFirebaseMessagingService
课堂上
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MyFirebaseMessagingService : FirebaseMessagingService
{
const string TAG = "MyFirebaseMsgService";
public override void OnMessageReceived(RemoteMessage message)
{
Log.Debug(TAG, "From: " + message.From);
var body = message.GetNotification().Body;
Log.Debug(TAG, "Notification Message Body: " + body);
SendNotification(body, message.Data);
}
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.ic_stat_ic_notification)
.SetContentTitle("FCM Message")
.SetContentText(messageBody)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
var notificationManager = NotificationManagerCompat.From(this);
notificationManager.Notify(MainActivity.NOTIFICATION_ID, notificationBuilder.Build());
}
}
我可以在这里收到消息:Log.Debug(TAG, "Notification Message Body: " + body);
它会记录消息的正文,但是在调用SendNotification
函数之后,然后在这一行:
var notificationManager = NotificationManagerCompat.From(this);
notificationManager.Notify(MainActivity.NOTIFICATION_ID, notificationBuilder.Build());
会有错误:Java.Lang.IllegalArgumentException: <Timeout exceeded getting exception details>
我在哪里可以获得价值CHANNEL_ID
和NOTIFICATION_ID
的价值?目前,它的值:my_notification_channel 和 100 分别与文档中的相同。这是我出现此错误的原因吗?
请帮忙。谢谢你。
解决方案
通知结构有问题
var notificationBuilder = new NotificationCompat.Builder(this, MainActivity.CHANNEL_ID)
.SetSmallIcon(Resource.Drawable.ic_stat_ic_notification) //you could not commen it
.SetContentTitle("FCM Message")
.SetContentText(messageBody)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
SetSmallIcon是必填参数,否则会生成错误
推荐阅读
- python - 如何根据其他列的条件分配新列?
- reactjs - React jsx 执行顺序:
- css - 禁用 matInput 时隐藏 mat-hint
- c - 读取 .csv 然后将其保存到 C 中的数组中
- c - 与 FreeRTOS 一起使用时,“utils/uartstdio.h”会出现未解决的符号错误
- c++ - 是否不允许使用与使用不同类型的设计选择访问数组的元素?
- oracle - 将 Cognos 特定行转换为 oracle sql
- c# - C# switch 表达式不正确的可空性
- docker - 如果采用 8888,则配置 Docker 以动态选择第一个可用的 Jupyter 端口
- python-3.x - 我尝试将图像大小调整一半有什么问题?