Click here to Skip to main content
15,878,048 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have created a xamarin application. It retrieves data from the MySQL server. it shows a new message notification when a new entry is entered in listview. I mean I want to perform the same function as WhatsApp / other social networking apps that send a notification even app is closed. should I have to perform local notification or remote? if I use local notification it does not end notification when the app is closed?

What I have tried:

Xamarin.Forms local notifications - Xamarin | Microsoft Docs[^]
but works well until app is open but when app is killed or close it stosp working?
Updated 23-Sep-21 23:45pm

They add a Service to the app, which is what handles notifications. It's separate from the "actual app" and runs in the background permanently: Creating a Service - Xamarin | Microsoft Docs[^]
Share this answer
I wanted to provide more context on OriginalGriff's message. I was working on an Android application via Xamarin a while back and was hoping to add notifications and was having a lot of issues, and they stemmed from the version of Android I was running.

Newer versions of Android (Oreo+) have stricter restrictions on background services, now shutting them down in order to save on resources and battery. This has caused headaches among Android developers, and the recommended practise for the newer Android versions is to in-fact create a "foreground" service which can produce a notification which sits in the notification drawer effectively permanently, allowing for background processing.

This might mean the phone will display a permanent notification (I've seen some Android applications do this, and I manually hide them to keep things tidy) but it will assert that your application will receive notifications. If you've noticed some applications don't create this notification, it's because some providers (ie. Samsung) white-list certain applications to ensure that the background services don't get shutdown.

Here's a link to a StackOverflow answer[^] which gives a good explanation of what is going to be needed. It does target Java Android but you can use the basic principle to work out how to implement it in Xamarin.
Share this answer
Member 14192879 27-Sep-21 1:07am    
thank you for your useful time. after some articles and your helpful link. i find below code and it works fine... but problem is that it always show the "service start". how can I hide this notification? i this want app keep sending message to client but first it does not show the foreground service "service started" notiifcation.
private Handler handler;
private Action runnable;
private bool isStarted;
private int DELAY_BETWEEN_LOG_MESSAGES = 5000;
private int NOTIFICATION_SERVICE_ID = 1001;
// private int NOTIFICATION_AlARM_ID = 1002;
private int NOTIFICATION_AlARM_ID = 1;
private string NOTIFICATION_CHANNEL_ID = "1003";
private string NOTIFICATION_CHANNEL_NAME = "MyChannel";
public int i;
public override void OnCreate()

handler = new Handler();

//here is what you want to do always, i just want to push a notification every 5 seconds here
runnable = new Action(() =>
if (isStarted)
DispatchNotificationThatAlarmIsGenerated("I'm running"+ i++);
handler.PostDelayed(runnable, DELAY_BETWEEN_LOG_MESSAGES);

public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
if (isStarted)
// service is already started
handler.PostDelayed(runnable, DELAY_BETWEEN_LOG_MESSAGES);
isStarted = true;
return StartCommandResult.Sticky;

public override void OnTaskRemoved(Intent rootIntent)

public override IBinder OnBind(Intent intent)
// Return null because this is a pure started service. A hybrid service would return a binder that would
// allow access to the GetFormattedStamp() method.
return null;

public override void OnDestroy()
// Stop the handler.

// Remove the notification from the status bar.
var notificationManager = (NotificationManager)GetSystemService(NotificationService);

isStarted = false;

private void CreateNotificationChannel()
//Notification Channel
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationImportance.Max);
notificationChannel.SetVibrationPattern(new long[] { 100, 200, 300, 400, 500, 400, 300, 200, 400 });

NotificationManager notificationManager = (NotificationManager)this.GetSystemService(Context.NotificationService);

//start a foreground notification to keep alive
private void DispatchNotificationThatServiceIsRunning()
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.SetVibrate(new long[] { 100, 200, 300, 400, 500, 400, 300, 200, 400 })
.SetContentText("ervice started")
Member 14192879 27-Sep-21 1:08am    
NotificationManagerCompat notificationManager = NotificationManagerCompat.From(this);
StartForeground(NOTIFICATION_SERVICE_ID, builder.Build());

//every 5 seconds push a notificaition
private void DispatchNotificationThatAlarmIsGenerated(string message)
var intent = new Intent(this, typeof(MainActivity));
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

Notification.Builder notificationBuilder = new Notification.Builder(this, NOTIFICATION_CHANNEL_ID)

var notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.Notify(NOTIFICATION_AlARM_ID, notificationBuilder.Build());

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900