Local notifications

I have a Xamarin.Forms PCL project that I am trying to get the notifications to work on both Android and iOS. I have tried wiring up the notifications on the platform side within the subscription to a message that was sent by the shared project. Is there any way to wire up notifications for any Xamarin.Forms projects that does not require connections to remote servers or services but are ran locally from the users phone or the application itself?


  • LearnEverythingLearnEverything USMember ✭✭✭

    using Android.App; using Android.Content; using Android.Support.V4.App; using Android.OS; namespace XamarinAndroid.Noti { public class NotiHelper { static int NOTIFICATION_ID = 1000; static string CHANNEL_ID = "location_notification"; internal static string TITLE_KEY = "count"; static int count = 0; public static void SendNotification(Context context, string title, string message, int drawableIcon) { CreateNotificationChannel(context); // Pass the current button press count value to the next activity: var valuesForActivity = new Bundle(); valuesForActivity.PutInt(title, count); TITLE_KEY = title; // When the user clicks the notification, Activity will start up. var notIntent = new Intent(context, typeof(MainActivity)); var contentIntent = PendingIntent.GetActivity(context, 0, notIntent, PendingIntentFlags.CancelCurrent); // Build the notification: var builder = new NotificationCompat.Builder(context, CHANNEL_ID) .SetAutoCancel(true) // Dismiss the notification from the notification area when the user clicks on it .SetContentIntent(contentIntent) // Start up this activity when the user clicks the intent. .SetContentTitle(title) // Set the title .SetNumber(count) // Display the count in the Content Info .SetSmallIcon(drawableIcon) // This is the icon to display .SetContentText(message); // the message to display. // Finally, publish the notification: var notificationManager = NotificationManagerCompat.From(context); notificationManager.Notify(NOTIFICATION_ID, builder.Build()); // Increment the button press count: count++; } static void CreateNotificationChannel(Context context) { 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 name = "channel_name"; var description = "channel_description"; var channel = new NotificationChannel(CHANNEL_ID, name, NotificationImportance.Default) { Description = description }; var notificationManager = (NotificationManager)context.GetSystemService(Context.NotificationService); notificationManager.CreateNotificationChannel(channel); } } }
Sign In or Register to comment.