【发布时间】:2019-02-13 05:24:50
【问题描述】:
我在 Xamarin Forms 上创建了一个应用程序,我在其中通过名为 Plugin.Notifications 的插件在 PCL 中安排本地通知。但是我想知道用户是否通过通知进入应用程序。
我尝试检查 UIApplication.LaunchOptionsLocalNotificationKey 是否存在于启动“选项”字典中,但启动选项字典始终为空。
然后我尝试通过 ReceivedLocalNotification 委托方法来处理它,并且我能够获取点击事件,当应用程序处于前台或后台时它工作正常,但是当应用程序被杀死并通过点击打开时通知,它正在崩溃。
我无法找到崩溃的问题。
这是我在 ReceivedLocalNotification 方法中所做的。
我正在通过 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();
}
}
【问题讨论】:
标签: ios xamarin.forms xamarin.ios nsuserdefaults localnotification