美文网首页
阿里推送对接流程

阿里推送对接流程

作者: 叫我魏大川 | 来源:发表于2019-05-14 10:10 被阅读0次

    1:去注册阿里推送账号,完事会给到一个.plist的配置文件,把这个文件拉入工程,别忘点击copy item。这个玩意可以用来初始化sdk。

    image

    2:推送需要配置的一些地方

    image image image

    3:下载sdk,推荐使用pods

    需要注意的地方就是,pods库的source需要使用阿里的,podfile配置文件如下:

    image

    4:代码部分

    根据业务需求有所差异。直接上代码。

    
    /*
     iOS 如何判断是 *点击推送信息进入程序* 还是 *点击app图标进入程序*  ???
     当设备接到 apns 发来的通知,应用处理通知有以下几种情况:
     
     1. 应用还没有加载
     
     1.1 这时如果点击 通知的显示按钮,
     会调用   didFinishLaunchingWithOptions 方法,
     不会调用  didReceiveRemoteNotification 方法。
     
     1.2 如果点击通知的关闭按钮,再点击应用,
     同上
     
     2. 应用加载过了
     
     2.1 应用在前台(foreground)
     这时如果收到通知,会触发 didReceiveRemoteNotification 方法。
     
     2.2 应用在后台
     此时如果收到通知,点击显示按钮,会调用 didReceiveRemoteNotification 方法。
     点击关闭再点击应用,则上面两个方法都不会被调用这时,只能在 applicationWillEnterForeground 或者 applicationDidBecomeActive, 根据发过来通知中的 badge 进行判断是否有通知,然后发请求获取数据。
     */
    
    #import "AppDelegate.h"
    #import <CloudPushSDK/CloudPushSDK.h> //引入阿里云sdk
    
    @interface AppDelegate ()
    
    @end
    
    @implementation AppDelegate
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"isReachablex"];
        
        self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        HomeViewController* hv = [[HomeViewController alloc] init];
        BaseNavigationViewController* nvi = [[BaseNavigationViewController alloc] initWithRootViewController:hv];
        self.window.rootViewController = nvi;
        [self.window makeKeyAndVisible];
        
        [self listenNetworkState];//开启网络监听
        
        
        // APNs注册,获取deviceToken并上报
        [self registerAPNS:application];
        // 初始化SDK
        [self initCloudPush];
        // 监听推送通道打开动作
        [self listenerOnChannelOpened];
        // 监听推送消息到达
        [self registerMessageReceive];
        // 点击通知将App从关闭状态启动时,将通知打开回执上报
        [CloudPushSDK sendNotificationAck:launchOptions];
        
        return YES;
    }
    
    #pragma mark - 前、后台收到远程推送.ios7+。都会调用这个方法
    
    -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler
    {
        NSLog(@"\n ====== iOS 7+ 前台后台都会调用");
        /*
         建议使用该方法,还有一个作用。根据苹果给出的文档,系统给出30s的时间对推送的消息进行处理,此后就会运行CompletionHandler 程序块。
         在处理这类推送消息(即程序被启动后接收到推送消息)的时候,通常会遇到这样的问题 :
         就是当前的推送消息是当前程序正在前台运行时接收到的,还是说是程序在后台运行,用户点击系统消息通知栏对应项进入程序时而接收到的?这个其实很简单,用下面的代码就可以解决:
         */
        
        NSLog(@"Receive one notification.: %@",userInfo);
        // 取得APNS通知内容
        NSDictionary *aps = [userInfo valueForKey:@"aps"];
        // 内容
        NSString *content = [aps valueForKey:@"alert"];
        // badge数量
        NSInteger badge = [[aps valueForKey:@"badge"] integerValue];
        // 播放声音
        NSString *sound = [aps valueForKey:@"sound"];
        // 取得Extras字段内容
        NSString *Extras = [userInfo valueForKey:@"Extras"]; //服务端中Extras字段,key是自己定义的
        NSLog(@"content = [%@], badge = [%ld], sound = [%@], Extras = [%@]", content, (long)badge, sound, Extras);
        // iOS badge 清0
        application.applicationIconBadgeNumber = 0;
        // 通知打开回执上报
        [CloudPushSDK sendNotificationAck:userInfo];
        
        // 做相应的判断是前台还是后台,做相应的处理
        if (application.applicationState == UIApplicationStateActive) {
            // 程序当前正处于前台
            
        } else if (application.applicationState == UIApplicationStateInactive) {
            // 程序处于后台
            
        }
    
    }
    
    #pragma mark - 注册苹果推送
    /**
     *    注册苹果推送,获取deviceToken用于推送
     */
    - (void)registerAPNS:(UIApplication *)application {
        
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
            // iOS 8 Notifications
            [application registerUserNotificationSettings:
             [UIUserNotificationSettings settingsForTypes:
              (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)
                                               categories:nil]];
            [application registerForRemoteNotifications];
        }
    }
    /*
     *  苹果推送注册成功回调,将苹果返回的deviceToken上传到CloudPush服务器
     */
    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
        [CloudPushSDK registerDevice:deviceToken withCallback:^(CloudPushCallbackResult *res) {
            if (res.success) {
                NSLog(@"Register deviceToken success.");
            } else {
                NSLog(@"Register deviceToken failed, error: %@", res.error);
            }
        }];
    }
    /*
     *  苹果推送注册失败回调
     */
    - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
        NSLog(@"didFailToRegisterForRemoteNotificationsWithError %@", error);
    }
    
    
    #pragma mark - 初始化推送sdk
    - (void)initCloudPush {
        
        // SDK初始化,通过阿里推送的配置文件自动注册sdk
        [CloudPushSDK autoInit:^(CloudPushCallbackResult *res) {
            if (res.success) {
                
                NSLog(@"Push SDK init success, deviceId: %@.", [CloudPushSDK getDeviceId]);
                [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%@",[CloudPushSDK getDeviceId]] forKey:DeviceID];
            } else {
                NSLog(@"Push SDK init failed, error: %@", res.error);
            }
        }];
    }
    
    
    #pragma mark - 注册推送通道打开监听 Channel Opened
    /**
     *    注册推送通道打开监听
     */
    - (void)listenerOnChannelOpened {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(onChannelOpened:)
                                                     name:@"CCPDidChannelConnectedSuccess"
                                                   object:nil];
    }
    
    /**
     *    推送通道打开回调
     */
    - (void)onChannelOpened:(NSNotification *)notification {
        NSLog(@"推送通道打开成功");
    }
    
    
    #pragma mark - 推送消息到来监听
    /**
     *    注册推送消息到来监听
     */
    - (void)registerMessageReceive {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(onMessageReceived:)
                                                     name:@"CCPDidReceiveMessageNotification"
                                                   object:nil];
    }
    /**
     *    处理到来推送消息
     */
    - (void)onMessageReceived:(NSNotification *)notification {
        CCPSysMessage *message = [notification object];
        NSString *title = [[NSString alloc] initWithData:message.title encoding:NSUTF8StringEncoding];
        NSString *body = [[NSString alloc] initWithData:message.body encoding:NSUTF8StringEncoding];
        NSLog(@"Receive message title: %@, content: %@.", title, body);
    }
    
    

    相关文章

      网友评论

          本文标题:阿里推送对接流程

          本文链接:https://www.haomeiwen.com/subject/dyvjaqtx.html