More Group Sites
Education Books
School Rankings
Jobless Net
Better Home
Enviro++
更好教育论坛
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Share
Options
View
Go to last post Go to first unread
hong  
#1 Posted : Sunday, October 20, 2019 9:09:07 AM(UTC)
hong

Rank: Administration

Reputation:

Groups: AcademicCoachingSchool, admin, Administration, BookSeller, CatholicSchool, CoachingAdult, CoachingProfessional, CoachingSports, ExtraCurriculumCoaching, IndependentSchool, Moderator, MusicTeacher, PrivateSchool, PublicSchool, SelectiveSchool, tutor
Joined: 11/23/2008(UTC)
Posts: 522

Xamarin Forms - How to show Android heads-up notifications

Android introduced a high-priority notification presentation format called Heads-up since Android 5.0. Heads-up notifications slide down from the top of the screen for a few seconds and then retreat back up to the notification area.

Only high and maximum priority notifications are displayed as Heads-up notifications. Notification priority tells the system how “interruptive” this notification should be. 

Some APIs regarding notification behaviors were moved from Notification to NotificationChannel. On Android 8.0 (API level 26) and above, importance of a notification is determined by the importance of the channel the notification was posted to. For example, use NotificationChannel.setImportance() instead of NotificationCompat.Builder.setPriority(). 

In MainActivity.cs, create a notification channel for Android API 26 or later and can call this method in the onCreate method.         

Code:
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(Constants.CHANNEL_ID, "FCM Notifications", NotificationImportance.High)             {                 Description = "Firebase Cloud Messages appear in this channel"             };

            //channel.EnableLights(true);             channel.EnableVibration(true);             //channel.SetVibrationPattern(vibratePattern);             channel.LockscreenVisibility = NotificationVisibility.Public;             channel.SetShowBadge(true);

            var notificationManager = (NotificationManager)GetSystemService(Android.Content.Context.NotificationService);             notificationManager.CreateNotificationChannel(channel);         }

Settings in notification builder:            

Code:
var notificationBuilder = new NotificationCompat.Builder(this, channelId)
                .SetSmallIcon(Resource.Drawable.ic_static_notification)
                .SetContentTitle(appName)
                .SetContentText(messageBody)
                .SetStyle(new NotificationCompat.BigTextStyle().BigText(messageBody))
                .SetAutoCancel(true)
                .SetContentIntent(pendingIntent)
                .SetPriority((int)NotificationPriority.High)
                .SetVisibility((int)NotificationVisibility.Public)
                .SetDefaults(NotificationCompat.DefaultVibrate | NotificationCompat.DefaultSound)
                //.SetVibrate(new long[0])
                //.SetVibrate(new long[] { 300, 300, 300 })
                .SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification));

                var notificationManager = NotificationManagerCompat.From(this);

            //random notificationId, will display multiple notifications             notificationManager.Notify(notificationId, notificationBuilder.Build());

On earlier Android, importance of each notification is determined by the notification's priority by SetPriority(0 above.

References

Local notifications on Android

Android Notifications Overview

Headsup notification and badge is not displaying in some android devices?

Edited by user Sunday, October 20, 2019 9:14:27 AM(UTC)  | Reason: Not specified

Sponsor
Rss Feed  Atom Feed
Users browsing this topic
Guest (2)
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.