ios didRegisterForRemoteNotificationsWithDeviceToken 未执行 - 推送通知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/17697141/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
didRegisterForRemoteNotificationsWithDeviceToken not executed - Push Notification
提问by Ashish Pisey
I am using Parse.comto send push notification in my iOS app.
but when i execute following code to create PFInstallationobject,the device token field is blank. 
我正在使用Parse.com在我的 iOS 应用程序中发送推送通知。但是当我执行以下代码来创建PFInstallation对象时,设备令牌字段为空。
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)newDeviceToken {
    // Store the deviceToken in the current installation and save it to Parse.
    NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken");
    PFInstallation *currentInstallation = [PFInstallation currentInstallation];
    [currentInstallation setDeviceTokenFromData:newDeviceToken];
    [currentInstallation saveInBackground];
}
didFinishLaunchingWithOptionsmethod
didFinishLaunchingWithOptions方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    [Parse setApplicationId:@"xhjhkk869698lhlljk554hl55khlkhl4ff99065" clientKey:@"spg1t6jad1ShK2lh5456khh6j7j4nmn1YD6J6rl8vt3"];
    [PFAnalytics trackAppOpenedWithLaunchOptions:launchOptions];
    [FBProfilePictureView class];
// Register for push notifications
[application registerForRemoteNotificationTypes:
 UIRemoteNotificationTypeBadge |
 UIRemoteNotificationTypeAlert |
 UIRemoteNotificationTypeSound];
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
return YES;
}
}
I noticed that didRegisterForRemoteNotificationsWithDeviceTokenis never executed. i cross checked my certificates and provisioning profile and i tested them using method specified here(see under Making a PEM file). Certificates and connection work fine. I also checked if my wifi is blocking the push notification,there is no issue with it. 
我注意到didRegisterForRemoteNotificationsWithDeviceToken从未执行过。我交叉检查了我的证书和配置文件,并使用此处指定的方法对其进行了测试(参见制作 PEM 文件下)。证书和连接工作正常。我还检查了我的 wifi 是否阻止了推送通知,它没有问题。
so can anyone Please suggest what am i doing wrong here?
所以任何人都可以请建议我在这里做错了什么?
回答by Leta0n
Did you implement? - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)errorMaybe you have some error and in this method you can get description of it.
你实施了吗?- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error也许你有一些错误,在这个方法中你可以得到它的描述。
回答by Ajay
This is what I do in my applications to register for push notifications:
这就是我在我的应用程序中注册推送通知时所做的:
In AppDelegate.m
在 AppDelegate.m 中
#pragma mark PUSH NOTIFICATION
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)token
{
    NSUInteger rntypes;
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    rntypes = [[[UIApplication sharedApplication] currentUserNotificationSettings] types];
} else {
    rntypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
}
    // Set the defaults to disabled unless we find otherwise...
    NSString *pushBadge = @"disabled";
    NSString *pushAlert = @"disabled";
    NSString *pushSound = @"disabled";
    if(rntypes == UIRemoteNotificationTypeBadge)
    {
        pushBadge = @"enabled";
    }
    else if(rntypes == UIRemoteNotificationTypeAlert)
    {
        pushAlert = @"enabled";
    }
    else if(rntypes == UIRemoteNotificationTypeSound)
    {
        pushSound = @"enabled";
    }
    else if(rntypes == ( UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert))
    {
        pushBadge = @"enabled";
        pushAlert = @"enabled";
    }
    else if(rntypes == ( UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound))
    {
        pushBadge = @"enabled";
        pushSound = @"enabled";
    }
    else if(rntypes == ( UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound))
    {
        pushAlert = @"enabled";
        pushSound = @"enabled";
    }
    else if(rntypes == ( UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound))
    {
        pushBadge = @"enabled";
        pushAlert = @"enabled";
        pushSound = @"enabled";
    }
    NSLog(@"PUSH SOUND %@",pushBadge);
    NSLog(@"PUSH ALERT %@",pushAlert);
    NSLog(@"PUSH SOUND %@",pushSound);
    NSString *deviceToken = [[[[token description]
                               stringByReplacingOccurrencesOfString:@"<"withString:@""]
                              stringByReplacingOccurrencesOfString:@">" withString:@""]
                             stringByReplacingOccurrencesOfString: @" " withString: @""];
    NSLog(@"%d bytes", [token length]);
    NSLog(@"device token = %@", deviceToken);
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
    NSString *str1 = [NSString stringWithFormat: @"Error: %@", err];
    NSLog(@"%@",str1);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    for (id key in userInfo)
    {
        NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
    }
    NSLog(@"remote notification: %@",[userInfo description]);
    NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
    NSString *alert = [apsInfo objectForKey:@"alert"];
    NSLog(@"Received Push Alert: %@", alert);
    NSString *sound = [apsInfo objectForKey:@"sound"];
    NSLog(@"Received Push Sound: %@", sound);
    NSString *badge = [apsInfo objectForKey:@"badge"];
    NSLog(@"Received Push Badge: %@", badge);
    application.applicationIconBadgeNumber = [[apsInfo objectForKey:@"badge"] integerValue];
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Notification" message:alert delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
    [alertView release];
}
And put this into the didFinishLaunchingWithOptions:
并将其放入 didFinishLaunchingWithOptions 中:
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    {
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
    else
    {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
         (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
    }

