原文:http://m.blog.csdn.net/article/details?id=38824551

1.先创建一个apns证书,链接如下

  http://developer.easemob.com/docs/emchat/ios/push/certificate.html

  创建完证书后,将证书弄成p12文件,然后上传到环信后台

2.再创建真机调试证书,和描述文件,保证能进行真机调试。并且appid要又推送功能

3.绑定环信证书和appkey

//注册 APNS文件的名字, 需要与后台上传证书时的名字一一对应
NSString *apnsCertName = @"chatdemo";
[[EaseMob sharedInstance] registerSDKWithAppKey:@“” apnsCertName:apnsCertName];
[[EaseMob sharedInstance] enableBackgroundReceiveMessage];
[[EaseMob sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
4.然后实现环信的几个方法
// 收到消息回调
-(void)didReceiveMessage:(EMMessage *)message{ #if !TARGET_IPHONE_SIMULATOR
[self playSoundAndVibration]; BOOL isAppActivity = [[UIApplication sharedApplication] applicationState] == UIApplicationStateActive;
if (!isAppActivity) {
[self showNotificationWithMessage:message];
}
#endif
} - (void)playSoundAndVibration{ //如果距离上次响铃和震动时间太短, 则跳过响铃
NSLog(@"%@, %@", [NSDate date], self.lastPlaySoundDate);
NSTimeInterval timeInterval = [[NSDate date]
timeIntervalSinceDate:self.lastPlaySoundDate];
if (timeInterval < kDefaultPlaySoundInterval) {
return;
}
//保存最后一次响铃时间
self.lastPlaySoundDate = [NSDate date]; // 收到消息时,播放音频
[[EaseMob sharedInstance].deviceManager asyncPlayNewMessageSound];
// 收到消息时,震动
[[EaseMob sharedInstance].deviceManager asyncPlayVibration];
} - (void)showNotificationWithMessage:(EMMessage *)message{
id<IEMMessageBody> messageBody = [message.messageBodies firstObject];
NSString *messageStr = nil;
switch (messageBody.messageBodyType) {
case eMessageBodyType_Text:
{
messageStr = ((EMTextMessageBody *)messageBody).text;
}
break;
case eMessageBodyType_Image:
{
messageStr = @"[图片]";
}
break;
case eMessageBodyType_Location:
{
messageStr = @"[位置]";
}
break;
case eMessageBodyType_Voice:
{
messageStr = @"[音频]";
}
break;
case eMessageBodyType_Video:{
messageStr = @"[视频]";
}
break;
default:
break;
} //发送本地推送
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate date]; //触发通知的时间 NSString *title = message.from;
if (message.isGroup) {
NSArray *groupArray = [[EaseMob sharedInstance].chatManager groupList];
for (EMGroup *group in groupArray) {
if ([group.groupId isEqualToString:message.conversation.chatter]) {
title = [NSString stringWithFormat:@"%@(%@)", message.groupSenderName, group.groupSubject];
break;
}
}
} notification.alertBody = [NSString stringWithFormat:@"%@:%@", title, messageStr];
notification.alertAction = @"打开";
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName; //发送通知
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
UIApplication *application = [UIApplication sharedApplication];
application.applicationIconBadgeNumber += ;
}
这样就可以进行消息推送了

经过自己的研究,个人拓展一下:一般接收消息推送通知放在主控制器去操作,只需要在主控制器注册代理,即可回调,发送通知

//**************************************************远程通知部分*******************************************************//

//两次提示的默认间隔

#import "MainViewController.h"

static const CGFloat kDefaultPlaySoundInterval = 3.0;

static NSString *kMessageType = @"MessageType";

static NSString *kConversationChatter = @"ConversationChatter";


@interface MainViewController ()<EMChatManagerChatDelegate>

@property (strong, nonatomic) NSDate *lastPlaySoundDate;

@end

#pragma - 远程通知部分

- (BOOL)needShowNotification:(NSString *)fromChatter
{
BOOL ret = YES;
NSArray *igGroupIds = [[EaseMob sharedInstance].chatManager ignoredGroupIds];
for (NSString *str in igGroupIds) {
if ([str isEqualToString:fromChatter]) {
ret = NO;
break;
}
}
return ret;
} // 收到消息回调
-(void)didReceiveMessage:(EMMessage *)message
{
BOOL needShowNotification = (message.messageType != eMessageTypeChat) ? [self needShowNotification:message.conversationChatter] : YES;
if (needShowNotification) {
#if !TARGET_IPHONE_SIMULATOR UIApplicationState state = [[UIApplication sharedApplication] applicationState];
switch (state) {
case UIApplicationStateActive:
[self playSoundAndVibration];
break;
case UIApplicationStateInactive:
[self playSoundAndVibration];
break;
case UIApplicationStateBackground:
[self showNotificationWithMessage:message];
break;
default:
break;
}
#endif
}
}

//透传消息
-(void)didReceiveCmdMessage:(EMMessage *)message
{
[self showHint:NSLocalizedString(@"receiveCmd", @"receive cmd message")];
} - (void)playSoundAndVibration{
NSTimeInterval timeInterval = [[NSDate date]
timeIntervalSinceDate:self.lastPlaySoundDate];
if (timeInterval < kDefaultPlaySoundInterval) {
//如果距离上次响铃和震动时间太短, 则跳过响铃
NSLog(@"skip ringing & vibration %@, %@", [NSDate date], self.lastPlaySoundDate);
return;
} //保存最后一次响铃时间
self.lastPlaySoundDate = [NSDate date]; // 收到消息时,播放音频
[[EMCDDeviceManager sharedInstance] playNewMessageSound]; // 收到消息时,震动
[[EMCDDeviceManager sharedInstance] playVibration];
} - (void)showNotificationWithMessage:(EMMessage *)message
{
EMPushNotificationOptions *options = [[EaseMob sharedInstance].chatManager pushNotificationOptions];
//发送本地推送
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate date]; //触发通知的时间 if (options.displayStyle == ePushNotificationDisplayStyle_messageSummary) {
id<IEMMessageBody> messageBody = [message.messageBodies firstObject];
NSString *messageStr = nil;
switch (messageBody.messageBodyType) {
case eMessageBodyType_Text:
{
messageStr = ((EMTextMessageBody *)messageBody).text;
}
break;
case eMessageBodyType_Image:
{
messageStr = NSLocalizedString(@"message.image", @"Image");
}
break;
case eMessageBodyType_Location:
{
messageStr = NSLocalizedString(@"message.location", @"Location");
}
break;
case eMessageBodyType_Voice:
{
messageStr = NSLocalizedString(@"message.voice", @"Voice");
}
break;
case eMessageBodyType_Video:{
messageStr = NSLocalizedString(@"message.video", @"Video");
}
break;
default:
break;
} // NSString *title = [[UserProfileManager sharedInstance] getNickNameWithUsername:message.from];
NSString *title = message.from;
if (message.messageType == eMessageTypeGroupChat) {
// NSArray *groupArray = [[EaseMob sharedInstance].chatManager groupList];
// for (EMGroup *group in groupArray) {
// if ([group.groupId isEqualToString:message.conversationChatter]) {
// title = [NSString stringWithFormat:@"%@(%@)", message.groupSenderName, group.groupSubject];
// break;
// }
// }
}
else if (message.messageType == eMessageTypeChatRoom)
{
// NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
// NSString *key = [NSString stringWithFormat:@"OnceJoinedChatrooms_%@", [[[EaseMob sharedInstance].chatManager loginInfo] objectForKey:@"username" ]];
// NSMutableDictionary *chatrooms = [NSMutableDictionary dictionaryWithDictionary:[ud objectForKey:key]];
// NSString *chatroomName = [chatrooms objectForKey:message.conversationChatter];
// if (chatroomName)
// {
// title = [NSString stringWithFormat:@"%@(%@)", message.groupSenderName, chatroomName];
// }
} notification.alertBody = [NSString stringWithFormat:@"%@:%@", title, messageStr];
}
else{
notification.alertBody = NSLocalizedString(@"receiveMessage", @"you have a new message");
} #warning 去掉注释会显示[本地]开头, 方便在开发中区分是否为本地推送
// notification.alertBody = [[NSString alloc] initWithFormat:@"[本地]%@", notification.alertBody]; notification.alertAction = NSLocalizedString(@"open", @"Open");
notification.timeZone = [NSTimeZone defaultTimeZone];
NSTimeInterval timeInterval = [[NSDate date] timeIntervalSinceDate:self.lastPlaySoundDate];
if (timeInterval < kDefaultPlaySoundInterval) {
NSLog(@"skip ringing & vibration %@, %@", [NSDate date], self.lastPlaySoundDate);
} else {
notification.soundName = UILocalNotificationDefaultSoundName;
self.lastPlaySoundDate = [NSDate date];
} NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
[userInfo setObject:[NSNumber numberWithInt:message.messageType] forKey:kMessageType];
[userInfo setObject:message.conversationChatter forKey:kConversationChatter];
notification.userInfo = userInfo; //发送通知
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
UIApplication *application = [UIApplication sharedApplication];
application.applicationIconBadgeNumber += ;
}

iOS: 环信的推送的更多相关文章

  1. iOS 环信集成项目应用

    环信iOS端3.0版本集成记录--聊天界面篇 环信离线推送证书... 1,环信处在后台的时候,消息的接收与推送 离线发推送 配置属性 EMCallOptions *options = [[EMClie ...

  2. 基于APNs最新HTTP/2接口实现iOS的高性能消息推送(服务端篇)

    1.前言 本文要分享的消息推送指的是当iOS端APP被关闭或者处于后台时,还能收到消息/信息/指令的能力. 这种在APP处于后台或关闭情况下的消息推送能力,通常在以下场景下非常有用: 1)IM即时通讯 ...

  3. iOS开发之远程推送

    说到远程推送,应该用的也挺多的,今天就基于SEA的云推送服务,做一个推送的小demo,来了解一下iOS中的远程推送是怎么一回事儿,首先你得有苹果的开发者账号,好咸蛋也差不多了,主要内容走起. 一.准备 ...

  4. IOS个人帐号推送证书创建

    (IOS个人帐号推送证书制作所有步骤: 可以直接将产品推送证书和开发者推送证书一起制作到一个Identifier帐号下) 一. 首先需要创建一个id:有推送功能的(App ID Suffix)只有它才 ...

  5. (转)在SAE使用Apple Push Notification Service服务开发iOS应用, 实现消息推送

    在SAE使用Apple Push Notification Service服务开发iOS应用, 实现消息推送 From: http://saeapns.sinaapp.com/doc.html 1,在 ...

  6. 李洪强iOS之集成极光推送三iOS集成指南

    李洪强iOS之集成极光推送三iOS集成指南 SDK说明 适用版本 本文匹配的 SDK版本:r2.1.5 以后.查看最近更新了解最新的SDK更新情况.使用Xcode 6及以上版本可以使用新版Push S ...

  7. 李洪强iOS之集成极光推送二iOS 证书 设置指南

    李洪强iOS之集成极光推送二iOS 证书 设置指南 创建应用程序ID 登陆 iOS Dev Center 选择进入iOS Provisioning Portal. 在 iOS Provisioning ...

  8. 李洪强iOS之集成极光推送一iOS SDK概述

    李洪强iOS之集成极光推送一iOS SDK概述 JPush iOS 从上图可以看出,JPush iOS Push 包括 2 个部分,APNs 推送(代理),与 JPush 应用内消息. 红色部分是 A ...

  9. 李洪强iOS开发之极光推送JPush

    李洪强iOS开发之极光推送JPush

随机推荐

  1. python 基础 习题

    1.执行 Python 脚本的两种方式2.简述位.字节的关系 1Byte = 8bits 3.简述 ascii.unicode.utf-8.gbk 的关系 都是字符集,unicode兼容其他3种字符集 ...

  2. 【python】pip的使用

    来源:http://www.ttlsa.com/python/how-to-install-and-use-pip-ttlsa/ pip是用来安装python相关的包的.使用参数如下: # pip - ...

  3. LeetCode解题报告—— Group Anagrams & Pow(x, n) & Spiral Matrix

    1. Group Anagrams Given an array of strings, group anagrams together. For example, given: ["eat ...

  4. 《逐梦旅程 WINDOWS游戏编程之从零开始》笔记8——载入三维模型&Alpha混合技术&深度测试与Z缓存

    第17章 三维游戏模型的载入 主要是如何从3ds max中导出.X文件,以及如何从X文件加载三维模型到DirextX游戏程序里.因为复杂的3D物体,要用代码去实现,那太反人类了,所以我们需要一些建模软 ...

  5. 机器学习方法:回归(一):线性回归Linear regression

    欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 开一个机器学习方法科普系列:做基础回顾之用,学而时习之:也拿出来与大家分享.数学水平有限,只求易懂,学习与工 ...

  6. PHP PDO类 单例

    <?php /*//pdo连接信息 $pdo=array("mysql:host=localhost;dbname=demo;charset=utf8","root ...

  7. Python中的多进程:fork和multiprocessing

    Python的多进程 套路1:os.fork() 先敲段代码: #!/usr/bin/env python3 import os os.fork() print('1111111111') 执行结果: ...

  8. Codeforces 714B. Filya and Homework

    题目链接:http://codeforces.com/problemset/problem/714/B 题意: 给你一个含有 n 个数的数组, 问你是否存在一个 x, 使得这个数组中的某些数加上 x, ...

  9. jq函数绑定与解绑

    最近学到几个新的jq函数 1.bind()绑定函数 2.unbind()解绑函数 3.add() .给元素追加字符串 4.addClass() 给某元素增加class属性值

  10. Visual Studio 2017启动x86的Android模拟器失败

     Visual Studio 2017启动x86的Android模拟器失败 Visual Studio 2017默认提供多个Android模拟器.其中,x86模拟器运行较快.但是由于和Hyper-V服 ...