pourquoi didRegisterForRemoteNotificationswithdevicetoken n'est pas appelé

je fais une application dans laquelle je veux implémenter le service de notification Apple push. Je suis les instructions étape par étape données dans ce tutoriel .

mais encore, les méthodes ne sont pas appelées. Je ne sais pas ce qui est à l'origine du problème. Quelqu'un peut-il m'aider?

    - (void)application:(UIApplication *)appdidRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 
        //NSString * token = [[NSString alloc] initWithData:deviceTokenencoding:NSUTF8StringEncoding];
        NSString *str = [NSString stringWithFormat:@"Device Token=%@",deviceToken];
        NSLog(@"Device Token:%@",str);

        //NSLog(@"Device token is called");
        //const void *devTokenBytes = [deviceToken bytes];
        //NSLog(@"Device Token");
    }

    - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err { 
        NSString *str = [NSString stringWithFormat: @"Error: %@", err];
        NSLog(@"Error:%@",str);    
    }
82
demandé sur Willy 2010-11-03 14:31:46

19 réponses

j'ai eu la même question: appelant registerForRemoteNotificationTypes: n'a invoqué ni application:didRegisterForRemoteNotificationsWithDeviceToken: ni application:didFailToRegisterForRemoteNotificationsWithError:

j'ai finalement résolu cette question à l'aide de la note technique D'Apple TN2265 .

C'est ce que j'ai fait:

tout d'abord, j'ai vérifié deux fois que je suis bien s'enregistrer correctement pour les notifications Push , y compris la vérification de mon profil d'approvisionnement pour " aps-environnement" la clé et le codesigning de la .application fichier lui-même. J'avais tous mis en place correctement.

j'ai ensuite dû déboguer les messages D'état de notification Push dans la console (vous devez installer PersistentConnectionLogging.mobileconfig provisioning profile sur votre appareil et redémarrez-le. Voir TN2265 sous" Observing Push Status Messages"). J'ai remarqué que le processus apns démarre une minuterie et calcule une date de feu minimum, ce qui m'a fait soupçonner que la notification Push-Notification le message de confirmation d'inscription, qui est normalement présenté à ce point, est supprimé par APNS, tel qu'indiqué dans L'AMT2265:

Réinitialisation des Notifications Push permissions Alert on iOS

la première fois qu'une application activée par push enregistre des notifications push, iOS demande à l'utilisateur s'il souhaite recevoir des notifications pour cette application. Une fois que l'Utilisateur a répondu à cette Alerte, elle n'est plus présentée, sauf si l'appareil est restauré ou l'application a été désinstallé depuis au moins un jour.

Si vous voulez simuler un premier temps d'exécution de votre application, vous pouvez quitter l'application désinstallée pour une journée. Vous pouvez obtenir la dernière sans réellement attendre un jour par le réglage de l'horloge système de l'avant une journée ou plus, éteindre l'appareil complètement, puis en tournant le périphérique.

Donc, j'ai supprimé l'application de l'appareil, puis modifié manuellement l'iPhone date dans les Paramètres, redémarré l'appareil, et ré-installé l'application.

la prochaine fois que mon code a appelé registerForRemoteNotificationTypes , il a reçu des callbacks comme prévu.

Cela a résolu le problème pour moi. Espérons que cela aide.

69
répondu 100grams 2011-11-07 11:48:58

Dans iOS 8, certaines méthodes sont obsolètes. Suivez les étapes ci-dessous pour la compatibilité iOS 8

1. Notification du registre

if([[UIDevice currentDevice] systemVersion].floatValue >= 8.0)
{
    UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];    
}
else
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound|UIRemoteNotificationTypeBadge)];
}

2. Ajouter 2 nouvelles méthodes

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    //register to receive notifications
    [application registerForRemoteNotifications];
}

//For interactive notification only
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
{
    //handle the actions
    if ([identifier isEqualToString:@"declineAction"]){
    }
    else if ([identifier isEqualToString:@"answerAction"]){
    }
}

Note: ci-dessus deux nouvelles méthodes sont requises dans iOS 8 en plus de didRegisterForRemoteNotificationsWithDeviceToken et didReceiveRemoteNotification ..Sinon délégué méthode ne sera pas invoquée.

voir: notification à distance iOS 8

66
répondu Community 2017-05-23 12:18:04

In iOS 8 , en plus de demander l'accès à la notification push différemment, vous devez également vous inscrire différemment.

Demande D'Accès:

if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    // iOS 8
    UIUserNotificationSettings* settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
} else {
    // iOS 7 or iOS 6
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}

Handle de périphérique enregistré:

// New in iOS 8
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
    [application registerForRemoteNotifications];
}

// iOS 7 or iOS 6
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
    // Send token to server
}
26
répondu Kyle Clegg 2014-09-27 23:15:20

assurez-vous d'appeler votre code (mise à jour selon les types de notification pris en charge)

[[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];

et le profil d'approvisionnement est activé APNS. Vous devrez peut-être télécharger à nouveau le profil d'approvisionnement après avoir activé APNS. Si vous avez des problèmes et que vous avez des erreurs, alors peut-être que vous devriez créer des droits.ajouter la touche "aps-environnement" avec la valeur "développement" ou" production " en fonction du type de construction (normalement cette paire de clé-valeur est contenue dans le profil d'approvisionnement, mais parfois Xcode leur fait du mal).

12
répondu Lorenzo Bevilacqua 2011-02-02 14:08:45

garder à l'esprit que les notifications à distance ne sont pas prises en charge dans le simulateur. Par conséquent, si vous exécutez votre application dans le simulateur, didRegisterForRemoteNotificationsWithDeviceToken ne sera pas appelé.

10
répondu Eric 2015-06-11 12:10:52

si les profils de provisionnement sont utilisés auparavant pour activer et configurer le service de notification Apple Push, vous devrez à nouveau Télécharger les profils de provisionnement.

supprimer les profils d'approvisionnement de L'Organisateur Xcode et de l'iPhone/iPad. Passez à Settings -> General -> Profiles -> [Your provisioning] -> Remove .

installez les nouveaux profils d'approvisionnement téléchargés. Ensuite nettoyez et exécutez le projet à partir de XCode. Maintenant didRegisterForRemoteNotificationsWithDeviceToken devrait être appelé.

6
répondu Borislav Gizdov 2015-11-06 21:30:28

j'ai fait une erreur et négligé un détail d'implémentation qui m'a conduit ici. J'ai essayé d'obtenir fantaisie et de demander à l'utilisateur pour des Notifications Push plus tard dans le processus application onboarding, donc j'ai eu mes registerForRemoteNotificationTypes , didRegisterForRemoteNotificationsWithDeviceToken et didFailToRegisterForRemoteNotificationsWithError tous dans un UIView personnalisé.

: les didRegisterForRemoteNotificationsWithDeviceToken et didFailToRegisterForRemoteNotificationsWithError doivent figurer dans le UIApplicationDelegate ( YourAppDelegate.m ) à déclencher.

semble évident maintenant.

3
répondu GraehamF 2014-07-18 00:03:06

assurez-vous que votre connexion internet est activée. Cela m'a pris des heures pour obtenir des notifications de travail en raison de la connexion internet.

3
répondu Rendel 2014-10-15 12:43:56

si vous avez ajouté push à une application existante, assurez-vous de générer à nouveau vos profils de provisionnement. Si vous ne le faites pas, le profil ne sera pas au courant de votre activation de push sur L'application.

3
répondu Michael Peterson 2015-01-17 16:55:04

Essayer ce travail pour moi ,

Premier Pas

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

dans la méthode ci-dessus ajouter le code ci-dessous

 UIApplication *application = [UIApplication sharedApplication];
 if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge
                                                                                     |UIUserNotificationTypeSound
                                                                                     |UIUserNotificationTypeAlert) categories:nil];
[application registerUserNotificationSettings:settings];
 } 
 else {
      UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:myTypes];
     }

Deuxième Étape

Ajouter ci-dessous la fonction de code

 #ifdef __IPHONE_8_0
   - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
   {
  //register to receive notifications
  [application registerForRemoteNotifications];
  }

 - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
  {
    //handle the actions
    if ([identifier isEqualToString:@"declineAction"]){
    }
   else if ([identifier isEqualToString:@"answerAction"]){
   }
}
 #endif

vous obtiendrez le jeton de périphérique dans la fonction ci-dessous

 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 

pour plus de détails, veuillez vous référer à .

Espère que c'est de l'aide pour certains .

3
répondu Jaywant Khedkar 2017-05-23 12:26:03
-​(BOOL)application:(UIApplication​*)application​ didFinishLaunchingWithOptions:(NSDictionary​*)launchOptions​{​​​​ ​​​​ ​​​​//​Override​point​for​customization​after​application​launch.
​​​​//​Add​the​view​controller’s​view​to​the​window​and​display. ​​​​[window​addSubview:viewController.view]; ​​​​[window​makeKeyAndVisible];
NSLog(@”Registering for push notifications...”);
 [[UIApplication sharedApplication]
registerForRemoteNotificationTypes: (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound)]; 
​​​​return​YES;
}


- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
}
NSString *str = [NSString stringWithFormat:@”Device Token=%@”,deviceToken];
NSLog(@”%@”, str);


- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSString *str = [NSString stringWithFormat: @”Error: %@”, err]; NSLog(@”%@”, str);
}


- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
}
for (id key in userInfo) { NSLog(@”key: %@, value: %@”, key, [userInfo objectForKey:key]);
}
2
répondu Sachin Kumaram 2011-11-28 09:42:37

exigence minimale pour obtenir un jeton de dispositif:



pas besoin de configurer l'app id, le provisioning ou le Certificat etc donc pas de jeu de signature de code pour obtenir la méthode de délégué didRegisterForRemoteNotificationswithdevicetoken appelé.



je viens de créer un nouveau projet iOS dans Xcode 7 pour la vue unique avec les paramètres par défaut et a donné un paquet aléatoire id COMME com.mycompany.pushtest qui n'est pas configuré dans apple dev portal.



avec le code suivant, je reçois mon token de périphérique dans didregisterpour les notifications remoten avec la méthode devicetoken sur mon iPad avec internet accès au WIFI. Mon appareil est attaché et je lance l'application directement à partir de xcode et je regarde les valeurs dans la console de xcode.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions   

{  
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)])  
    {  
        UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound);  
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];  
        [application registerUserNotificationSettings:settings];  
        [application registerForRemoteNotifications];  
    }  
    else  
    {  
        // Register for Push Notifications, if running iOS version < 8  
        [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];  
    }  
    return YES;  
}  


- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"Error: %@", error.description);
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken: %@", deviceToken);
}

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
    NSLog(@"NotificationSettings: %@", notificationSettings);
}
2
répondu zeeawan 2015-11-10 03:18:11

j'ai un point à ce sujet.Récemment, j'ai moi aussi fait face à ce problème.J'ai tout fait selon la documentation, mais la méthode du délégué n'appelait pas.Enfin j'ai vu un post disant que le problème avec le réseau.Puis j'ai changé de réseau et ça marche très bien.Alors prenez soin de réseau aussi parce que peu de réseaux peuvent bloquer les APN.

1
répondu SURESH SANKE 2015-11-06 18:43:54

si l'arrêt push message de l'application,

applidregisterforremotenotificationswithdevicetoken will never be called

1
répondu libai 2016-06-29 09:30:25

cela m'est arrivé, parce que j'ai réinitialisé et supprimé toutes les données sur le téléphone (je voulais un téléphone dev à utiliser). Cela a empêché APN de se connecter après avoir mis en place le téléphone à nouveau.

j'ai essayé toutes sortes de choses, mais la seule chose qui l'a réparé était de configurer le téléphone pour travailler avec un transporteur sous une nouvelle carte SIM.

ce lien offre plus de conseils sur ce qui aurait pu se passer: https://developer.apple.com/library/ios/technotes/tn2265/_index.html

il est dit que L'APN essaie de se connecter préférentiellement via carrier / towers par opposition au wifi. Peut-être que le problème était aussi quelque chose qui se passait avec le blocage du port 5223 du routeur sur le réseau wifi, mais je doute qu'il fonctionne bien la veille de la réinitialisation globale.

0
répondu Phyllis Sutherland 2013-10-17 16:41:05

pour moi ce qui a résolu il allait aux paramètres de construction et sous la section de signature de code, sélectionner manuellement l'identité de signature de code et le profil de provision. Apparemment, le réglage automatique ne détectait pas le bon et donc l'application n'était pas correctement autorisée.

0
répondu Diego Rebosio 2016-04-28 03:50:06

aussi, n'oubliez pas de vérifier l'état du système chez Apple https://developer.apple.com/system-status / .

j'avais essayé toutes les solutions affichées ci-dessus mais à la fin la faute était parce que le service APNS était en baisse! Le lendemain, tout fonctionnait à nouveau comme prévu.

vous avez aussi une erreur typographique dans votre méthode de rappel:

- (void)application:(UIApplication *)appdidRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

comme Rupesh a souligné le nom de la méthode correcte est:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

c'est probablement pour ça que vous n'avez jamais reçu le jeton dans votre cas!

0
répondu Brett 2016-08-18 08:44:03

vous devez appeler registrerfornotifications method de did finishlaunchingwithoptions.

 func registerForNotifications(){

        if #available(iOS 10.0, *) {

            let center = UNUserNotificationCenter.current()
            center.delegate = self
            center.requestAuthorization(options:[.alert,.sound,.badge]) { (granted, error) in
                if granted{
                    UIApplication.shared.registerForRemoteNotifications()
                }else{

                    print("Notification permission denied.")

                }
            }

        } else {

            // For iOS 9 and Below
            let type: UIUserNotificationType = [.alert,.sound,.badge];
            let setting = UIUserNotificationSettings(types: type, categories: nil);
            UIApplication.shared.registerUserNotificationSettings(setting);
            UIApplication.shared.registerForRemoteNotifications()
        }
    }

 func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

        let token = String(format: "%@", deviceToken as CVarArg).trimmingCharacters(in: CharacterSet(charactersIn: "<>")).replacingOccurrences(of: " ", with: "")
        print(token)

    }


extension AppDelegate : UNUserNotificationCenterDelegate{

    @available(iOS 10.0, *)

    func userNotificationCenter(_ center: UNUserNotificationCenter,  willPresent notification: UNNotification, withCompletionHandler   completionHandler: @escaping (_ options:   UNNotificationPresentationOptions) -> Void) {
        print("Handle push from foreground”)

        let info = ((notification.request.content.userInfo as NSDictionary).value(forKey: "aps") as! NSDictionary)
        if let type = info.value(forKey: "type") as? Int{
            if type == 0 {  

         // notification received ,Handle your notification here

            }
        }
    }

    @available(iOS 10.0, *)

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

        print("Handle push from background or closed")

        let info = ((response.notification.request.content.userInfo as NSDictionary).value(forKey: "aps") as! NSDictionary)
        if let type = info.value(forKey: "type") as? Int{
            if type == 0 {   

            // notification received ,Handle your notification here
            }
        }
    }
}
0
répondu Ved Rauniyar 2017-03-30 18:02:08

j'ai eu un autre problème où mes appels de notification de poussée étaient détournés par les bibliothèques de tiers, que j'avais inclus, à savoir Firebase. Ces bibliothèques swizzle méthodes de Callback notification push pour obtenir les callbacks.

J'espère que ça aidera quelqu'un.

0
répondu jarora 2017-05-25 04:44:45