iOS: 环信的推送
原文: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: 环信的推送的更多相关文章
- iOS 环信集成项目应用
环信iOS端3.0版本集成记录--聊天界面篇 环信离线推送证书... 1,环信处在后台的时候,消息的接收与推送 离线发推送 配置属性 EMCallOptions *options = [[EMClie ...
- 基于APNs最新HTTP/2接口实现iOS的高性能消息推送(服务端篇)
1.前言 本文要分享的消息推送指的是当iOS端APP被关闭或者处于后台时,还能收到消息/信息/指令的能力. 这种在APP处于后台或关闭情况下的消息推送能力,通常在以下场景下非常有用: 1)IM即时通讯 ...
- iOS开发之远程推送
说到远程推送,应该用的也挺多的,今天就基于SEA的云推送服务,做一个推送的小demo,来了解一下iOS中的远程推送是怎么一回事儿,首先你得有苹果的开发者账号,好咸蛋也差不多了,主要内容走起. 一.准备 ...
- IOS个人帐号推送证书创建
(IOS个人帐号推送证书制作所有步骤: 可以直接将产品推送证书和开发者推送证书一起制作到一个Identifier帐号下) 一. 首先需要创建一个id:有推送功能的(App ID Suffix)只有它才 ...
- (转)在SAE使用Apple Push Notification Service服务开发iOS应用, 实现消息推送
在SAE使用Apple Push Notification Service服务开发iOS应用, 实现消息推送 From: http://saeapns.sinaapp.com/doc.html 1,在 ...
- 李洪强iOS之集成极光推送三iOS集成指南
李洪强iOS之集成极光推送三iOS集成指南 SDK说明 适用版本 本文匹配的 SDK版本:r2.1.5 以后.查看最近更新了解最新的SDK更新情况.使用Xcode 6及以上版本可以使用新版Push S ...
- 李洪强iOS之集成极光推送二iOS 证书 设置指南
李洪强iOS之集成极光推送二iOS 证书 设置指南 创建应用程序ID 登陆 iOS Dev Center 选择进入iOS Provisioning Portal. 在 iOS Provisioning ...
- 李洪强iOS之集成极光推送一iOS SDK概述
李洪强iOS之集成极光推送一iOS SDK概述 JPush iOS 从上图可以看出,JPush iOS Push 包括 2 个部分,APNs 推送(代理),与 JPush 应用内消息. 红色部分是 A ...
- 李洪强iOS开发之极光推送JPush
李洪强iOS开发之极光推送JPush
随机推荐
- Matcher匹配器查找字符串指定内容
public static void main(String[] args) { String s = "我的手机号码是18988888888,曾经用过18987654321,还用过1881 ...
- Hive分组取第一条记录
需求 交易系统,财务要求维护每个用户首个交易完成的订单数据(首单表,可取每个用户交易完成时间最老的订单数据).举例: 简写版的表结构: 表数据: 则 财务希望汇总记录如下: uid order_id ...
- 在Redis集群中使用pipeline批量插入
在Redis集群中使用pipeline批量插入 由于项目中需要使用批量插入功能, 所以在网上查找到了Redis 批量插入可以使用pipeline来高效的插入, 示例代码如下: Pipeline p = ...
- Decode Ways——动态规划
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- git在window与linux的换行符问题
1:背景.我win7,后端是win10,使用了TortoiseGit工具.我使用ssh,他使用http.仓库是在linux,使用gitLab管理 2:问题.仓库是总监之前建好的.后端把文件add后pu ...
- mysql中的制表符替换
-- 问题重现set @v_t=' ';select hex(@v_t) -- 原因: -- ASCII格式的hex进展ox09对应char为(ht)制表tab,喔原来是有很多的制表符吧. ...
- Delphi获取毫秒级时间戳
function GetJavaTime( d: TDateTime ): Int64; var dJavaStart: TDateTime; begin //java里的时间是从1970年1月1日0 ...
- Qtp测试中的密码问题
Qtp中,一般录制的脚本进行回放的时候就会遇到各种各样的问题,导致回放不能顺利进行,比如这次我在录制一个简单的登陆脚本的时候,就遇到了这样的问题: [遇到问题] 1, 录制好的脚本中的密码不能正常输 ...
- vue-music 关于Search(搜索页面)--上拉加载
建立搜索框组件页面,searchBox,组件接受一个可以自定义传入的placeholder 属性.input v-model 双向绑定数据关联到query 中, 在created中监听 quer ...
- 将cmake文件转化为vs方便代码阅读与分析
下面通过“chengxuyuancc”同学的图来说明.通过cmake将winafl cmake编译方式转化为vs2015,方便源码阅读与分析. 1.到官网下载cmake软件.启动图形版 2.选择源码目 ...