iOS 推送

1.注册apns

UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];

[[UIApplication sharedApplication] registerUserNotificationSettings:setting];

2.在允许推送之后向苹果获取deviceToken

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings

{

[application registerForRemoteNotifications];

}

3.获取deviceToken回调方法 这里可以把获取的token发送到自己的服务器

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

{

NSString *token = [NSString stringWithFormat:@"%@",deviceToken];

token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];

token = [token stringByReplacingOccurrencesOfString:@"<" withString:@""];

token = [token stringByReplacingOccurrencesOfString:@">" withString:@""];

NSLog(@"%@",token);

}

4.推送数据格式 可在custom添加推送消息的业务逻辑数据

{"aps":{"alert":"test","badge":1,"sound":"default","custom":{"t":"t","p":"second"}}}

5.为了便于本地测试可以去GitHub下载一个工具NWPusher,可以模仿服务器推送消息

https://github.com/noodlewerk/NWPusher

你可能感兴趣的:(iOS 推送)