表单Android推送通知不';不出现
我正在使用azure中心通知。 我正在表单Android推送通知不';不出现,android,xamarin,push-notification,xamarin.android,google-cloud-messaging,Android,Xamarin,Push Notification,Xamarin.android,Google Cloud Messaging,我正在使用azure中心通知。 我正在Xamarin.Forms中开发我的应用程序。对于android,我可以在测试时收到通知,然后显示一个DisplayAlert 但我不能将其显示为通知。我搜索了,之后我应该创建一个通知通道 但我不知道怎么做。他们说您应该在strings.xml中创建通知id,但我没有strings.xml文件。我不知道怎么做,有人能帮忙吗 internal static readonly string CHANNEL_ID = "cross_channel"; vo
Xamarin.Forms
中开发我的应用程序。对于android,我可以在测试时收到通知,然后显示一个DisplayAlert
但我不能将其显示为通知。我搜索了,之后我应该创建一个通知通道
但我不知道怎么做。他们说您应该在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上正确显示通知时,我遇到了一些麻烦。表单也是。我假设您已经覆盖了“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());
}
private void ShowNotification(RemoteMessage消息、IDictionary数据)
{
var intent=新intent();
intent.AddFlags(ActivityFlags.ClearTop);
foreach(data.Keys中的var键)
intent.PutExtra(键,数据[key]);
var pendingIntent=pendingIntent.GetActivity(Android.App.Application.Context,100,intent,PendingIntentFlags.OneShot);
var notificationBuilder=new NotificationCompat.Builder(Android.App.Application.Context)//注意:我在网上读到的所有内容都说在这里提供ChannelID字符串,但这样做会导致它不显示通知。
.SetSmallIcon(Resource.Drawable.abc_btn_radio_to_on_mtrl_000)//您可以将其设置为应用程序图标
.SetContentTitle(msg.GetNotification().Title)
.SetContentText(msg.GetNotification().Body)
.SetPriority((int)Android.App.NotificationImportance.Max)
.SetDefaults(NotificationCompat.DefaultAll)
.SetContentIntent(pendingIntent)//即使此处的intent为空,您*可能*需要包含它才能显示通知,我从未尝试过不包含它。
.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);
}
分别是通道id和通知id的定义
在加载XF后的MainActivity的OnCreate中,调用以下命令:
LoadApplication(new App());
CreateNotificationChannel();
祝你好运
在查询时还原用于将附加(“额外”)数据传递给意图。它可能与您无关,在这种情况下,您应该能够忽略它。请看:我已经添加了一个答案,请看一看
LoadApplication(new App());
CreateNotificationChannel();