2017-10-12 70 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您提供的代碼應該在後臺工作。 –

相關問題