ios - 推送通知在装有 iOS 8.0 的 iPhone 6 中不起作用

标签 ios ios8 push-notification apple-push-notifications

我正在使用推送通知开发 iPhone 应用程序。我遇到通知问题,我在 iOS 6、7、8.1、8.1.2 中成功获取设备 token ID,但对于我的 iPhone 6 和 iOS 8.0。

所以在我的推送通知方法“didRegisterForRemoteNotificationsWithDeviceToken”中, token ID 返回 nil。所以,我无法获得推送通知。我不知道是什么问题。

这是我的代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{        
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
    }
    else{
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
         (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    }
    .
    .
    .
    .

    [self.window makeKeyAndVisible];

    return YES;
}

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    NSString *newToken = [deviceToken description];
    newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    newToken = [newToken stringByReplacingOccurrencesOfString:@" " withString:@""];

    [GlobalVariables proTrain].deviceToken = newToken;

    NSLog(@"device token id = %@", newToken);
}

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
    [application registerForRemoteNotifications];
}

最佳答案

#define IS_OS_8_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

if (IS_OS_8_OR_LATER)
{
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
}

关于ios - 推送通知在装有 iOS 8.0 的 iPhone 6 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27687888/

相关文章:

swift - 如何更改导航栏标题位置?

ios - Xcode 6 iOS 8 UIImage imageNamed from bundle 问题

android - 当应用程序处于后台状态时,FCM 多个推送通知无法正常工作

ios - Swift 2.0 OneSignal .pushNotification

ios - 大全景到平移视频

ios - 使用 2 个 NSTimers 时出现问题

Webview 中的 iOS 键盘样式?

ios - 从呈现的 View Controller 返回时保留 UICollectionView contentOffset

ios - 如何从键盘将动画 gif 复制到剪贴板?(iOS 8)

android - FCM 推送通知问题 : When I tap on notification and every time i got getExtras() from intent is null.(应用程序在后台)