[英]Xamarin forms: Push notification is not working on Android 7.1.2
推送通知不適用於版本號為 7.1.2 的 Android 設備,但在版本 9 上工作正常。以下是我用於顯示通知的代碼。
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
{
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
var notificationBuilder = new Android.App.Notification.Builder(this, Utils.CHANNEL_ID)
.SetContentTitle(Header)
.SetSmallIcon(Resource.Drawable.icon)
.SetContentText(body)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
var notificationManager = NotificationManager.FromContext(this);
notificationManager.Notify(0, notificationBuilder.Build());
}
else
{
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
var notificationBuilder = new Android.App.Notification.Builder(this, Utils.CHANNEL_ID)
.SetContentTitle(Header)
.SetSmallIcon(Resource.Drawable.icon)
.SetContentText(body)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent)
.SetChannelId(Utils.CHANNEL_ID);
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
{
return;
}
var channel = new NotificationChannel(Utils.CHANNEL_ID, "FCM Notifications", NotificationImportance.High)
{
Description = "Firebase Cloud Messages appear in this channel"
};
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(channel);
notificationManager.Notify(0, notificationBuilder.Build());
}
任何人都可以為此提出解決方案嗎?
我從我的生產項目中發布了我的 SendNotification 方法。
按照我的工作代碼並更改您的方法。
void SendNotification (string messageBody, string title)
{
var intent = new Intent (this, typeof (SplashActivity));
intent.AddFlags (ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity (this, 0, intent, PendingIntentFlags.OneShot);
//if i want more than one notification ,different unique value in every call
Random u = new Random ();
if (Build.VERSION.SdkInt >= BuildVersionCodes.O) {
string channelName = Resources.GetString (Resource.String.channel_name);
NotificationCompat.Builder notificationBuilder;
notificationBuilder = new NotificationCompat.Builder (this, channelName)
.SetContentTitle (title)
.SetSmallIcon (Resource.Drawable.ic_stat_g)
.SetContentText (messageBody)
.SetAutoCancel (true)
.SetContentIntent (pendingIntent);
var notificationManager = GetSystemService (Context.NotificationService) as NotificationManager;
NotificationChannel channel = new NotificationChannel (channelName, "notification channel", NotificationImportance.Default);
notificationManager.CreateNotificationChannel (channel);
notificationManager.Notify (u.Next(), notificationBuilder.Build ());
}
else
{
NotificationCompat.Builder notificationBuilder;
notificationBuilder = new NotificationCompat.Builder (this)
.SetContentTitle (title)
.SetSmallIcon (Resource.Drawable.ic_stat_g)
.SetContentText (messageBody)
.SetAutoCancel (true)
.SetContentIntent (pendingIntent);
var notificationManager = GetSystemService (Context.NotificationService) as NotificationManager;
notificationManager.Notify (u.Next(), notificationBuilder.Build ());
}
}
首先不要使用 Android.App.Notification.Builder 已被棄用。 NotificationCompat.Builder 是新的。
也許這是你的新代碼
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
{
var notificationBuilder = new NotificationCompat.Builder(this)
.SetContentTitle(Header)
.SetSmallIcon(Resource.Drawable.icon)
.SetContentText(body)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
var notificationManager = GetSystemService (Context.NotificationService) as NotificationManager;
notificationManager.Notify(0, notificationBuilder.Build());
}
else
{
var notificationBuilder = new NotificationCompat.Builder(this, Utils.CHANNEL_ID)
.SetContentTitle(Header)
.SetSmallIcon(Resource.Drawable.icon)
.SetContentText(body)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
var notificationManager = GetSystemService (Context.NotificationService) as NotificationManager;
NotificationChannel channel = new NotificationChannel (Utils.CHANNEL_ID, "FCM Notifications", NotificationImportance.Default);
notificationManager.CreateNotificationChannel (channel);
notificationManager.Notify (0, notificationBuilder.Build ());
}
Firebase 通知的行為取決於接收應用的前台/后台狀態。
當您的應用程序在后台時傳遞的通知消息。 在這種情況下,通知將傳送到設備的系統托盤。 默認情況下,用戶點擊通知會打開應用程序啟動器。
在后台接收時具有通知和數據負載的消息。 在這種情況下,通知被傳送到設備的系統托盤,數據有效負載被傳送到啟動器 Activity 的意圖的額外內容中。
有關更多信息,請訪問https://firebase.google.com/docs/cloud-messaging/android/receive
我也有這個示例代碼供您嘗試:
var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
NotificationChannel notificationChannel = new NotificationChannel("my_channel", "This is my Notification Channel", NotificationImportance.High);
builder.SetChannelId("my_channel");
notificationManager.CreateNotificationChannel(notificationChannel);
}
var notification = builder.SetContentIntent(PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.Immutable))
.SetSmallIcon(Resource.Drawable.icon)
.SetContentTitle(Header)
.SetContentText(body)
//Set vibrate
.SetVibrate(new long[] { 200, 200, 200, 200 })
//LED
.SetLights(Android.Graphics.Color.Blue, 1000, 1000)
//Auto cancel will remove the notification once the user touches it
.SetAutoCancel(true).Build();
notificationManager.Notify(0, notification);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.