融云即时通 :消息推送篇

RongIM 消息推送 分为二类:
1.远程推送:指 程序在后台 2分钟后被杀死,通过远程推送方式,实现消息通知
2.本地推送:程序在后台存活时,若有消息,将以本地推送方式通知

实现消息推送步骤:
第一步:配置证书 上传P12 证书
具体方式 见:http://www.rongcloud.cn/docs/ios_push.html#push
ps:APP本身一定要打开推送

第二步:处理推送

pragma mark - 推送处理

  • (void)registerRongIMPushWith:(UIApplication *)application{

    /**

    • 推送处理1
      */
      if ([application
      respondsToSelector:@selector(registerUserNotificationSettings:)]) {
      //注册推送, 用于iOS8以及iOS8之后的系统
      UIUserNotificationSettings *settings = [UIUserNotificationSettings
      settingsForTypes:(UIUserNotificationTypeBadge |
      UIUserNotificationTypeSound |
      UIUserNotificationTypeAlert)
      categories:nil];
      [application registerUserNotificationSettings:settings];
      } else {
      //注册推送,用于iOS8之前的系统
      UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge |
      UIRemoteNotificationTypeAlert |
      UIRemoteNotificationTypeSound;
      [application registerForRemoteNotificationTypes:myTypes];
      }

}
/**

  • 推送处理2
    */
    //注册用户通知设置
  • (void)application:(UIApplication *)application didRegisterUserNotificationSettings:
    (UIUserNotificationSettings *)notificationSettings {
    // register to receive notifications
    [application registerForRemoteNotifications];
    }

/**

  • 推送处理3
    */
  • (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSString *token =
    [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<" withString:@""]stringByReplacingOccurrencesOfString:@">"withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""];

    [[RCIMClient sharedRCIMClient] setDeviceToken:token];
    }

到此,远程推送基本实现

第三步:本地推送
本地推送需要设置代理方法
Delegate:
[RCIM sharedRCIM].receiveMessageDelegate = self;
[RCIM sharedRCIM].userInfoDataSource = self;

代理方法如下:

pragma mark - 获取用户信息

  • (void)getUserInfoWithUserId:(NSString )userId completion:(void(^)(RCUserInfo userInfo))completion
    {
    if ([userId isEqualToString:@"13501030554"]) {

            NSLog(@"+++++++++++++++++++++++++++++++++");
      return completion([[RCUserInfo alloc] initWithUserId:userId name:@"当前登录用户的用户名" portrait:@"当前登录用户头像的url"]);
    

    }else
    {
    // 根据存储联系人信息的模型,通过 userId 来取得对应的name和头像url,进行以下设置(此处因为项目接口尚未实现,所以就只能这样给大家说说,请见谅)

              NSLog(@"============================");
      
      return completion([[RCUserInfo alloc] initWithUserId:userId name:@"name" portrait:@"http://pic32.nipic.com/20130827/12906030_123121414000_2.png"]);
    

    }

}

pragma mark - 消息接收器代理

//在前台和后台活动状态时收到任何消息都会执行。

  • (void)onRCIMReceiveMessage:(RCMessage *)message left:(int)left{

    NSLog(@"活跃状态接受的消息:%@",message);

}

  • (BOOL)onRCIMCustomLocalNotification:(RCMessage *)message withSenderName:(NSString *)senderName{

    NSLog(@"发送者是 :%@",senderName);
    NSLog(@"发送的消息是 : %@",message);
    return NO;
    }

ps:需要注意的两点:

    • (BOOL)onRCIMCustomLocalNotification:(RCMessage *)message withSenderName:(NSString *)senderName 返回值应该为NO
      2.一定要设置用户信息- (void)getUserInfoWithUserId:(NSString )userId completion:(void(^)(RCUserInfo userInfo))completion

你可能感兴趣的:(融云即时通 :消息推送篇)