Implement background service to download data RRS feed

  • Question

  • User292725 posted

    I am implementing the app in xamarin form and have download button in same to download the data but I want to download data in background even when app is not running How we implement the same in xamarin form in all platforms android/ios/windows phone

    Tuesday, January 24, 2017 3:41 AM

All replies

  • User292725 posted

    I try to create demo app for same and implement the following code As per my assumption the service need to be hit in every 30 second please check and suggest me if i miss something

    In MainActivity of Droid

    public void ShowNotification() { Intent alarmIntent = new Intent(Android.App.Application.Context, typeof(AlarmReceiver)); alarmIntent.PutExtra("message", "Message"); alarmIntent.PutExtra("title", "Title"); PendingIntent pendingIntent = PendingIntent.GetBroadcast(Android.App.Application.Context, 0, alarmIntent, PendingIntentFlags.UpdateCurrent); AlarmManager alarmManager = (AlarmManager)Android.App.Application.Context.GetSystemService(Context.AlarmService);
    alarmManager.Set(AlarmType.RtcWakeup, DateTime.Now.Millisecond + 30000, pendingIntent); }

    After that I create BroadcastReceiver as below

    namespace testApp { [BroadcastReceiver] [IntentFilter(new string[] { "android.intent.action.BOOT_COMPLETED" }, Priority = (int)IntentFilterPriority.LowPriority)] public class AlarmReceiver : BroadcastReceiver { public override void OnReceive(Context context, Intent intent) { var message = intent.GetStringExtra("message"); var title = intent.GetStringExtra("title"); var notIntent = new Intent(context, typeof(MainActivity)); var contentIntent = PendingIntent.GetActivity(context, 0, notIntent, PendingIntentFlags.CancelCurrent); var manager = NotificationManagerCompat.From(context); var style = new NotificationCompat.BigTextStyle(); style.BigText(message); //Generate a notification with just short text and small icon var builder = new NotificationCompat.Builder(context) .SetContentIntent(contentIntent) .SetSmallIcon(Resource.Drawable.AlertDarkFrame) .SetContentTitle(title) .SetContentText(message) .SetStyle(style) .SetWhen(Java.Lang.JavaSystem.CurrentTimeMillis()) .SetAutoCancel(true); var notification = builder.Build(); manager.Notify(0, notification); }
    } }

    After that I modified the menifest as bellow

    <receiver android:name=".AlarmReceiver" android:enabled="true" android:exported="true" android:process=":remote"

    android:label="AlarmReceiver" />

    Tuesday, January 24, 2017 8:23 AM