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.
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);
}
}
}
Override the DidReceivedRemoteNotification event in AppDelegate.
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)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With