Xam.Plugins.Notifier无法在IOS 11上运行

时间:2017-10-12 05:27:28

标签: notifications xamarin.ios xamarin.forms

我正在使用Xam.Plugins.Notifier包在Xamarin.Forms项目中实现本地通知。

以下是我在PCL项目中编写的代码。 CrossLocalNotifications.Current.Show(" Title"," Description");

它在Android上运行良好,但它不适用于IOS。 我不确定它是否适用于较低的IOS sdk。 无论如何,它不适用于IOS 11。

以下是我在AppDelegate.cs中添加的代码

            if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
            {
                // Ask the user for permission to get notifications on iOS 10.0+
                UNUserNotificationCenter.Current.RequestAuthorization(
                    UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound,
                    (approved, error) => { });
            }
            else if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
            {
                // Ask the user for permission to get notifications on iOS 8.0+
                var settings = UIUserNotificationSettings.GetSettingsForTypes(
                    UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound,
                    new NSSet());

                UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
            }

有人可以帮我解决吗? 我想让这个包在IOS上工作。

感谢。

1 个答案:

答案 0 :(得分:3)

哪种情况不起作用?有效还是在后台?

如果它处于活动状态时不起作用,您可能忘记处理委托(子类UNUserNotificationCenterDelegate

修改您的代码如下:

if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
{
    // Ask the user for permission to get notifications on iOS 10.0+
    UNUserNotificationCenter.Current.RequestAuthorization(
        UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound,
        (approved, error) => { });

    // Watch for notifications while app is active
    UNUserNotificationCenter.Current.Delegate = new UserNotificationCenterDelegate();
}

创建子类UserNotificationCenterDelegate

public class UserNotificationCenterDelegate : UNUserNotificationCenterDelegate
{
    public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
    {
        // Tell system to display the notification anyway or use
        // `None` to say we have handled the display locally.
        completionHandler(UNNotificationPresentationOptions.Alert);
    }
}