APNS在您的应用程序中的逐步集成,您可以在here中获取步骤
iOS9 Apple 表示,每次安装您的应用时,设备令牌可能会发生变化。所以最好的方法是在每次启动时重新注册设备令牌。
第一步
注册推送通知有两个步骤。首先,您必须获得用户的许可才能显示任何类型的通知,之后您才能注册远程通知。如果一切顺利,系统会为您提供一个设备令牌,您可以将其视为该设备的“地址”。
此方法创建 UIUserNotificationSettings 的实例并将其传递给 registerUserNotificationSettings(_:)。
UIUserNotificationSettings 存储应用程序将使用的通知类型的设置。对于 UIUserNotificationTypes,您可以使用以下任意组合:
.Badge 允许应用在应用图标的角上显示一个数字。
.Sound 允许应用播放声音。
.Alert 允许应用显示文本。
您当前传递 nil 的 UIUserNotificationCategorys 集允许您指定应用可以处理的不同类别的通知。当您想要实现可操作的通知时,这变得很有必要,您将在以后使用它
- (void)applicationDidFinishLaunching:(UIApplication *)app {
// other setup tasks here....
// Register the supported interaction types.
UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings =
[UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
// Register for remote notifications.
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
构建并运行。当应用启动时,您应该会收到一条提示,要求您允许向您发送通知:
点击确定,然后噗!该应用现在可以显示通知。
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
if (notificationSettings.types != UIUserNotificationTypeNone) {
//register to receive notifications
[application registerForRemoteNotifications];
}
}
这里,您首先检查用户是否授予您任何通知权限;如果有,您直接调用 registerForRemoteNotifications()。
再次调用 UIApplicationDelegate 中的方法来通知您 registerForRemoteNotifications() 的状态。
// Handle remote notification registration.
- (void)application:(UIApplication *)app
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
const void *devTokenBytes = [devToken bytes];
self.registered = YES;
// send your Device Token to server
}
顾名思义,注册成功时系统调用application(:didRegisterForRemoteNotificationsWithDeviceToken:),否则调用application(:didFailToRegisterForRemoteNotificationsWithError:)。
- (void)application:(UIApplication *)app
didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSLog(@"Error in registration. Error: %@", err);
}
斯威夫特
let defaults = NSUserDefaults.standardUserDefaults()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
// PUSH NOTIFICATION
let deviceToken = defaults.objectForKey(UserDefaultsContracts.KEY_DEVICE_TOKEN) as String?
if (deviceToken == nil) {
print("There is no deviceToken saved yet.")
var types: UIUserNotificationType = UIUserNotificationType.Badge |
UIUserNotificationType.Alert |
UIUserNotificationType.Sound
var settings: UIUserNotificationSettings = UIUserNotificationSettings( forTypes: types, categories: nil )
application.registerUserNotificationSettings( settings )
application.registerForRemoteNotifications()
}
return true
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData!) {
print("Got token data! (deviceToken)")
var characterSet: NSCharacterSet = NSCharacterSet( charactersInString: "<>" )
var deviceTokenString: String = ( deviceToken.description as NSString )
.stringByTrimmingCharactersInSet( characterSet )
.stringByReplacingOccurrencesOfString( " ", withString: "" ) as String
print( deviceTokenString )
defaults.setObject(deviceTokenString, forKey: UserDefaultsContracts.KEY_DEVICE_TOKEN)
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError!) {
print("Couldn’t register: (error)")
}
}
更多信息请在Apple Documents