简体   繁体   中英

Xamarin.Forms Android Push Notification Doesn't Appear

I am using azure hub notification. I am developing my app in Xamarin.Forms . For android I can get notification when I test it hits to debug and i can show a DisplayAlert for that.

But I cannot show it as notification. I searched and after android oreo I should create a notification channel.

But I don't know how to do it. They are saying that you should create a notification id in your strings.xml but I don't have strings.xml file. I dont know how to do it, can anyone help?

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

In the MainActivity.cs You can call this method in the onCreate method :

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

Where

    internal static readonly string CHANNEL_ID = "my_notification_channel";
    internal static readonly int NOTIFICATION_ID = 100; 

are the definition for channel id and notification id respectively.

In the MainActivity's OnCreate after loading XF call this :

LoadApplication(new App());
CreateNotificationChannel();

Good luck

Revert in case of queries

I had a bit of trouble getting notifications properly showing on with Xamarin.Forms also. I'm assuming you have overridden the "OnMessageReceived" event and you're calling "CreateNotification" directly? In the end, this was the code that worked for me:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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