2017-10-12 3 views
0

J'utilise le package Xam.Plugins.Notifier pour implémenter Local Notification dans le projet Xamarin.Forms.Xam.Plugins.Notifier ne fonctionne pas sur IOS 11

Voici le code que j'ai écrit dans le projet PCL. CrossLocalNotifications.Current.Show ("Titre", "Description");

Cela fonctionne bien sur Android, mais cela ne fonctionne pas sur IOS. Je ne suis pas sûr si cela fonctionne sur SDK inférieur IOS. Quoi qu'il en soit, il ne fonctionne pas sur IOS 11.

Voici le code que j'ai ajouté dans 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); 
      } 

Quelqu'un peut-il me aider à résoudre ce problème? Je veux que ce paquet fonctionne sur IOS.

Merci.

Répondre

0

Quel scénario ne fonctionne-t-il pas? Actif ou en arrière-plan?

Si cela ne fonctionne pas quand il est actif, vous pouvez oublier de gérer le délégué (sous-classes UNUserNotificationCenterDelegate)

Modifier votre code comme ci-dessous:

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

Créer une sous-classe 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

merci pour votre réponse, je l'ai déjà manipulé moi-même. Ce qui devrait être ajouté si cela ne fonctionne pas quand il est en arrière-plan. –

+0

@ Passionate.C Le code que vous avez fourni devrait fonctionner en arrière-plan. –