Hello guys i'm trying to check for notification every 1 minute
when click the home button every 1 min i receive notification but when i terminate the app from the task manager i don't receive anything
this is my code :
Mainactivity.cs
protected override void OnCreate(Bundle bundle) { TabLayoutResource = Resource.Layout.Tabbar; ToolbarResource = Resource.Layout.Toolbar; bbundle = bundle; base.OnCreate(bundle); InitBroadcast(); global::Xamarin.Forms.Forms.Init(this, bbundle); UserDialogs.Init(this); ImageCircleRenderer.Init(); RoundedBoxViewRenderer.Init(); DeviceOrientationImplementation.Init(); CarouselViewRenderer.Init(); Xamarin.Forms.DataGrid.DataGridComponent.Init(); Zumero.DataGridComponent.Init(); // NOTIFICATION LocalNotificationsImplementation.NotificationIconId = Resource.Drawable.app_epf; LoadApplication(new App()); // BAckground task for notification var intent = new Intent(this, typeof(PeriodicService)); StartService(intent); AppDomain.CurrentDomain.UnhandledException += (sender, args) => { //Console.WriteLine(args.ExceptionObject.GetType()); }; } void InitBroadcast() { TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)); long millis = (long)ts.TotalMilliseconds; Intent intentAlarm = new Intent(this, typeof(BackgroundReceiver)); AlarmManager alarmManager = (AlarmManager)GetSystemService(Context.AlarmService); int interval = 10000; alarmManager.SetRepeating(AlarmType.RtcWakeup, millis, interval, PendingIntent.GetBroadcast(this, 1, intentAlarm, PendingIntentFlags.UpdateCurrent)); }
PeriodicService.cs
namespace epf.Droid
{
[Service(Enabled = true, Exported = false)]
public class PeriodicService : Service
{
public override IBinder OnBind(Intent intent)
{
return null;
}
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId) { // From shared code or in your PCL CrossLocalNotifications.Current.Show("SERVICE", "SERVICE"); return StartCommandResult.NotSticky; } }
}
namespace epf.Droid
{
[BroadcastReceiver(Enabled = true, Exported = false)]
public class BackgroundReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
PowerManager pm = (PowerManager)context.GetSystemService(Context.PowerService); PowerManager.WakeLock wakeLock = pm.NewWakeLock(WakeLockFlags.Partial, "BackgroundReceiver"); wakeLock.Acquire(); LocalNotif notification = new LocalNotif(); CrossLocalNotifications.Current.Show("BACKROUND TEST", "RAPPEL A ZÉB"); BadgeConter badgenumber = new BadgeConter(); badgenumber.badgenotification("MARKS", "ABSENCES", "OTHERS", "EVENTS"); int numberbadge = badgenumber.badge5; Badge badge = new Badge(context); badge.count(numberbadge); Console.WriteLine("JE SUIS DANS SERVICE BADGEDABA TKOUN 3 --> " + numberbadge); wakeLock.Release(); ////Toast.MakeText(context, string.Format("THE TIME IS {0}", DateTime.Now.ToShortTimeString()), ToastLength.Long).Show(); //Vibrator vibrator = (Vibrator)context.GetSystemService(Context.VibratorService); LocalNotif notification1 = new LocalNotif(); //vibrator.Vibrate(500); } }
}
Thank you
Hi @soufianemarly ,
If your app is an background for certain amount of time then OS will terminate all the services related to app.
By looking at code, I think you have implemented local notifications.
If you want to receive notifications all the time then it is better to go with PUSH NOTIFICATIONs except local in app notifications.
Hi @soufianemarly ,
Means you want to auto generate NOTIFICATION from your server when some event happens. Right?
Then you need to write some code on your server (in your WebAPI/WCF app, that you are consuming in your app) to trigger Azure server to send push notification.
Please visit https://docs.microsoft.com/en-us/azure/notification-hubs/notification-hubs-aspnet-backend-windows-dotnet-wns-notification.
Answers
@soufianemarly
If the user kills the instance your background process will end or the OS needs the memory your app can be killed
Maybe this answer helps
https://stackoverflow.com/questions/3197335/restful-api-service
Your not guaranteed to keep the background service running though.
https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/services/#background-execution-limits-in-android-80
Hi @soufianemarly ,
If your app is an background for certain amount of time then OS will terminate all the services related to app.
By looking at code, I think you have implemented local notifications.
If you want to receive notifications all the time then it is better to go with PUSH NOTIFICATIONs except local in app notifications.
Hello @ManojkumarMali,
thank you now i'm using azure to send push notification. I was able to send notifications manually from azure
but i already have a server that content the informations how can i receive notification from my own server via azure
Hi @soufianemarly ,
Means you want to auto generate NOTIFICATION from your server when some event happens. Right?
Then you need to write some code on your server (in your WebAPI/WCF app, that you are consuming in your app) to trigger Azure server to send push notification.
Please visit https://docs.microsoft.com/en-us/azure/notification-hubs/notification-hubs-aspnet-backend-windows-dotnet-wns-notification.