shinyorg / shiny

.NET Framework for Backgrounding & Device Hardware Services (iOS, Android, & Catalyst)
https://shinylib.net
MIT License
1.44k stars 227 forks source link

[Bug]: Notification + Background Job #1368

Closed South2AK closed 8 months ago

South2AK commented 8 months ago

Component/Nuget

Jobs (Shiny.Jobs)

What operating system(s) are effected?

Version(s) of Operation Systems

17.2.1 IOS Android 14

Hosting Model

Steps To Reproduce

in Code Sample

Expected Behavior

After the user has clicked on start, it should start a counter and set a DateTime into Preferences

When more than 8 hours (in the codesample I´ve set it to one minute so i won´t have to wait that long) it should show a notification saying "App is runing since more than 8 hours" so the user is reminded

Actual Behavior

Nothing happens, whether in console or in app

Exception or Log output

No response

Code Sample

Hey,

how can I actually use a combination of both?

What I want to to is the following:

In my app there is a start / stop button which sets a DateTime into preferences and when ever a user has the app in background I want the App to remind him after 8 Hours that he still is on running

This is my Job:

public class TimeCheckJob : Shiny.NotifyPropertyChanged, IJob
{
    public readonly INotificationManager notificationManager;
    public TimeCheckJob(INotificationManager notificationManager)
    {
        this.notificationManager = notificationManager;
    }

    public Task Run(JobInfo jobInfo, CancellationToken cancelToken)
    {
        Console.WriteLine(DateTime.Now.ToString("dd.MM.yyyy HH:mm") + " Running");
        if (Preferences.Get("StartTime", "").Length > 0)
        {
            if (Preferences.Get("Started", "false") == "true")
            {
                DateTime startTime = DateTime.Parse(Preferences.Get("StartTime", ""));
                if (DateTime.Now - startTime > TimeSpan.FromMinutes(1))
                {
                    var notification = new Notification
                    {
                        Title = "Zeit",
                        Message = "Mehr als 8 Stunden sind vorbei, bitte prüfe deine Arbeitszeiten!"
                    };
                    notificationManager.Send(notification);
                }
            }
        }
        return null;
    }
}

This is mauiprogram.cs (ofc .UseShiny() is set)

            var job = new Shiny.Jobs.JobInfo("TimeCheckJob", typeof(TimeCheckJob), true, null, InternetAccess.Any, false, false, true);
            builder.Services.AddJob(job);

Is there anything else that I need to set in order for this to work? Android Manifest has been Updated (Post Notifications is active), Apple Item.plist has also been added

Code of Conduct

aritchie commented 8 months ago

Jobs are periodic, not scheduled, so your timing won't work out well.

You have to use RequestAccess for notifications (and pretty much everything in Shiny) in the foreground to get the users permission.

South2AK commented 8 months ago

Hi Aritchie,

I did ask for the permissions and allowed them on the phone, but it doesnt Show anything on where ios nor android even though i ve been running it for hours now.

Any ideas?

aritchie commented 8 months ago

Im not going to guess. You have to submit a sample.

Try sending a notification in the foreground to ensure notifications is setup properly. Jobs will run, but it can be hours before they do

South2AK commented 8 months ago

I´ll create a example as fast as I can but it will take some time...

Foreground works as intended.

Also what I´ve found out is that in foreground the console has a log with shinyjobs entries in it, but when I set RunOnForeground to false nothing get´s written in it

aritchie commented 8 months ago

I would encourage you to look into the code and how the perspective platforms work. You basically have to wait until jobs start running. I run this scenario all over the place.

South2AK commented 8 months ago

Okay...

I left them open yesterday for the whole day but nothing really happened unfortunately. What I just found out is, the first time I open the App on android or on IOS I´ll receive a notification immediately.

Is there maybe a task needed which repeats it self in background mode?

aritchie commented 8 months ago

If you're swiping away the app or killing the debugger, you're killing the app which prevents things from running.

The sample app in this repo does this stuff and works. Please provide a reproducible sample, I'll take a look, but this seems like a case of misunderstanding of how periodic jobs work on the platforms.