iOS 应用未注册推送通知

iOS App not registering for push notifications

我正在尝试在我的 iOS 应用程序中注册推送通知。但它既不调用 didRegisterForRemoteNotificationsWithDeviceToken 也不调用 didFailToRegisterForRemoteNotificationsWithError 回调方法。我已撤销并重新生成应用程序的配置文件。

我正在使用 iOS8,并且在 Info.plist

中启用了以下背景模式
  1. 应用注册位置更新
  2. 应用下载内容以响应推送通知
  3. 应用从网络下载内容

密码是:

-(void) application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"Failed to register for push");
}

-(void) application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSLog(@"did succeed in register for push");

    // Get the device token string
    const char* data = [deviceToken bytes];
    NSMutableString* token = [NSMutableString string];

    for (int i = 0; i < [deviceToken length]; i++) {
        [token appendFormat:@"%02.2hhX", data[i]];
    }
    [[NSUserDefaults standardUserDefaults] setObject:token forKey:@"DeviceToken"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

    [self respondToEventNotification:userInfo];

}

-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

    [self respondToEventNotification:userInfo];

}

您是否使用 didFinishLaunchingWithOptions 方法为 iOS8 注册了您的应用程序,例如

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        [[UIApplication sharedApplication] registerForRemoteNotifications];
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge
                                                                                         |UIRemoteNotificationTypeSound
                                                                                         |UIRemoteNotificationTypeAlert) categories:nil];

        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    }
    else
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
         (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

}

您必须考虑 iOS8 中推送通知的注册过程。

希望对您有所帮助。