宽容他人,放过自己。

环信推送相关

Posted on By anchoriteFili

环信推送相关文档

添加三个相关证书(p12文件)

752372-20160512153149749-1362327949.png

电脑上安装三个证书,并且添加相关的描述文件

752372-20160512153355499-2115791534.png

在xcode上配置相关信息

752372-20160512153640718-241842941.png

相关代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    //注册环信
    [[EaseMob sharedInstance] registerSDKWithAppKey:@"anchoritefiligod#testone" apnsCertName:@"Development"];
       //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];
    }
    
    [[EaseMob sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
    MainViewController *main = [[MainViewController alloc] init];

    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:main];
    self.window.rootViewController = nav;
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}
//-(void)addLocalNotification{
//    
//    //定义本地通知对象
//    UILocalNotification *notification=[[UILocalNotification alloc]init];
//    //设置调用时间
//    notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:1.0];//通知触发的时间,10s以后
//    notification.repeatInterval=2;//通知重复次数
//    //notification.repeatCalendar=[NSCalendar currentCalendar];//当前日历,使用前最好设置时区等信息以便能够自动同步时间
//    
//    //设置通知属性
//    notification.alertBody=@"最近添加了诸多有趣的特性,是否立即体验?"; //通知主体
//    notification.applicationIconBadgeNumber=1;//应用程序图标右上角显示的消息数
//    notification.alertAction=@"打开应用"; //待机界面的滑动动作提示
//    notification.alertLaunchImage=@"Default";//通过点击通知打开应用时的启动图片,这里使用程序启动图片
//    //notification.soundName=UILocalNotificationDefaultSoundName;//收到通知时播放的声音,默认消息声音
//    notification.soundName=@"msg.caf";//通知声音(需要真机才能听到声音)
//    
//    //设置用户信息
//    notification.userInfo=@{@"id":@1,@"user":@"Kenshin Cui"};//绑定到通知上的其他附加信息
//    
//    //调用通知
//    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
//}



#ifdef __IPHONE_8_0


- (void)application:(UIApplication*)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    [application registerForRemoteNotifications];
}


#endif
// 将得到的deviceToken传给SDK
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    [[EaseMob sharedInstance] application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
    NSString *str = [NSString
                     stringWithFormat:@"Device Token=%@",deviceToken];
    NSLog(@"%@",str);
    [define setObject:str forKey:@"MyToken"];
}

// 注册deviceToken失败
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    [[EaseMob sharedInstance] application:application didFailToRegisterForRemoteNotificationsWithError:error];
//    NSLog(@"Register error -- %@",error);
    
//    [[[UIAlertView alloc] initWithTitle:@"警告" message:error delegate:self cancelButtonTitle:nil otherButtonTitles:@"取消", nil]show ];

}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
//    [[[UIAlertView alloc] initWithTitle:@"警告" message:@"收到错误" delegate:self cancelButtonTitle:nil otherButtonTitles:@"取消", nil]show ];
 
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    [[EaseMob sharedInstance] applicationWillEnterForeground:application];
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
        [[UIApplication sharedApplication]setApplicationIconBadgeNumber:0];//进入前台取消应用消息图标
}

-(void)didReceiveRemoteNotification:(NSDictionary *)userInfo{
    NSLog(@"成功推送");
//      [self addLocalNotification];
    NSError *parseError = nil;
    NSData  *jsonData = [NSJSONSerialization dataWithJSONObject:userInfo
                                                        options:NSJSONWritingPrettyPrinted error:&parseError];
    NSString *str =  [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"apns.content", @"Apns content")
                                                    message:str
                                                   delegate:nil
                                          cancelButtonTitle:NSLocalizedString(@"ok", @"OK")
                                          otherButtonTitles:nil];
    [alert show];
//    [[[UIAlertView alloc] initWithTitle:@"警告" message:@"收到推送" delegate:self cancelButtonTitle:nil otherButtonTitles:@"取消", nil]show ];
//
//    for (id key in userInfo) {
//        NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
//    }
}
- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
        [[EaseMob sharedInstance] applicationWillTerminate:application];
}