推送通知在 Xamarin.Android 中不起作用
Push notifications not working in Xamarin.Android
我已经使用 Azure 通知中心 和 Firebase[为 Xamarin.Android 应用程序配置了推送通知。我已经关注了这个 tutorial
从 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());
我已经使用 Azure 通知中心 和 Firebase[为 Xamarin.Android 应用程序配置了推送通知。我已经关注了这个 tutorial
从 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());