简体   繁体   English

Xamarin 表单:推送通知不适用于 Android 7.1.2

[英]Xamarin forms: Push notification is not working on Android 7.1.2

Push notification is not working on Android device having a version number 7.1.2, but working fine on version 9. Following is my code for showing the notification.推送通知不适用于版本号为 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());
        }

Can anyone please suggest a solution for this?任何人都可以为此提出解决方案吗?

I following this sample https://docs.microsoft.com/en-us/azure/notification-hubs/xamarin-notification-hubs-push-notifications-android-gcm我遵循此示例https://docs.microsoft.com/en-us/azure/notification-hubs/xamarin-notification-hubs-push-notifications-android-gcm

I post my SendNotification method from my production project.我从我的生产项目中发布了我的 SendNotification 方法。

Following my working code and change your method.按照我的工作代码并更改您的方法。

    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 ());
        }
    }

First of all don't use Android.App.Notification.Builder is deprecated.首先不要使用 Android.App.Notification.Builder 已被弃用。 The NotificationCompat.Builder is the new one. NotificationCompat.Builder 是新的。

Maybe this one is your new code也许这是你的新代码

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 notifications behave differently depending on the foreground/background state of the receiving app. Firebase 通知的行为取决于接收应用的前台/后台状态。

Notification messages delivered when your app is in the background.当您的应用程序在后台时传递的通知消息。 In this case, the notification is delivered to the device's system tray.在这种情况下,通知将传送到设备的系统托盘。 A user tap on a notification opens the app launcher by default.默认情况下,用户点击通知会打开应用程序启动器。

Messages with both notification and data payload, when received in the background.在后台接收时具有通知和数据负载的消息。 In this case, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.在这种情况下,通知被传送到设备的系统托盘,数据有效负载被传送到启动器 Activity 的意图的额外内容中。

For more information, visit https://firebase.google.com/docs/cloud-messaging/android/receive有关更多信息,请访问https://firebase.google.com/docs/cloud-messaging/android/receive

I also have this sample code for you to try:我也有这个示例代码供您尝试:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM