处理本地通知点击 Xamarin Forms iOS [英] Handle local notification tap Xamarin Forms iOS

查看:98
本文介绍了处理本地通知点击 Xamarin Forms iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Xamarin Forms 上创建了一个应用程序,我在其中通过名为 Plugin.Notifications 的插件在 PCL 中安排本地通知.但我想知道用户是否通过通知进入应用程序.

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.

我试图检查 UIApplication.LaunchOptionsLocalNotificationKey 是否存在于启动选项"字典中,但启动选项字典始终为空.

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.

然后我尝试通过 ReceivedLocalNotification 委托方法处理它,并且我能够获得点击事件,当应用程序在前台或后台时它工作正常,但是当应用程序被杀死并通过点击打开时通知,它崩溃了.

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.

我找不到崩溃的问题.

这是我在 ReceivedLocalNotification 方法中所做的.
我正在通过 DependencyService 设置 bool 值.

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);
    }

依赖服务的处理

[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();
       }
}

推荐答案

我找到了解决此问题的方法,我不知道这是否是正确的方法,但对我有用.

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

在 Xamarin.iOS 中,当应用启动时,首先调用基类方法(即 applicationDidFinishLaunching),然后调用 Xamarin 部分(即 FinishedLaunching).

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.

所以我改变了 FinishedLaunching 参数名称,类似于基类参数(即app"到uiApplication"和options"到launchOptions"),我在launchOptions字典中得到了密钥.

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);
    }

当用户通过通知点击启动应用时,AppDelegate 的 FinishedLaunching 方法被执行,我们可以验证 launchOptions 字典来找到点击事件,如果应用在后台或运行阶段,用户点击通知然后调用 AppDelegate 的 RecieveLocalNotification 方法.

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;
    }

初始化值后,我使用 DependencyService 将其保存在 UserDefaults 中

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

这篇关于处理本地通知点击 Xamarin Forms iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆