Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Notification Hub received in console but not showing

soufianemarlysoufianemarly Member ✭✭✭
edited March 2019 in Xamarin.Forms

Hello Developers,
i'm using xamarin forms. i was able to send notification from back end to IOS Devices.
But for Android the app receive the title and message from azure portal and don't show it as a notification i don't know where is the problem

Some Help please.

Thank you

Best Answer

  • LandLuLandLu Member, Xamarin Team Xamurai
    Accepted Answer

    For Android 8.0 and later, you have to add a Notification Channel to show it up: https://blog.xamarin.com/android-oreo-notification-channels
    So you could refer to my SendNotification code:

    public const string URGENT_CHANNEL = "com.xamarin.myapp.urgent";
    public const int NOTIFY_ID = 1100;
    
    void SendNotification(string messageBody)
    {
        var importance = NotificationImportance.High;
        NotificationChannel chan = new NotificationChannel(URGENT_CHANNEL, "Urgent", importance);
        chan.EnableVibration(true);
        chan.LockscreenVisibility = NotificationVisibility.Public;                
    
        var intent = new Intent(this, typeof(MainActivity));
        intent.AddFlags(ActivityFlags.ClearTop);
        var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.UpdateCurrent);
    
        var notificationBuilder = new NotificationCompat.Builder(this)
            .SetSmallIcon(Resource.Drawable.ic_stat_ic_notification)
            .SetContentTitle("New Todo Item")
            .SetContentText(messageBody)
            .SetContentIntent(pendingIntent)
            .SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification))
            .SetAutoCancel(true)
            .SetChannelId(URGENT_CHANNEL);
    
        NotificationManager notificationManager = (NotificationManager)GetSystemService(NotificationService);
        notificationManager.CreateNotificationChannel(chan);
    
        notificationManager.Notify(NOTIFY_ID, notificationBuilder.Build());
    }
    

Answers

  • PaulNTUPaulNTU USMember ✭✭✭

    Have you added the code to the android client?

    https://blog.xamarin.com/implementing-push-notifications-android-apps/

    On android you need to create a receiver that handles the incoming message and allows you to layout how the notification appears on the device (E.g icon, text, action on open etc). See the link above for instructions

    HTH

  • LandLuLandLu Member, Xamarin Team Xamurai
    Accepted Answer

    For Android 8.0 and later, you have to add a Notification Channel to show it up: https://blog.xamarin.com/android-oreo-notification-channels
    So you could refer to my SendNotification code:

    public const string URGENT_CHANNEL = "com.xamarin.myapp.urgent";
    public const int NOTIFY_ID = 1100;
    
    void SendNotification(string messageBody)
    {
        var importance = NotificationImportance.High;
        NotificationChannel chan = new NotificationChannel(URGENT_CHANNEL, "Urgent", importance);
        chan.EnableVibration(true);
        chan.LockscreenVisibility = NotificationVisibility.Public;                
    
        var intent = new Intent(this, typeof(MainActivity));
        intent.AddFlags(ActivityFlags.ClearTop);
        var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.UpdateCurrent);
    
        var notificationBuilder = new NotificationCompat.Builder(this)
            .SetSmallIcon(Resource.Drawable.ic_stat_ic_notification)
            .SetContentTitle("New Todo Item")
            .SetContentText(messageBody)
            .SetContentIntent(pendingIntent)
            .SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification))
            .SetAutoCancel(true)
            .SetChannelId(URGENT_CHANNEL);
    
        NotificationManager notificationManager = (NotificationManager)GetSystemService(NotificationService);
        notificationManager.CreateNotificationChannel(chan);
    
        notificationManager.Notify(NOTIFY_ID, notificationBuilder.Build());
    }
    
Sign In or Register to comment.