远程推送iOS开发经验收集iOS进阶之路

ios 个推推送整理

2016-04-13  本文已影响6229人  Minoz_min

个推配置参考个推文档 http://docs.getui.com/mobile/ios/overview/

特别说明:

ios的消息是分两部分的 一部分是走apn的通知栏,消息 另一部分是走我们通道的透传消息 这两部分是服务端推送代码里面你们的人员会设定好的 分别是两个不同的方法 如果消息下发的时候 你客户端是在后台的(也就是客户端是离线)那么会收到apn的通知 透传消息就进离线了 只有你下次在线的时候 (也就是下次应用到前台的时候)才会下发下来 如果下发的时候应用是在前台的 那这样的话客户端就直接收到透传消息了 apn那部分消息就不会下发了
app没启动,或者在后台,或者锁屏,都是离线状态,你推送透传时,首先走的是苹果的apns通道,此时会收到apn通知,点开通知,进入应用,app就在线了,就会走个推通道,下发透传消息

1.启动个推

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    //启动个推SDK appid, appkey, appsecret
    [GeTuiSdk startSdkWithAppId:GetuiAppId appKey:GetuiAppKey appSecret:GetuiAppSecret delegate:self];
    // 注册APNS
    [self registerUserNotification];
    #//是否允许SDK 后台运行(这个一定要设置,否则后台apns不会执行)
    [GeTuiSdk runBackgroundEnable:true];
   
    return YES;
}

2.注册用户通知

/** 注册用户通知 */
- (void)registerUserNotification {
    
    /*
     注册通知(推送)
     申请App需要接受来自服务商提供推送消息
     */
    
    // 判读系统版本是否是“iOS 8.0”以上
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 ||
        [UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {
        
        // 定义用户通知类型(Remote.远程 - Badge.标记 Alert.提示 Sound.声音)
        UIUserNotificationType types = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
        
        // 定义用户通知设置
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
        
        // 注册用户通知 - 根据用户通知设置
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    } else { // iOS8.0 以前远程推送设置方式
        // 定义远程通知类型(Remote.远程 - Badge.标记 Alert.提示 Sound.声音)
        UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
        
        // 注册远程通知 -根据远程通知类型
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
    }
}

#pragma mark - 用户通知(推送)回调 _IOS 8.0以上使用

/** 已登记用户通知 */
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
    // 注册远程通知(推送)
    [application registerForRemoteNotifications];
}

3.远程通知(推送)回调

#pragma mark - 远程通知(推送)回调

/** 远程通知注册成功委托 */
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSLog(@"\n>>>[DeviceToken Success]:%@\n\n", token);
    //向个推服务器注册deviceToken
    [GeTuiSdk registerDeviceToken:token];
}

/** 远程通知注册失败委托 */
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"\n>>>[DeviceToken Error]:%@\n\n", error.description);
}

4.Background Fetch 接口回调

#pragma mark - Background Fetch 接口回调
//后台刷新数据
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    /// Background Fetch 恢复SDK 运行
    [GeTuiSdk resume];
    completionHandler(UIBackgroundFetchResultNewData);
}

5.APP运行中接收到通知(推送)处理

#pragma mark - APP运行中接收到通知(推送)处理

/** APP已经接收到“远程”通知(推送) - (App运行在后台/App运行在前台) */
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    application.applicationIconBadgeNumber = 0; // 标签
    
    NSLog(@"\n>>>[Receive RemoteNotification]:%@\n\n", userInfo);
}

#apns通道---离线
/** APP已经接收到“远程”通知(推送) - 透传推送消息  */
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
    
    // 处理APN
    self.isAPNS = true;
    completionHandler(UIBackgroundFetchResultNewData);
}

6.GeTuiSdkdelegate 注册回调,获取CID信息

#pragma mark - GeTuiSdkdelegate 注册回调,获取CID信息

/** SDK启动成功返回cid */
- (void)GeTuiSdkDidRegisterClient:(NSString *)clientId {
    //个推SDK已注册,返回clientId
    NSLog(@"\n>>>[GeTuiSdk RegisterClient]:%@\n\n", clientId);
}

/** SDK遇到错误回调 */
- (void)GeTuiSdkDidOccurError:(NSError *)error {
    //个推错误报告,集成步骤发生的任何错误都在这里通知,如果集成后,无法正常收到消息,查看这里的通知。
    NSLog(@"\n>>>[GexinSdk error]:%@\n\n", [error localizedDescription]);
}

#个推透传消息通道---在线
- (void)GeTuiSdkDidReceivePayloadData:(NSData *)payloadData andTaskId:(NSString *)taskId andMsgId:(NSString *)msgId andOffLine:(BOOL)offLine fromGtAppId:(NSString *)appId {
    
    // [4]: 收到个推消息
    NSString *payloadMsg = nil;
    if (payloadData) {
        payloadMsg = [[NSString alloc] initWithBytes:payloadData.bytes length:payloadData.length encoding:NSUTF8StringEncoding];
        NSData *data = [[NSData alloc] initWithData:[payloadMsg dataUsingEncoding:NSUTF8StringEncoding]];
        self.payloadDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    }
    
    //isAPNS==true 处理apns消息  离线状态
    if (self.isAPNS) {
        self.isAPNS = false;
        [HSQSchema openURL:[NSURL URLWithString:[[self.payloadDic valueForKey:@"message"] valueForKey:@"schema"]]];
    }else {
        NSString *string = [[self.payloadDic valueForKey:@"message"] valueForKey:@"schema"];
        if (string.length == 0 || [string isEqualToString:StringToastNone]) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[self.payloadDic valueForKey:@"title"] message:[self.payloadDic valueForKey:@"content"] delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
            [alertView show];
        }else {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[self.payloadDic valueForKey:@"title"] message:[self.payloadDic valueForKey:@"content"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"前往", nil];
            [alertView show];
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读