您好,欢迎来到二三四教育网。
搜索
您的当前位置:首页泛谈iOS 通知服务

泛谈iOS 通知服务

来源:二三四教育网

APNs

说起苹果设备的push推送服务就要提到苹果公司伟大的一个架构设计 APNs(Apple Push Notification Service)。苹果设备收到的 push消息实际上是由APNs向设备推送的,并不是直接由开发者向用户本身推送消息。有了APNs开发者需要做的只是将DeviceToken和消息内容发送到APNs服务器,其它都是APNs服务器完成的。具体过程如下:

APNs.png Provider.png

Provider 是自己应用服务器。

1.客户端向苹果公司注册push证书。

2.APNs会给客户端返回一个deviceToken 。

3.客户端将deviceToken上传给Provide。

4.Provider将deviceToken和通知内容发送给APNs。

5.APNs根据deviceToken查找指定的设备,将通知推送给设备。

6.设备收到通知后根据SSL证书判断这个push通知是发个那个应用。

客户端部署

更新到iOS 10 之后,Xcode就不需要再兼容iOS 7 ,然而iOS 8 的push系统还是跟iOS 10 不一样的。这边还是要做一下处理。

1.push证书生成

向苹果公司注册push证书。在苹果开发者中心生成push证书并下载,将相关证书文件添加到Xcode中。

2.注册通知

客户端向系统注册通知

- (void)registerPushNotification
{
    
#if !TARGET_IPHONE_SIMULATOR
    if (OSVersionIsAtLeastiOS10()) {
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
            if(granted & !error){
                [[UIApplication sharedApplication] registerForRemoteNotifications];
//                [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
//                    // todo 可以获取用户设定信息
//                }];
            }else{
                //点击不允许
                DDLogWarn(@"User Close Notification.");
            }
        }];
    }else {
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    }
//    else{ // iOS 7
//        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
//    }
#else
    DDLogWarn(@"Simulator not support push");
#endif
}

3.devToken

向系统注册通知,请求成功之后会给客户端返回devToken,因为deviceToken 随时会发生变化,每次启动App的时候都要请求一次。得到devToken 需要上传到服务器Provider。

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken
{
    DDLogInfo(@"application didRegisterForRemoteNotificationsWithDeviceToken : %@", devToken);
    // 上传devToken
    [self doPushRegDeviceTask:devToken];
}

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err
{
    //注册失败
    DDLogError(@"application didFailToRegisterForRemoteNotificationsWithError : %@", err);
}

4.处理回调函数

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
   // 处理通知消息
}

第一次安装App 有个弹框获取用户的许可是否允许接受App的通知,如果用户选择"不允许",App是不会收到push通知。iOS 10 可以获取用户对"通知"操作信息,如果用户不允许通知,你可以在恰当的时机选择通知用户是否开启通知。想重新收到通知,用户必须自己去设置->通知->App->允许通知.

信鸽

1.注册信鸽

2.初始化信鸽

初始化信鸽,这里就用到从官网获取的access_idaccess_key需要注意的是用户如果注销的,再次使用信鸽还是需要重新注册的。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    //.....
    // 信鸽初始化
    [XGPush startApp:kXGPush_Id appKey:kXGPush_Key];

    //设置设备的帐号
    // user_key : 本质上是registerDevice的一个参数
    [XGPush setAccount:user_key]

    //注销之后需要再次注册前的准备
    __weak typeof(self) weakSelf = self;
    void (^successCallback)(void) = ^(void){
        //如果变成需要注册状态
        if(![XGPush isUnRegisterStatus] ){
            [weakSelf registerPush];
        }
    };
    [XGPush initForReregister:successCallback];
    //[XGPush registerPush];  //注册Push服务,注册后才能收到推送
}

3.注册通知 & devToken

这跟上面的操作步骤是一样的,一样要获取到devToken上传给信鸽。

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSString * deviceTokenStr = [XGPush registerDevice:deviceToken];
}

Pusher

keychain.png
Pusher.png push.png

Copyright © 2019- how234.cn 版权所有 赣ICP备2023008801号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务