IOS 8注册远程通知仅半工作

时间:2014-11-11 20:01:36

标签: objective-c ios8 xcode6

修改

我会把它放在顶部,因为这似乎可能是问题。注册用户设置后,我NSLog:

NSLog(@"current notifications : %@", [application currentUserNotificationSettings]);

我收到了回复:

current notifications : <UIUserNotificationSettings: 0x165844a0; types: (none);>

但是正如您所看到的,我正在清楚地注册设置以使用徽章,声音和警报。

  

另外,我想我应该提一下我已经关闭了通知   我的设置。但是,我希望它能打电话   didFailToRegisterForRemoteNotificationsWithError在这种情况下   是我生成自己的电话号码的地方。

- 我收回了上面所说的内容。当我删除应用程序并重新安装它时,它会重新启用“允许通知”。 -


我知道这个问题在这里很多,但我似乎无法让这个工作。这是我愚蠢的代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    [self registerForNotifications:application];
}

- (void)registerForNotifications:(UIApplication *)application {
    // Let the device know we want to receive push notifications
    NSLog(@"Registering for Remote Notifications");

    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        #ifdef __IPHONE_8_0
            UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge
                                                                                                 |UIRemoteNotificationTypeSound
                                                                                                 |UIRemoteNotificationTypeAlert) categories:nil];
            [application registerUserNotificationSettings:settings];
        #endif
    } else {
        UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
        [application registerForRemoteNotificationTypes:myTypes];
    }
    NSLog(@"Done registering for Remote Notifications");
}

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    NSLog(@"Did register for remote notification settings");
    //register to receive notifications
    [application registerForRemoteNotifications];
}

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
{
    NSLog(@"There was an error?");
    //handle the actions
    if ([identifier isEqualToString:@"declineAction"]){

    }else if ([identifier isEqualToString:@"answerAction"]){

    }
}

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    NSLog(@"My token is: %@\nMy  device token is: %@\nSend Phone ID is: %@", [self loadSettings:@"phone_id"], deviceToken, [self loadSettings:@"send_phone_id"]);
}

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

对于我的NSLog语句,这是我收到的全部内容:

2014-11-11 14:57:11.066 eTicket[3144:988203] Registering for Remote Notifications
2014-11-11 14:57:11.083 eTicket[3144:988203] Done registering for Remote Notifications
2014-11-11 14:57:11.148 eTicket[3144:988203] Settings Validated?: NO
2014-11-11 14:57:11.219 eTicket[3144:988203] Did register for remote notification settings

忽略“设置已验证”行。这只是我检查他们是否已成功登录已跳过一些步骤。该部分有效,与远程通知没有任何关系。

我没有表明它是否成功注册了通知,或者是否有错误。应用程序不会崩溃或抛出错误。我很遗憾为什么没有一个底层方法被击中。

谢谢, 詹姆斯

1 个答案:

答案 0 :(得分:0)

好吧,好像我上面的一切都是正确的。但是,在注册设置后,我需要检查是否实际打开了任何设置。如果没有,我需要将它们发送给我自己的功能。所以我这样做了:

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    NSLog(@"Did register for remote notification settings");
    NSLog(@"current notifications : %@", [[UIApplication sharedApplication] currentUserNotificationSettings]);
    //register to receive notifications
    if (notificationSettings.types) {
        NSLog(@"user allowed notifications");
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }else{
        NSLog(@"user did not allow notifications");
        // show alert here
        [self doNotRegisterForRemoteNotifications];
    }
}

doNotRegisterForRemoteNotifications是我自己的功能,我可以做我需要做的事情来生成我自己的手机ID。