我遵循了this thread,但仍然没有调用方法didRegisterForRemoteNotificationsWithDeviceToken:

文件上说:

调用

didRegisterUser看起来不错,但不是did register notif.

以下是我在AppDelegate中的代码(应用程序版本为8.1):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //register notif
    UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
                                                    UIUserNotificationTypeBadge |
                                                    UIUserNotificationTypeSound);
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];
    [application registerUserNotificationSettings:settings];


    return YES;
}

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

-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"error here : %@", error);//not called
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    /*
    // Store the deviceToken in the current installation and save it to Parse.
    PFInstallation *currentInstallation = [PFInstallation currentInstallation];
    [currentInstallation setDeviceTokenFromData:deviceToken];
    currentInstallation.channels = @[ @"global" ];
    [currentInstallation saveInBackground];
     */
    NSLog(@"did register notif");//not called
}

I also have background mode -> remote notification in the info.plist.

推荐答案

你的代码似乎是正确的.作为一个小小的改进,您可以这样编写您的didRegisterUserNotificationSettings方法:

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
    if (notificationSettings.types != UIUserNotificationTypeNone) {
        NSLog(@"didRegisterUser");
        [application registerForRemoteNotifications];
    }
}

可能存在导致APN注册失败的配置问题.

  1. 确保您的资源调配配置文件包含aps环境条目

  2. 确保在配置文件中设置了唯一的应用程序标识符(不带任何"*"的字符串).您还应该在信息中使用这个确切的标识符作为"包标识符".普利斯特

  3. 可能您在初次安装后拒绝了推送功能——在这种情况下,您将再也看不到应用内推送alert ,必须再次启用应用内推送设置.

  4. 试试其他设备.

Objective-c相关问答推荐

如何追踪 SIGABRT 的原因

在 Objective-C 类中混合 C 函数

如何获取 iPhone 的当前方向?

额外的 75 秒从何而来?

如何在 iPhone 上显示来自 API 的 HTML 文本?

Grand Central Dispatch (GCD) 与 performSelector - 需要更好的解释

如何转储存储在 Objective-C 对象(NSArray 或 NSDictionary)中的数据

无法加载从笔尖引用的图像

OSStatus 错误代码 -34018

arc4random 和 arc4random_uniform 有什么区别?

为什么不鼓励使用伞式框架?

Cocoa 与 Cocoa Touch - 有什么区别?

UIBezierPath 减go 路径

Xcode 在您的 keys 串中找不到此配置文件的有效私有证书/有效密钥对

如何从设置屏幕打开位置服务屏幕?

嵌入自定义容器视图控制器时,内容位于导航栏下方.

为什么我们不能在当前队列上使用 dispatch_sync?

为什么 Select Emacs/Vim/Textmate? Xcode 还不够好吗?

字符串常量和字符串文字有什么区别?

Xcode 警告未使用属性访问结果 - getter 不应用于副作用