iOS远程推送
APNS远程推送的流程:
1、app 注册到 APNS。
2、APNS 下发 devicetoken 给 app。
3、app 将 device token 发送给 app Server。
4、app Serve 将[消息+device token]发送给 APNS。
5、APNS 根据 device token 推送消息给iOS设备上的app。
实现上述步骤需要一个前提:应用程序的推送证书(开发环境&生产环境两类推送证书)和描述文件(Provisioning Profile)配置完备。苹果会在推送前根据描述文件和 App ID 等信息对应用程序的合法性进行验证。`
Objective-C相关代码
//推送相关逻辑
@interface AppDelegate (AppPush)<UNUserNotificationCenterDelegate>
- (void)registerRemoteNotifications:(UIApplication *)application;
@end
@implementation AppDelegate (AppPush)
- (void)registerRemoteNotifications:(UIApplication *)application {
if (@available(iOS 10, *)) {
UNUserNotificationCenter *notificationCenter = [UNUserNotificationCenter currentNotificationCenter];
notificationCenter.delegate = self;
// 申请权限
[notificationCenter requestAuthorizationWithOptions:UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) {
NSLog(@"申请权限granted = %d", granted);
if (!error && granted) {
NSLog(@"远程通知注册成功");
} else {
NSLog(@"远程通知注册失败error-%@", error);
}
}];