2017-10-12 71 views
0

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

这是我在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上工作。

谢谢。

回答

0

哪种情况不起作用?活跃还是在后台?

如果它不工作时,它是积极的,你可能会忘记办理委托(子类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); 
    } 
} 
+0

感谢您的回复,我已经自己处理了。如果它在后台不起作用,应该添加什么。 –

+0

@ Passionate.C您提供的代码应该在后台工作。 –

相关问题