为什么应用程序没有在iOS 8中注册推送通知?

use*_*840 8 iphone objective-c apple-push-notifications xcode6 ios8

我将我的Xcode升级到Xcode 6.0.1,现在iOS 8设备没有进行远程通知注册.它适用于iOS 7设备.

我在app delegate中添加了代码,如下所述:

    //-- Set Notification
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge
     |UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert) categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

    NSLog(@"current notifications : %@", [[UIApplication sharedApplication] currentUserNotificationSettings]);
}
else
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
Run Code Online (Sandbox Code Playgroud)

即使是当前的通知也存在,并且它不是零.

然而,以下方法未被调用:

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
Run Code Online (Sandbox Code Playgroud)

下面的屏幕截图说明我在后台模式中启用了某些选项:

在此输入图像描述

并且通知在我的应用的设备设置中设置.

Leo*_*ica 15

你需要打电话

[[UIApplication sharedApplication] registerForRemoteNotifications];
Run Code Online (Sandbox Code Playgroud)

注册用户通知设置后,在iOS8代码路径中.

  • 非常感谢.现在,该应用程序已成功注册推送通知. (2认同)

iGW*_*iGW 13

下面的代码适用于iOS 8.0 Xcode 6.0或更高版本以及以下版本.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //This code will work in iOS 8.0 xcode 6.0 or later
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    {
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
    else
    {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeNewsstandContentAvailability| UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    }

    return YES;
}
Run Code Online (Sandbox Code Playgroud)


Pal*_*ade 5

检查以下步骤希望它对您有所帮助

步骤1在didFinishLaunchingWithOptions中

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
    //ios8 ++
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)])
    {
        UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
    }
}
else
{
    // ios7
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerForRemoteNotificationTypes:)])
    {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
    }
}
Run Code Online (Sandbox Code Playgroud)

第2步

 -(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings // available in iOS8
{
[application registerForRemoteNotifications];
}
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
   NSString * token = [NSString stringWithFormat:@"%@", deviceToken];
//Format token as you need:
token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
token = [token stringByReplacingOccurrencesOfString:@">" withString:@""];
token = [token stringByReplacingOccurrencesOfString:@"<" withString:@""];
NSLog(@"%@",token);
}
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// Handle your remote RemoteNotification 
}

-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(@"Error:%@",error);
}
Run Code Online (Sandbox Code Playgroud)