xamarin - 推送通知在 Xamarin.Android 中不起作用
问题描述
我已经使用Azure Notification Hub和Firebase为Xamarin.Android应用程序配置了推送通知。我已经按照这个教程
从Azure 通知中心发送测试推送通知时,我可以看到我的通知代码被调用,所以看起来一切都已配置。没有发生错误,但也没有收到推送通知。
[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);
if (message.GetNotification() != null)
{
//These is how most messages will be received
Log.Debug(TAG, "Notification Message Body: " + message.GetNotification().Body);
SendNotification(message.GetNotification().Body);
}
else
{
//Only used for debugging payloads sent from the Azure portal
SendNotification(message.Data.Values.First());
}
}
void SendNotification(string messageBody)
{
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
var notificationBuilder = new NotificationCompat.Builder(this)
.SetContentTitle("FCM Message")
.SetSmallIcon(Resource.Drawable.drivingalert)
.SetContentText(messageBody)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
var notificationManager = NotificationManager.FromContext(this);
notificationManager.Notify(0, notificationBuilder.Build()); // this should send the notification!!
}
}
我可以单步执行代码,一切似乎都没有问题,但没有收到推送通知。
解决方案
在 MainActivity 中调用以下方法设置通知通道:
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(NotificationService);
notificationManager.CreateNotificationChannel(channel);
}
如果您检查 Firebase Github,他们已经更新了此代码,但在他们的文档中似乎不可用,而且其他推送通知服务器文档还没有更新它,这导致人们以巨大的速度面临这个问题,谢谢指出这一点,我将向 Mircosoft 提出这个问题!
更新
此外,请检查您是否使用 Notification Compat 类来实现向后兼容性,如下所示:
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.SetSmallIcon(Resource.Drawable.Icon)
.SetContentTitle(messageTitle)
.SetContentText(messageBody)
.SetSound(Settings.System.DefaultNotificationUri)
.SetVibrate(new long[] { 1000, 1000 })
.SetLights(Color.AliceBlue, 3000, 3000)
.SetAutoCancel(true)
.SetOngoing(true)
.SetContentIntent(pendingIntent);
NotificationManagerCompat notificationManager = NotificationManagerCompat.From(this);
notificationManager.Notify(0, notificationBuilder.Build());
推荐阅读
- paypal - ngx-paypal unhandled_error“无法捕获订单”单次付款的多个订单
- android - Android view vs view-model 以及 Jetpacks ViewModel 是如何相关的?
- wix - WiX:如何在安装过程中禁用创建还原点,至少在测试时
- c# - 创建一个 ASP.Net Web 应用程序,详细信息如下:
- python - 将表格导入雪花的 Python 工具
- java - 使用 powershell 脚本创建编码为 UTF-8 的 .json 文件
- apache-flink - Flink 进程中的 TimerException
- javascript - 反应将数组元素添加到下拉列表?
- embedded - STM32F103不断重启
- r - 绘制树 - 折叠节点向量