Xamarin表单:推送通知在Android 7.1.2上不起作用

Xamarin表单:推送通知在Android 7.1.2上不起作用,android,xamarin.forms,firebase-cloud-messaging,Android,Xamarin.forms,Firebase Cloud Messaging,推送通知在版本号为7.1.2的Android设备上不起作用,但在版本9上可以正常工作。以下是我显示通知的代码 if (Build.VERSION.SdkInt < BuildVersionCodes.O) { var intent = new Intent(this, typeof(MainActivity)); intent.AddFlags(ActivityFlags.ClearTop); var

推送通知在版本号为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());
        }
if(Build.VERSION.SdkInt

任何人都可以为此提出解决方案吗?

Firebase通知的行为因接收应用程序的前台/后台状态而异

应用程序处于后台时发送的通知消息。在这种情况下,通知将发送到设备的系统托盘。默认情况下,用户点击通知会打开应用程序启动器

在后台接收时,同时具有通知和数据负载的消息。在这种情况下,通知被传送到设备的系统托盘,数据有效负载在启动器活动的目的的附加部分中传送

有关更多信息,请访问

我还有以下示例代码供您尝试:

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);
我遵循这个样本

我发布生产项目中的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 ());
        }
var intent=新意图(此,类型为(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent=pendingIntent.GetActivity(this,0,intent,PendingIntentFlags.OneShot);
if(Build.VERSION.SdkInt
获取了
intent
的错误,因此我添加了
var intent=newintent(这个,typeof(MainActivity))到代码。但是代码执行会在
NotificationChannel
code处停止。您是否至少收到了错误消息或在此处发布了stacktrace?太好了!!!我从这里创建我的图标通知图标有任何颜色限制吗?检查此项