Question:
When a push is sent, we are sending some custom data with it.
On Android, this custom data is read, and when the push notification is tapped, the resulting action works with the custom data that was sent.
I'm having trouble finding any documentation or examples on how to hook in to this same behaviour on iOS, I was hoping you might be able to point me in the right direction?
Answer:
To retrieve a push payload, you can use the following code snippet:
using System;
using System.Collections.Generic;
using System.Linq;
using Foundation;
using UIKit;
using Pushwoosh;
using UserNotifications;
namespace demoapp.iOS
{
[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public class PushDelegate : PushNotificationDelegate
{
public override void OnPushAccepted(PushNotificationManager pushManager, NSDictionary pushNotification)
{
Console.WriteLine("Push accepted: " + pushNotification); // replace with your pushNotification handling code
}
}
public PushDelegate _pushDelegate;
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
PushNotificationManager pushManager = PushNotificationManager.PushManager;
_pushDelegate = new PushDelegate();
pushManager.Delegate = _pushDelegate;
UNUserNotificationCenter.Current.Delegate = pushManager.NotificationCenterDelegate;
if (options != null)
{
if (options.ContainsKey(UIApplication.LaunchOptionsRemoteNotificationKey))
{
pushManager.HandlePushReceived(options);
}
}
pushManager.RegisterForPushNotifications();
return base.FinishedLaunching(app, options);
}
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
PushNotificationManager.PushManager.HandlePushRegistration(deviceToken);
}
public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
{
PushNotificationManager.PushManager.HandlePushRegistrationFailure(error);
}
public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
{
PushNotificationManager.PushManager.HandlePushReceived(userInfo);
}
}
}
The output of Console.WriteLine("Push accepted: " + pushNotification) is:
default 16:05:02.688094 +0700 demoapp.iOS Push accepted: {
aps = {
alert = "test push";
sound = default;
};
p = 2is;
}
You can get a value of any key inside pushNotification dictionary and then handle it as you wish.
Comments
0 comments
Please sign in to leave a comment.