Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OneSignal: How to Handle notificationOpened in AppDelegate of a Xamarin.Forms app?

I am working on implementing OneSignal push-notification in Xamarin.Forms.

I need to pass the string returned by OneSignal AdditionalData into the constructor of App().

So I used HandleNotificationOpened(OSNotificationOpenedResult result) for handling the notification tap and fetching the string and then pass it to LoadApplication(new App(myData)).

So for this, I have written the code in MainActivity for Android and in AppDelegate for iOS.

Everything is working fine for Android; i.e. the HandleNotificationOpened() fetched the additionalData and passes it toLoadApplication(new App(myData)).

But in iOS, when I opened the notification, the HandleNotificationOpened() code is not called.

AppDelegate.cs

static string s = null;

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    OneSignal.Current.StartInit("MyKey").HandleNotificationOpened(HandleNotificationOpened).EndInit();
    
    if(s!=null)
    {
        LoadApplication(new App(s));
    }
    else
    {
        LoadApplication(new App("myUrl.lasso")); 
    }

    return base.FinishedLaunching(app, options);
}

private static void HandleNotificationOpened(OSNotificationOpenedResult result)
{
    OSNotificationPayload payload = result.notification.payload;
    Dictionary<string, object> additionalData = payload.additionalData;

    if (additionalData != null)
    {
        if (additionalData.ContainsKey("url_direct"))
        {
            s = additionalData["url_direct"].ToString();
            System.Diagnostics.Debug.WriteLine("We need to redirect it to: " + s);
        }
    }
}
like image 467
Apoorv Mehrotra Avatar asked Aug 30 '17 05:08

Apoorv Mehrotra


1 Answers

Answer

Override the DidReceivedRemoteNotification event in AppDelegate.

Code

public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, System.Action<UIBackgroundFetchResult> completionHandler)
{
    base.DidReceiveRemoteNotification(application, userInfo, completionHandler);

    NSDictionary aps = userInfo.ObjectForKey(new NSString("aps")) as NSDictionary;
            
    string alert = string.Empty;
    if (aps.ContainsKey(new NSString("alert")))
        alert = (aps [new NSString("alert")] as NSString).ToString();

    if (application.ApplicationState == UIApplicationState.Active)
    {
        //app is currently active, you can get your payload (userInfo) data here
    }
    else if (application.ApplicationState == UIApplicationState.Background)
    {
        //app is in background, you can get your payload (userInfo)
    }
    else if (application.ApplicationState == UIApplicationState.Inactive)
    {
        //app is transitioning from background to foreground (user taps notification), do what you need when user taps here, you can get your payload (userInfo)
    }
}
like image 51
Pavan V Parekh Avatar answered Oct 19 '22 06:10

Pavan V Parekh