Handle local notification tap Xamarin Forms iOS

懵懂的女人 提交于 2020-01-17 01:39:06

问题


I have created an application on Xamarin Forms in which I am scheduling local notifications in PCL through a Plugin named Plugin.Notifications. But I wanted to know whether the user enters into the app through notification or not.

I tried to check whether the UIApplication.LaunchOptionsLocalNotificationKey is present in the launch 'options' dictionary or not, but the launch options dictionary is always null.

Then I tried to handle it through the ReceivedLocalNotification delegate method, and I was able to get the tap event, it works fine when the app is in foreground or in the background, but when the app gets killed and opens through tapping on the notification, its getting crashed.

I am unable to find the issue for the crash.

here is what I am doing in the ReceivedLocalNotification method.
I am setting a bool value through a DependencyService.

 public override void ReceivedLocalNotification(UIApplication application, UILocalNotification notification)
    {
        NSUserDefaults.StandardUserDefaults.Init();
        DependencyService.Get<INotificationTap>().SetNoitificationTap(true);
    }

The handling of the Dependency Service

[assembly: Xamarin.Forms.Dependency(typeof(NotificationTapIOS))]
namespace abc.iOS
{
    public class NotificationTapIOS:NSObject,INotificationTap
    {
        public bool GetNotificationTap()
       {
           return NSUserDefaults.StandardUserDefaults.BoolForKey("notificationTapKey");
       }

       public void SetNoitificationTap(bool isNotificationTapped)
       {
     NSUserDefaults.StandardUserDefaults.SetBool(isNotificationTapped,"notificationTapKey");
        NSUserDefaults.StandardUserDefaults.Synchronize();
       }
}

回答1:


public override bool FinishedLaunching(UIApplication app, NSDictionary options)

{

if (options != null)
        {
            // check for a local notification
            if (options.ContainsKey(UIApplication.LaunchOptionsLocalNotificationKey))
            {
                var localNotification = options[UIApplication.LaunchOptionsLocalNotificationKey] as UILocalNotification;
                if (localNotification != null)
                {
                    LoadApplication(new App());
                    UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;// reset our badge
                }
            }
        }
else
        {
            LoadApplication(new App());
        }
}



回答2:


I found a workaround for this issue, I don't know whether this is a correct approach, but it worked for me.

In Xamarin.iOS , when the app launched , the base class methods are called first(i.e applicationDidFinishLaunching) and then the Xamarin part(i.e FinishedLaunching) is called.

So I changed the FinishedLaunching parameters names , similar to the base class parameters (i.e 'app' to 'uiApplication' and 'options' to 'launchOptions' ) and I got the key in the launchOptions Dictionary.

 public override bool FinishedLaunching(UIApplication uiApplication, NSDictionary launchOptions)
    {
        global::Xamarin.Forms.Forms.Init();

        isNotificationTapped = false;

        if (launchOptions != null) {
            if (launchOptions.ContainsKey(UIApplication.LaunchOptionsLocalNotificationKey))
               isNotificationTapped = true;
        }

        LoadApplication(new App());
        return base.FinishedLaunching(uiApplication, launchOptions);
    }

When the user starts the app through notification tap, then FinishedLaunching method of AppDelegate is executed and we can validate the launchOptions dictionary to find the tap event, and if the app is in background or running stage , and user taps on the notification then the RecieveLocalNotification method of the AppDelegate is called.

  public override void ReceivedLocalNotification(UIApplication application, UILocalNotification notification)
    {
        isNotificationTapped = true;
    }

After initializing the value , I saved it in the UserDefaults using the DependencyService



来源:https://stackoverflow.com/questions/52229624/handle-local-notification-tap-xamarin-forms-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!