Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Background service stop when app is terminated Xamarin android

$
0
0

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


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>