How to run background task erveryday at 6:30PM

AswathiPSAswathiPS USMember ✭✭
edited April 2018 in Xamarin.Android

https://xamarinhelp.com/xamarin-background-tasks/#comment-1296 .. am using this background worker. I want to show Local Notification Repeatedly on every day where my application state either running or not. Its not work for me.. Please help

Thanks !

Best Answer

  • ChristopheBERNARDChristopheBERNARD BE ✭✭✭✭
    Accepted Answer

    Hello @AswathiPS no returned mail that's strange !
    here is it the code

    main activity
    `
    [Activity(Label = "AppBroadCastNeverEnd", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
    protected override void OnCreate(Bundle bundle)
    {
    base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
    
            // bcEvery10Sec.bcEvery10Sec => if not work see this application
            InitBroadcast();
        }
        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(ToastBroadcast));
            AlarmManager alarmManager = (AlarmManager)GetSystemService(Context.AlarmService);
            int interval = 10000;
    
            alarmManager.SetRepeating(AlarmType.RtcWakeup, millis, interval, PendingIntent.GetBroadcast(this, 1, intentAlarm, PendingIntentFlags.UpdateCurrent));
        }
    }
    

    `

    the broadcast
    `

    [BroadcastReceiver]
    public class ToastBroadcast : BroadcastReceiver
    {
    public override void OnReceive(Context context, Intent intent)
    {
    Toast.MakeText(context, string.Format("THE TIME IS {0}", DateTime.Now.ToShortTimeString()), ToastLength.Long).Show();
    Vibrator vibrator = (Vibrator)context.GetSystemService(Context.VibratorService);
    vibrator.Vibrate(500);
    }
    }
    `

    and the manifest

    `
    uses-permission android:name="android.permission.INTERNET"
    uses-permission android:name="com.android.alarm.permission.SET_ALARM"
    uses-permission android:name="android.permission.VIBRATE"

    `

    hope this help

Answers

  • AswathiPSAswathiPS USMember ✭✭

    @AdamPedley

    https://xamarinhelp.com/xamarin-background-tasks/#comment-1296 .. am using this background worker. I want to show Local Notification Repeatedly on every day where my application state either running or not. Its not work for me.. Please help

    Thanks !

  • ispirLee25ispirLee25 Member ✭✭

    easy come

  • AswathiPSAswathiPS USMember ✭✭

    @ispirLee25 ,I don't get u.. Please share your idea about my questions..

  • AswathiPSAswathiPS USMember ✭✭

    public async Task SendNotification()
    {
    Intent alarmIntent = new Intent(this, typeof(AlarmReceiverNew));
    alarmIntent.PutExtra(“message”, “message”);
    alarmIntent.PutExtra(“title”, “Welcome”);

    PendingIntent pendingIntent = PendingIntent.GetBroadcast(this, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
    AlarmManager alarmManager = (AlarmManager)this.GetSystemService(Context.AlarmService);
    long futureInMillis = SystemClock.ElapsedRealtime() + 12 * 3600 * 1000;
    alarmManager.Set(AlarmType.ElapsedRealtimeWakeup, futureInMillis, pendingIntent);
    }

    [BroadcastReceiver]
    public class AlarmReceiverNew : BroadcastReceiver
    { public async override void OnReceive(Context context, Intent intent)
    {

    Intent notIntent = new Intent(context, typeof(MainActivity));
    PendingIntent contentIntent = PendingIntent.GetActivity(context, 0, notIntent, PendingIntentFlags.CancelCurrent);
    NotificationManagerCompat manager = NotificationManagerCompat.From(context);

    var wearableExtender = new NotificationCompat.WearableExtender().SetBackground(BitmapFactory.DecodeResource(context.Resources, 1));

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
    .SetContentIntent(contentIntent)
    .SetSmallIcon(Resource.Drawable.icon)
    .SetContentTitle(“title”)
    .SetContentText(“message”)
    .SetWhen(Java.Lang.JavaSystem.CurrentTimeMillis())
    .SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification))
    .Extend(wearableExtender);

    Notification notification = builder.Build();

    manager.Notify(0, notification);

    }
    }

    This is my code .. but it not worked ... Please help

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭

    hello @AswathiPS
    your code look good ...put a break point a see if you stop in the method => OnReceive or change the time of the notification ...add each 10 secondes for to see ...

  • AswathiPSAswathiPS USMember ✭✭

    @ChristopheBERNARD , thanks for your reply, Can u make it clear . I don't understand it.

  • AswathiPSAswathiPS USMember ✭✭

    @AdamP https://xamarinhelp.com/xamarin-background-tasks/#comment-1296 .. am using this background worker. I want to show Local Notification Repeatedly on every day where my application state either running or not. Its not work for me.. Please help

    Thanks !

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭
  • AswathiPSAswathiPS USMember ✭✭

    @ChristopheBERNARD
    http://eggmetrics.com/xamarin-android-notifications-schedule/ am trying this .. its worked well where my application state is running. and is not working my application state is not running.

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭

    @AswathiPS => normally the broadcast must be work if the app is an background and if the app is closed ...
    i have try and for me that's work

    my app make a toast with the time each minute, i close the app in debugger of cours :)
    after i start the app by the device, finger touch the icon , and each minute i receive the broadcast receiver

  • AswathiPSAswathiPS USMember ✭✭

    @ChristopheBERNARD , the solution is worked for me where my application is running and available in the recent history. but its not working where am cleared recent history.

    is your solutions is working where you are clear recent history.

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭

    Hello @AswathiPS when you said clear recent history you talk about the app when it's on background ?
    I've create the app, push to the device with visual studio => debug mode
    stop debug mode

    start the app by clicking on short cut on device, each minute i receive a toast i close the app by swiping, the background is still ALIVE ...

    send me a private mail with your mail address i will send you me code ..perhpas something is strange with your ...but i doubt ...

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭

    Hello @AswathiPS i just to send the zip file, compile and install and tell me if it's ok

  • AswathiPSAswathiPS USMember ✭✭
    edited April 2018

    @ChristopheBERNARD ,Thanks compile and install and let u know ,I didn't get the email yet.

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭

    @AswathiPS => did you receive all ? i've check my mail and no returned mail :)

  • AswathiPSAswathiPS USMember ✭✭

    @ChristopheBERNARD I didn't get any mail frm u.. could u plz share on Github.

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭
    Accepted Answer

    Hello @AswathiPS no returned mail that's strange !
    here is it the code

    main activity
    `
    [Activity(Label = "AppBroadCastNeverEnd", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
    protected override void OnCreate(Bundle bundle)
    {
    base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
    
            // bcEvery10Sec.bcEvery10Sec => if not work see this application
            InitBroadcast();
        }
        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(ToastBroadcast));
            AlarmManager alarmManager = (AlarmManager)GetSystemService(Context.AlarmService);
            int interval = 10000;
    
            alarmManager.SetRepeating(AlarmType.RtcWakeup, millis, interval, PendingIntent.GetBroadcast(this, 1, intentAlarm, PendingIntentFlags.UpdateCurrent));
        }
    }
    

    `

    the broadcast
    `

    [BroadcastReceiver]
    public class ToastBroadcast : BroadcastReceiver
    {
    public override void OnReceive(Context context, Intent intent)
    {
    Toast.MakeText(context, string.Format("THE TIME IS {0}", DateTime.Now.ToShortTimeString()), ToastLength.Long).Show();
    Vibrator vibrator = (Vibrator)context.GetSystemService(Context.VibratorService);
    vibrator.Vibrate(500);
    }
    }
    `

    and the manifest

    `
    uses-permission android:name="android.permission.INTERNET"
    uses-permission android:name="com.android.alarm.permission.SET_ALARM"
    uses-permission android:name="android.permission.VIBRATE"

    `

    hope this help

  • AswathiPSAswathiPS USMember ✭✭

    @ChristopheBERNARD , Thanks the solution is work for me , except some device (ie, product of MI's)

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭

    @AswathiPS alright => but except for some device that's pretty strange but i can't help you ..We work with only device Zebra TC 75

  • AswathiPSAswathiPS USMember ✭✭

    @ChristopheBERNARD Thank you ! :) :) :) :)

  • soufianemarlysoufianemarly Member ✭✭

    Hello @ChristopheBERNARD
    i saw your response
    and i was hoping that you can help me with the background task please i have a class that content the notification and i want to create a background task so i can receive notification while the add is in background
    Thank you

Sign In or Register to comment.