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

2023-12-25

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

有人可以为此建议一个解决方案吗?


我关注这个样本https://learn.microsoft.com/en-us/azure/notification-hubs/xamarin-notification-hubs-push-notifications-android-gcm https://learn.microsoft.com/en-us/azure/notification-hubs/xamarin-notification-hubs-push-notifications-android-gcm

我从我的生产项目中发布了我的 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 ());
        }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Xamarin 表单:推送通知在 Android 7.1.2 上不起作用 的相关文章

随机推荐