Xamarin.Forms Android 推送通知没有出现
Xamarin.Forms Android Push Notification Doesn't Appear
我正在使用 Azure Hub 通知。
我正在 Xamarin.Forms
开发我的应用程序。对于 android,我可以在测试它进行调试时收到通知,我可以为此显示 DisplayAlert
。
但我无法将其显示为通知。我进行了搜索,在 android oreo 之后我应该创建一个通知渠道。
但是我不知道该怎么做。他们说您应该在 strings.xml
中创建一个通知 ID,但我没有 strings.xml 文件。我不知道该怎么做,有人可以帮忙吗?
internal static readonly string CHANNEL_ID = "cross_channel";
void CreateNotification(string title, string desc)
{
var notificationManager = GetSystemService(Context.NotificationService)
as NotificationManager;
var uiIntent = new Intent(this, typeof(MainActivity));
var pendingIntent = PendingIntent.GetActivity(this, RandomGenerator(), uiIntent, PendingIntentFlags.OneShot);
var notification = new Notification(Android.Resource.Drawable.ButtonMinus, title)
{
Flags = NotificationFlags.AutoCancel
};
notification.SetLatestEventInfo(this, title, desc,
PendingIntent.GetActivity(this, 0, uiIntent, 0));
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
var channel = new NotificationChannel(CHANNEL_ID,
"Cross Notifications",
NotificationImportance.High);
notificationManager.CreateNotificationChannel(channel);
string channelId = "Cross Channel";
var notBuilder = new Notification.Builder(Application.Context, CHANNEL_ID).SetContentTitle(title).SetContentText(desc).SetSmallIcon(Android.Resource.Drawable.StarBigOn).SetAutoCancel(true);
notificationManager.Notify(1, notBuilder.Build());
channel.Description = (desc);
notBuilder.SetChannelId(channelId);
}
notificationManager.Notify(RandomGenerator(), notBuilder.Build());
}
我在 Xamarin.Forms 上也无法正确显示通知。我假设您已覆盖 "OnMessageReceived" 事件并直接调用 "CreateNotification"?最后,这是对我有用的代码:
private void ShowNotification(RemoteMessage msg, IDictionary<string, string> data)
{
var intent = new Intent();
intent.AddFlags(ActivityFlags.ClearTop);
foreach (var key in data.Keys)
intent.PutExtra(key, data[key]);
var pendingIntent = PendingIntent.GetActivity(Android.App.Application.Context, 100, intent, PendingIntentFlags.OneShot);
var notificationBuilder = new NotificationCompat.Builder(Android.App.Application.Context) // Note: Everything I read online said to provide the ChannelID string here, but doing so caused it to not display notifications.
.SetSmallIcon(Resource.Drawable.abc_btn_radio_to_on_mtrl_000) // You can set this to your apps icon
.SetContentTitle(msg.GetNotification().Title)
.SetContentText(msg.GetNotification().Body)
.SetPriority((int)Android.App.NotificationImportance.Max)
.SetDefaults(NotificationCompat.DefaultAll)
.SetContentIntent(pendingIntent) // Even though intent here is empty, you *may* need to include it for the notification to show, I never tried without one.
.SetVisibility((int)NotificationVisibility.Public);
var notificationManager = NotificationManagerCompat.From(Android.App.Application.Context);
notificationManager.Notify(100, notificationBuilder.Build());
}
在MainActivity.cs
你可以在onCreate方法中调用这个方法:
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);
}
哪里
internal static readonly string CHANNEL_ID = "my_notification_channel";
internal static readonly int NOTIFICATION_ID = 100;
分别是频道id和通知id的定义
加载 XF 后在 MainActivity 的 OnCreate 中调用此 :
LoadApplication(new App());
CreateNotificationChannel();
祝你好运
如有疑问请回复
我正在使用 Azure Hub 通知。
我正在 Xamarin.Forms
开发我的应用程序。对于 android,我可以在测试它进行调试时收到通知,我可以为此显示 DisplayAlert
。
但我无法将其显示为通知。我进行了搜索,在 android oreo 之后我应该创建一个通知渠道。
但是我不知道该怎么做。他们说您应该在 strings.xml
中创建一个通知 ID,但我没有 strings.xml 文件。我不知道该怎么做,有人可以帮忙吗?
internal static readonly string CHANNEL_ID = "cross_channel";
void CreateNotification(string title, string desc)
{
var notificationManager = GetSystemService(Context.NotificationService)
as NotificationManager;
var uiIntent = new Intent(this, typeof(MainActivity));
var pendingIntent = PendingIntent.GetActivity(this, RandomGenerator(), uiIntent, PendingIntentFlags.OneShot);
var notification = new Notification(Android.Resource.Drawable.ButtonMinus, title)
{
Flags = NotificationFlags.AutoCancel
};
notification.SetLatestEventInfo(this, title, desc,
PendingIntent.GetActivity(this, 0, uiIntent, 0));
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
var channel = new NotificationChannel(CHANNEL_ID,
"Cross Notifications",
NotificationImportance.High);
notificationManager.CreateNotificationChannel(channel);
string channelId = "Cross Channel";
var notBuilder = new Notification.Builder(Application.Context, CHANNEL_ID).SetContentTitle(title).SetContentText(desc).SetSmallIcon(Android.Resource.Drawable.StarBigOn).SetAutoCancel(true);
notificationManager.Notify(1, notBuilder.Build());
channel.Description = (desc);
notBuilder.SetChannelId(channelId);
}
notificationManager.Notify(RandomGenerator(), notBuilder.Build());
}
我在 Xamarin.Forms 上也无法正确显示通知。我假设您已覆盖 "OnMessageReceived" 事件并直接调用 "CreateNotification"?最后,这是对我有用的代码:
private void ShowNotification(RemoteMessage msg, IDictionary<string, string> data)
{
var intent = new Intent();
intent.AddFlags(ActivityFlags.ClearTop);
foreach (var key in data.Keys)
intent.PutExtra(key, data[key]);
var pendingIntent = PendingIntent.GetActivity(Android.App.Application.Context, 100, intent, PendingIntentFlags.OneShot);
var notificationBuilder = new NotificationCompat.Builder(Android.App.Application.Context) // Note: Everything I read online said to provide the ChannelID string here, but doing so caused it to not display notifications.
.SetSmallIcon(Resource.Drawable.abc_btn_radio_to_on_mtrl_000) // You can set this to your apps icon
.SetContentTitle(msg.GetNotification().Title)
.SetContentText(msg.GetNotification().Body)
.SetPriority((int)Android.App.NotificationImportance.Max)
.SetDefaults(NotificationCompat.DefaultAll)
.SetContentIntent(pendingIntent) // Even though intent here is empty, you *may* need to include it for the notification to show, I never tried without one.
.SetVisibility((int)NotificationVisibility.Public);
var notificationManager = NotificationManagerCompat.From(Android.App.Application.Context);
notificationManager.Notify(100, notificationBuilder.Build());
}
在MainActivity.cs
你可以在onCreate方法中调用这个方法:
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);
}
哪里
internal static readonly string CHANNEL_ID = "my_notification_channel";
internal static readonly int NOTIFICATION_ID = 100;
分别是频道id和通知id的定义
加载 XF 后在 MainActivity 的 OnCreate 中调用此 :
LoadApplication(new App());
CreateNotificationChannel();
祝你好运
如有疑问请回复