- 首先我们先去官网创建AppID和描述文件
- 创建AppId和描述文件
得到二个p12文件,要注意那个是开发的那个是生产的。
3.创建真机调试文件以及导入到项目中(这一步现在忽略不做了,苹果已经帮我们做好了,只需要在项目中登录账户就可以了)
4.在环信创建我们的应用和上传证书
注意要看清楚那个是开发那个是生产
5.集成环信到项目中
可以参考另外一篇文章iOS集成环信的会话列表
接着我们要去appledate.m文件里添加东西了,很重要一步,上代码
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,EMChatManagerDelegate>
@property (nonatomic, strong) UIWindow *window;
@end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
EMOptions *options = [EMOptions optionsWithAppkey:你的APPkey];
options.apnsCertName = @"Develop";//推送证书的名字
EMError *error = [[EMClient sharedClient]initializeSDKWithOptions:options];
if (!error) {
NSLog(@"初始化成功");
}
else{
NSLog(@"初始化失败 %@",error);
}
error = [[EMClient sharedClient]loginWithUsername:@"test3" password:@"123456"];
if (!error) {
NSLog(@"登录成功");
EMPushOptions *emoptions = [[EMClient sharedClient] pushOptions];
//设置有消息过来时的显示方式:1.显示收到一条消息 2.显示具体消息内容.
//自己可以测试下
emoptions.displayStyle = EMPushDisplayStyleMessageSummary;
[[EMClient sharedClient] updatePushOptionsToServer];
}
else{
NSLog(@"%@登录失败",error);
}
/**
注册APNS离线推送 iOS8 注册APNS
*/
if ([application respondsToSelector:@selector(registerForRemoteNotifications)]) {
[application registerForRemoteNotifications];
UIUserNotificationType notificationTypes = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:notificationTypes categories:nil];
[application registerUserNotificationSettings:settings];
}
else{
UIRemoteNotificationType notificationTypes = UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:notificationTypes];
}
//添加监听在线推送消息
[[EMClient sharedClient].chatManager addDelegate:self delegateQueue:nil];
return YES;
}
// 将得到的deviceToken传给SDK
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
NSLog(@"deviceToken: %@",deviceToken);
[[EMClient sharedClient] bindDeviceToken:deviceToken];
}
// 注册deviceToken失败
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
NSLog(@"error -- %@",error);
}
// APP进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[EMClient sharedClient] applicationDidEnterBackground:application];
}
// APP将要从后台返回
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[[EMClient sharedClient] applicationWillEnterForeground:application];
}