So how in Xamarin do you create a local notification that runs when the app is offline

xardalodinxardalodin Member ✭✭

hi (cant post links)
currently trying to create local notifications without a service , so registerd an alarm

  var alarmIntent = new Intent(Android.App.Application.Context, typeof(AlarmReceiver));

            var pending = PendingIntent.GetBroadcast(Android.App.Application.Context, 0, alarmIntent, PendingIntentFlags.Immutable);
            var mAlarmservice = Android.App.Application.AlarmService;

            var alarmManager = (AlarmManager)Android.App.Application.Context.GetSystemService(mAlarmservice);
            alarmManager.SetInexactRepeating(AlarmType.RtcWakeup, SystemClock.ElapsedRealtime()+3000,60*1000, pending);

have set the class to be a broadcastreciver

   [BroadcastReceiver(Enabled =true, Exported = true)]
    public class AlarmReceiver : BroadcastReceiver

and setup the notification.

string message = "You have " + "TESTING" + " orders waiting for approval";

                var title = "TEST";

                var mintent = new Intent(context, typeof(MainActivity));
                mintent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);

                Random random = new Random();
                int pushCount = random.Next(9999 - 1000) + 1000; //for multiplepushnotifications

                var pendingIntent = PendingIntent.GetActivity(context, 0, mintent, PendingIntentFlags.CancelCurrent);

                var builder = new NotificationCompat.Builder(context, CHANNEL_ID)
                              .SetAutoCancel(true) // Dismiss the notification from the notification area when the user clicks on it
                              .SetContentTitle(title) // Set the title
                              .SetSmallIcon(Resource.Drawable.ic_media_play_dark) // This is the icon to display
                              .SetContentText(message); // the message to display.

                var notificationManager = NotificationManagerCompat.From(context);
                notificationManager.Notify(NOTIFICATION_ID, builder.Build());

So this works as long as the app is active. but stops running if I stop the app.

from what the internet says the OS is deleting the broadcastreciver , it could be because I am running a HuaWei phone.

how exactly do you prevent the OS from deleting the BroadcastReceiver, register it in android manifest.

either the context is wrong. or is it because I start it in xaml on a content page(interface to android class)

"receiver android:name=".AlarmReceiver"/" added this to android manifest but did nothing so probably wrong

add an intentfilter?
[BroadcastReceiver(Enabled = true)]
[IntentFilter(new[] { "com.xamarin.example.TEST" })] <-- how does this work..

dont understand this

running a huawei phone

So how in Xamarin do you create a local notification that runs when the app is offline without a service

Best Answer


  • LeonLuLeonLu Member, Xamarin Team Xamurai

    App Online is the the app is killed or cannot connect the network?
    If the app was killed, it cannot be achieved, all the threads of this app will be killed by android , you cannot create a local notification by this app.

    If app just cannot connect the network, the app is running, you can create a local notification in the UI thread,

    If you want to get the data from online.And you could use google play service, Firebase cloud messaging is a good choice.

  • xardalodinxardalodin Member ✭✭

    not a push notification .
    a local notification , as in it should wake upp using the alarm manager() (some how wake the OS up after on a timer)
    check every 30min when app not running(online)

    check if internet is on.
    if so use a restAPi get som info.
    if info found send a local notifikation.

    currently only works if the application is on.

    currently trying to figure out how to build a service that will run on phone allways.
    and starts on boot and app start(also know as a batteri drainer). and taskkillers will remove it.

    so repeating the question.. with modifications

    how in Xamarin do you create a local notification that runs when the app is not running, without a service

  • xardalodinxardalodin Member ✭✭

    so the app has to be running .
    for local(pull) notifications to work.
    in native android.
    no way around that?

    (Implicit broadcast: none that can be subscribed to? ) ok found article about "kiss goodbye to your implicit BroadcastReceivers" so thats out. all about saving power.

    so since I need a person to login in the morning,
    get the 24hour restapi token, so app can check every 30 min ,
    if there is anything to authorise. (so just dont turn the app off )

    dont know how Push notifications work, dont really know how the authentication server works.
    but since I cant rebuild it , firebase is out(develop-authentication-custom auth system.)

    so going with dont turn the app off option..

    so used to interupt timers , in smaller cpus. just set timer and wake up run code set timer etc etc.

    a well thanks

Sign In or Register to comment.