EnteringForeground example

Hi,

In this thread I understand there’s something equivalent to didFinishLaunchingWithOptions which is Uno.Platform2.Application.EnteringForeground.

Is there any example on how to use this in a UNO file? Can’t find any, and the example in the thread is not clear to me…

And what is the diference to this JS event: Lifecycle.onEnteringForeground?

Thank you.

didFinishLaunchingWithOptions is not equivalent to EnteringForeground, that was a mistake from our side.

If you want an event that is called once as the app starts use: Fuse.Platform.Lifecycle.Started

If you want an event that is called every time the app moves from background to foreground use: Fuse.Platform.EnteringForeground

Fuse.Platform.EnteringForeground is equivalent to the JS Lifecycle.onEnteringForeground event

Don’t use the Uno.Platform.Application events as we are making big changes to the lower levels of Uno right now and I don’t want you to get bitten by those :slight_smile:

Fuse.Platform.Lifecycle.Started += OnStarted;     

static void OnStarted(Fuse.Platform.ApplicationState newState)
{
   debug_log "And here we are!";
}

To be honest though, if you are writing Uno you are probably making a module for JS right? In that case you will have a class like this:

[UXGlobalModule]
public sealed class Foo : NativeModule
{
    public Foo()
    {
        ..stuff..
    }
}

You can just put your initialization code in the constructor. We will then make sure the constructor is called as the app starts and you don’t have to worry about hooking/unhooking to uno events.

I hope this helps :slight_smile:

Hi Chris,

Thank you for the comprehensive explanation.

I’ve tried an approach like you suggested, but in fact, the SDK I was trying to port to Fuse (OneSignal push notifications) seems to need the didFinishLaunchingWithOptions method because it expects the launchOptions parameter.

This way, I was able to do it using CocoaPods and “injecting” the code like this:

[extern(iOS) Require("Cocoapods.Podfile.Target", "pod 'OneSignal'")]

[extern(iOS) Require("AppDelegate.SourceFile.Declaration", "#import <OneSignal/OneSignal.h>")]

[extern(iOS) Require("AppDelegate.SourceFile.DidFinishLaunchingWithOptions", "[OneSignal initWithLaunchOptions:launchOptions appId:@\"my-onesignal-id\"];")]

This seems to work (credits to @bolav) but it’s not very elegant in my opinion and not easy to read or maintain…

Is there a better way to do this?

Hi again fernando,
No, currently we don’t expose the launchOptions in any way (other than the workaround you have used). Maybe we need something akin to startup args from regular desktop apps which would let you get the options. I remember looking into this before and finding it not a good fit but I’ll add launchOptions to our internal wishlist so we can revisit it and maybe find some better solution.

Thanks for writing up your solution and am glad to hear you’ve made progress despite the difficulties.

Hi Fernando,
Just a quick message to say we are going to cache the launchOptions so they will be available from Uno. If all goes to plan they will be available as an ObjC.Object property as Uno.Platform.iOS.Application.LaunchOptions in the next release.

Hi Chris,
Was this included yet? I can’t seem to find any reference to it in the docs.

Oo good catch, that was not documented yet. However it has shipped so you should be able to access it from Uno.Platform.iOS.Application.LaunchOptions and as planned it is an ObjC.Object

Hi Fernando / Chris

I’m following the same steps (https://documentation.onesignal.com/docs/ios-sdk-setup) to implement OneSignal Push Notifications.

At the step 4, I can’t find the AppDelegate.m file, i found AppDelegate.mm, so I used that file.

Then the generated UNO code shows as:

    return [_unoContext
        application:application
        willFinishLaunchingWithOptions:launchOptions];

whereas the tutorial says:

   return YES;

When compiling, the preprocessor points in _config.h:

'uBase/Config.h' file not found

Fernando, you mentioned that OneSignal is working in your project, can you share some insights or keypoints to make it work?

Chris, how can I use Uno.Platform.iOS.Application.LaunchOptions to achieve OneSignal to work?