Apple push notification service in Xamarin.Mac behavior

JitheshChandrasekharanJitheshChandrasekharan USMember ✭✭
edited July 19 in Xamarin.Mac

I am working on adding Remote push notification support in my Xamarin.Mac application. I can successfully receive the push notification in my PC via apns and when I click the notification banner this is the behavior I am seeing.

  1. If my** app is not running**, it will launch the application. But never hits on "ReceivedRemoteNotification". Is this expected behavior ? Is there any way to receive the apns payload in this case ?

  2. If my app is running I get the payload via "ReceivedRemoteNotification".

Any help really appreciated.

thanks,
Jithesh

Best Answer

  • Accepted Answer

    I figured it out. You will get payload through NSNotification when we get launched through a notification.

    public override void DidFinishLaunching(NSNotification notification)
            {
                var uInfo = notification.UserInfo;
                if(uInfo["NSApplicationLaunchUserNotificationKey"] != null)
                {
                    var payLoad = uInfo["NSApplicationLaunchUserNotificationKey"] as NSUserNotification;
                    if(payLoad.ActivationType == NSUserNotificationActivationType.ActionButtonClicked)
                    {
                        NSAlert alert = new NSAlert();
                        alert.MessageText = "Button Clicked";
                        alert.RunModal();
                    }
                    var userInfo = payLoad.UserInfo;
    
                    //not.ActivationType == 
                    if (userInfo != null && userInfo.ContainsKey(new NSString("aps")))
                    {
                        //Get the aps dictionary
                        NSDictionary aps = userInfo.ObjectForKey(new NSString("aps")) as NSDictionary;
                        if (aps.ContainsKey(new NSString("content")))
                        {
                            var deepLink = (aps[new NSString("content")] as NSString).ToString();
                            NSAlert alert = new NSAlert();
                            alert.MessageText = deepLink;
                            alert.RunModal();
                            //DisplaySubView(deepLink);
                            //Console.WriteLine($"Deeplink : {deepLink}");
                        }
                    }
    
                }
    

Answers

  • JitheshChandrasekharanJitheshChandrasekharan USMember ✭✭
    Accepted Answer

    I figured it out. You will get payload through NSNotification when we get launched through a notification.

    public override void DidFinishLaunching(NSNotification notification)
            {
                var uInfo = notification.UserInfo;
                if(uInfo["NSApplicationLaunchUserNotificationKey"] != null)
                {
                    var payLoad = uInfo["NSApplicationLaunchUserNotificationKey"] as NSUserNotification;
                    if(payLoad.ActivationType == NSUserNotificationActivationType.ActionButtonClicked)
                    {
                        NSAlert alert = new NSAlert();
                        alert.MessageText = "Button Clicked";
                        alert.RunModal();
                    }
                    var userInfo = payLoad.UserInfo;
    
                    //not.ActivationType == 
                    if (userInfo != null && userInfo.ContainsKey(new NSString("aps")))
                    {
                        //Get the aps dictionary
                        NSDictionary aps = userInfo.ObjectForKey(new NSString("aps")) as NSDictionary;
                        if (aps.ContainsKey(new NSString("content")))
                        {
                            var deepLink = (aps[new NSString("content")] as NSString).ToString();
                            NSAlert alert = new NSAlert();
                            alert.MessageText = deepLink;
                            alert.RunModal();
                            //DisplaySubView(deepLink);
                            //Console.WriteLine($"Deeplink : {deepLink}");
                        }
                    }
    
                }
    
Sign In or Register to comment.