Je crée une application dans laquelle je souhaite implémenter le service de notification push Apple. 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 cause le problème. Quelqu'un peut-il m'aider?
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(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 *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSString *str = [NSString stringWithFormat: @"Error: %@", err];
NSLog(@"Error:%@",str);
}
objective-c
apple-push-notifications
Muzammil
la source
la source
Réponses:
J'ai eu le même problème: appeler
registerForRemoteNotificationTypes:
invoqué niapplication:didRegisterForRemoteNotificationsWithDeviceToken:
niapplication:didFailToRegisterForRemoteNotificationsWithError:
J'ai finalement résolu ce problème à l'aide de la note technique d'Apple TN2265 .
C'est ce que j'ai fait:
Tout d'abord, j'ai revérifié que je m'inscrivais bien aux notifications push , y compris la vérification de mon profil d'approvisionnement pour la clé "aps-environnement" et la signature du fichier .app lui-même. J'ai tout configuré correctement.
J'ai ensuite dû déboguer les messages d'état de notification push dans la console (vous devez installer le profil de provisionnement PersistentConnectionLogging.mobileconfig sur votre appareil et le redémarrer. Voir TN2265 sous «Observation des messages d'état push»). J'ai remarqué que le processus apns démarre une minuterie et calcule une date d'incendie minimale, ce qui m'a fait suspecter que le message de confirmation d'enregistrement Push-Notification, qui est normalement présenté à ce stade, est supprimé par APNS, comme indiqué dans TN2265:
J'ai donc supprimé l'application de l'appareil, puis modifié manuellement la date de l'iPhone 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 rappels comme prévu.Cela a résolu le problème pour moi. J'espère que cela aide.
la source
Dans iOS 8, certaines méthodes sont obsolètes. Suivez les étapes ci-dessous pour la compatibilité iOS 8
1. Enregistrer la notification
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"]){ } }
Remarque: ci-dessus, deux nouvelles méthodes sont requises dans iOS 8 en plus de
didRegisterForRemoteNotificationsWithDeviceToken
etdidReceiveRemoteNotification
.. Sinon, la méthode de délégation ne sera pas appelée.Voir: Notification à distance iOS 8
la source
[application registerForRemoteNotifications];
Dans iOS 8 , en plus de demander l'accès aux notifications push différemment, vous devez également vous enregistrer 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)]; }
Gérer l'appareil 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 }
la source
application:didRegisterForRemoteNotificationsWithDeviceToken:
Gardez à 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
elle ne sera pas appelée.la source
Assurez-vous d'appeler votre code (mise à jour selon les types de notification pris en charge)
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];
et le profil de provisionnement est activé APNS. Vous devrez peut-être retélécharger le profil d'approvisionnement après avoir activé APNS. Si vous rencontrez des problèmes et que vous obtenez des erreurs, vous devriez peut-être créer un Entitlements.plist et ajouter la clé "aps-environment" avec la valeur "development" ou "production" selon le type de build (normalement, cette paire clé-valeur est contenus dans le profil de provisionnement, mais parfois Xcode les gâche).
la source
Si les profils d'approvisionnement sont utilisés auparavant pour activer et configurer le service de notification push Apple, vous devrez retélécharger les profils d'approvisionnement.
Supprimez les profils d'approvisionnement de Xcode Organizer et de l'iPhone / iPad. Allez à
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é.la source
J'ai fait une erreur et négligé un détail de mise en œuvre qui m'amène ici. J'ai essayé d'avoir de la fantaisie et de demander à l'utilisateur des notifications push plus tard dans le processus d'intégration de l'application, donc j'avais mon
registerForRemoteNotificationTypes
,didRegisterForRemoteNotificationsWithDeviceToken
et ledidFailToRegisterForRemoteNotificationsWithError
tout dans un UIView personnalisé.CORRECTIF: les
didRegisterForRemoteNotificationsWithDeviceToken
etdidFailToRegisterForRemoteNotificationsWithError
doivent être dans leUIApplicationDelegate
(YourAppDelegate.m
) pour être déclenchés.semble évident maintenant, heh.
la source
AppDelegate
. Je ne peux pas croire que j'ai gaspillé mes dernières heures sur cette réponse simpleAssurez-vous que votre connexion Internet est activée. Cela m'a pris des heures pour recevoir des notifications de travail en raison de la connexion Internet.
la source
Si vous avez ajouté push à un ID d'application existant, assurez-vous de régénérer vos profils d'approvisionnement. Si vous ne le faites pas, le profil ne saura pas que vous avez activé le push sur l'ID d'application.
la source
Essayez ça, ça marche pour moi,
Premier pas
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Dans la méthode ci-dessus, ajoutez 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 un jeton d'appareil dans la fonction ci-dessous
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
pour une réponse détaillée, veuillez consulter ceci
J'espère que c'est de l'aide pour quelqu'un.
la source
-(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions{ //Overridepointforcustomizationafterapplicationlaunch. //Addtheviewcontroller’sviewtothewindowanddisplay. [windowaddSubview:viewController.view]; [windowmakeKeyAndVisible]; NSLog(@”Registering for push notifications...”); [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)]; returnYES; } - (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]); }
la source
Exigence minimale pour obtenir un jeton de périphérique:
pas besoin de configurer l'ID d'application, l'approvisionnement ou le certificat, etc., donc aucun jeu de signature de code pour obtenir la méthode déléguée didRegisterForRemoteNotificationsWithDeviceToken appelée.
Je viens de créer un nouveau projet iOS dans Xcode 7 pour une vue unique avec les paramètres par défaut et j'ai donné un identifiant de bundle aléatoire comme com.mycompany.pushtest qui n'est pas configuré dans le portail de développement Apple.
Avec le code suivant, je reçois le jeton de mon appareil dans la méthode didRegisterForRemoteNotificationsWithDeviceToken sur mon iPad avec accès Internet au WIFI. Mon appareil est connecté et je lance simplement l'application directement à partir de xcode et affiche 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); }
la source
J'ai un point à ce sujet.Récemment, je suis aussi confronté à ce problème.J'ai tout fait selon la documentation mais la méthode de délégation n'appelait pas.Enfin, j'ai vu un message disant ce problème avec le réseau.Ensuite, j'ai changé de réseau et cela fonctionne bien. Faites donc attention au réseau également car peu de réseaux peuvent bloquer l'APNS.
la source
Après avoir perdu les 3h les plus ennuyeuses, voici les étapes pour résoudre le problème:
Supprimer l'appli
Réinitialiser l'appareil
Courez à nouveau
Ça a juste marché
la source
Cela m'est arrivé, car j'ai réinitialisé et supprimé toutes les données du téléphone (je voulais qu'un téléphone de développement utilise). Cela a empêché APN de se connecter après avoir configuré à nouveau le téléphone.
J'ai essayé toutes sortes de choses, mais la seule chose qui a résolu le problème a été de configurer le téléphone pour qu'il fonctionne avec un opérateur sous une nouvelle carte SIM.
Ce lien offre plus d'indices sur ce qui aurait pu se passer: https://developer.apple.com/library/ios/technotes/tn2265/_index.html
Il dit qu'APN essaie de se connecter de préférence via un opérateur / des tours par opposition au wifi. Peut-être que le problème était également lié au blocage du port 5223 du routeur sur le réseau wifi, mais j'en doute car cela fonctionnait bien la veille avant la réinitialisation globale.
la source
Pour moi, ce qui a résolu cela a été d'aller dans les paramètres de construction et sous la section de signature de code, en sélectionnant manuellement l'identité de signature de code et le profil d'approvisionnement. Apparemment, le réglage automatique ne sélectionnait pas le bon et donc l'application n'était pas correctement autorisée.
la source
si arrêter le message push de l'application,
appdidRegisterForRemoteNotificationsWithDeviceToken ne sera jamais appelé
la source
N'oubliez pas non plus de vérifier l'état du système sur Apple https://developer.apple.com/system-status/ .
J'avais essayé toutes les solutions affichées ci-dessus mais à la fin la faute était que le service APNS était en panne! Le lendemain, tout fonctionnait à nouveau comme prévu.
En outre, vous avez une faute de frappe dans votre méthode de rappel:
- (void)application:(UIApplication *)appdidRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
Comme Rupesh l'a souligné, le nom correct de la méthode est:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
C'est probablement pourquoi vous n'avez jamais reçu le jeton dans votre cas!
la source
Vous devez appeler la méthode registerForNotifications à partir de didFinishLaunchingWithOptions.
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 } } } }
la source
J'ai eu un problème différent dans lequel mes rappels de notification push étaient détournés par les bibliothèques tierces, que j'avais incluses, à savoir Firebase. Ces bibliothèques utilisent des méthodes de rappel de notification push pour obtenir les rappels.
J'espère que cela aide quelqu'un.
la source