iOS应用程序如何知道它必须注册APNS?

时间:2016-03-16 06:31:48

标签: ios apple-push-notifications

我知道当您的应用成功注册推送通知并获得设备令牌时,didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken会被调用。

根据Apple文档设备令牌由您的客户端应用程序提供给您,客户端应用程序在使用远程通知服务注册后接收令牌

我想知道应用程序如何知道是否需要注册本身用于远程通知服务。

2 个答案:

答案 0 :(得分:1)

为了实现推送通知,您需要在应用程序didFinishLaunchingWithOptions中编写以下内容。

    UIUserNotificationType type=(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
    UIUserNotificationSettings *settings=[UIUserNotificationSettings settingsForTypes:type categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    [[UIApplication sharedApplication]registerForRemoteNotifications];

除此之外,您还需要创建apns开发证书和相应的配置文件,其中启用了推送通知服务。当您在具有这些配置文件的设备中构建应用程序时,设备将了解此应用程序启用了推送通知服务,并且设备将提供警报您是否希望获得推送通知? 如果用户按下确定,则只会触发didRegisterForRemoteNotificationsWithDeviceToken,开发人员可以获得deviceToken。

答案 1 :(得分:0)

我自己查了一下 - "初始点" 在开发者的配置文件中设置了推送通知触发指示注册您的应用以获取远程通知的门户网站。