0

J'ai créé ExtensionDelegate mon UNUserNotificationCenterDelegate et l'ai assigné correctement. J'ai mon application watchos 3 ajouter une demande de notification, ce qui déclenchewatchOS 3 - Que dois-je faire dans userNotificationCenter: willPresentNotification: withCompletionHandler: pour afficher réellement la notification?

userNotificationCenter:willPresentNotification:withCompletionHandler: 

J'ai mis en œuvre la méthode comme suit:

- (void) userNotificationCenter:(UNUserNotificationCenter*)center 
     willPresentNotification:(UNNotification*)notification 
      withCompletionHandler:(void(^)(UNNotificationPresentationOptions))completionHandler 
{ 
    NSLog(@"ExtensionDelegate: willPresent!"); 

    completionHandler(UNNotificationPresentationOptionAlert); 
} 

Le problème est que rien ne se passe. Je débogue dans Xcode 8.2 sur un périphérique exécutant watchOS 3.1, et bien que cette méthode de délégué soit déclenchée, et je peux frapper le point d'arrêt, aucune alerte n'est montrée. Mon contenu est le suivant:

UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init]; 
    content.title = notificationType; 
    content.subtitle = [NSString stringWithFormat:@"Subtitle: %@", notificationType]; 
    content.body = alertTitle; 
    content.badge = @1; 
    content.sound = [UNNotificationSound defaultSound]; 
    content.launchImageName = @"LaunchImage"; 
    content.userInfo = @{ 
         @"notificationType" : notificationType, 
         @"count" : @(count) 
         }; 
    content.attachments = @[]; 
    content.categoryIdentifier = notificationType; 

où NotificationType est le NSString que je mets sur le lancement de mon application comme suit:

// Register custom notification types 
    UNNotificationCategory* cat1 = [UNNotificationCategory categoryWithIdentifier:@"Category1" 
                      actions:@[dismissAction] 
                   intentIdentifiers:@[] 
                      options:UNNotificationCategoryOptionNone]; 

    UNNotificationCategory* cat2 = [UNNotificationCategory categoryWithIdentifier:@"Category2" 
                      actions:@[dismissAction, yesAction] 
                    intentIdentifiers:@[] 
                      options:UNNotificationCategoryOptionNone]; 
    NSSet* categorySet = [NSSet setWithArray:@[cat1, cat2]]; 
    [center setNotificationCategories:categorySet]; 

Malheureusement, lorsque la demande de notification est faite, willPresentNotification vient et va, le completionHandler est appelé, puis l'application continue à courir au premier plan. Aucune alerte de notification n'apparaît à l'écran. Il n'apparaît pas non plus dans le dock déroulant du cadran. Que dois-je faire d'autre pour l'afficher, que ce soit à l'écran à l'écran et à l'intérieur, ou dans le dock déroulant?

Voici un exemple visuel de ce que je vous attendriez à voir au-dessus de l'application ou au ras-dessus avec le haut de l'écran, avec l'application en cours d'exécution en dessous. La bannière peut être encore plus mince que montré ici:

Simple watchOS Notification

+0

Avez-vous demander l'autorisation d'envoyer une notification? À partir du guide de programmation de la montre: avant que le système puisse publier des alertes ou diffuser des sons pour les notifications de votre application, votre application de surveillance ou votre application iOS doit demander l'autorisation d'interagir avec l'utilisateur. Pour ce faire, appelez la méthode requestAuthorizationWithOptions: completionHandler: de l'objet UNUserNotificationCenter partagé. – lostAtSeaJoshua

+0

Non, j'ai demandé l'autorisation au démarrage et l'ai obtenu au démarrage. J'ai juste besoin d'envoyer l'alerte elle-même. Comme une bannière sur le haut de l'écran de la montre qui apparaît, puis disparaît. Devons-nous créer cela comme un WKInterfaceView (ou quoi que ce soit) nous-mêmes? – Cindeselia

+0

Ce qui n'est pas clair pour moi, c'est quand le willPresentNotification du délégué est appelé, que devons-nous faire pour afficher la notification? Je n'ai pas besoin d'un comportement personnalisé tape-à-l'œil comme changer d'interface - j'ai juste besoin de la petite bannière qui s'affiche (certains designers l'appellent «pancake» ou «toast») en haut de l'écran, avec l'icône de l'application et un une seule ligne de texte. "Objectif atteint!" ou peu importe. C'est le genre de notification que je verrais dans le menu déroulant sur le cadran par exemple; J'ai juste besoin de montrer ça. Dois-je coder cela à partir de zéro? On dirait que nous ne l'obtenons pas gratuitement. – Cindeselia

Répondre

0

question connexe, mais pour iOS: Displaying a stock iOS notification banner when your app is open and in the foreground?

J'ai essayé la même chose sur la montre et obtenir le même (avis de non-affichage) résultat. Une note, dans le cas où cela aide les autres, est que nous devons demander la même permission que nous retournons dans le gestionnaire de complétion. Par exemple, si nous voulons afficher les notifications Alert, nous devons utiliser les deux valeurs correspondantes comme indiqué ci-dessous. Comme le PO l'a noté, nous devons nous définir en tant que délégué du centre de notification.

(BTW, si j'ai des erreurs ici, s'il vous plaît les signaler - le mien ne fonctionne pas encore non plus).

Le code suivant figure dans le délégué d'extension watchkit.

- (void)applicationDidFinishLaunching { 
    // ... 
    // Register for, and request permission to send notifications 
    UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter]; 
    center.delegate = self; 
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert) 
         completionHandler:^(BOOL granted, NSError * _Nullable error) { 
          // Enable or disable features based on authorization. 
          NSLog(@"Got auth response: %d", granted); 
         }]; 
} 

Ce qui précède est réussi uniquement lorsque j'ai les autorisations de notification définies sur "Miroir l'iPhone". Si je l'éteins, ma demande d'autorisation échoue. Un problème différent, mais s'il vous plaît vérifier la valeur granted retournée et assurez-vous que c'est true.

// Foreground notification 
- (void)userNotificationCenter:(UNUserNotificationCenter *)center 
     willPresentNotification:(UNNotification *)notification 
      withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler { 
    // The method will be called on the delegate only if the 
    // application is in the foreground. 
    NSLog(@"Will present notification: %@", notification); 
    // The following doesn't present the notification, though. 
    completionHandler(UNNotificationPresentationOptionAlert); 
} 

La méthode userNotificationCenter:willPresentNotification:withCompletionHandler ne nous appelle, mais je ne vois pas une superposition de notification, ni est-il un dans la liste des notifications.