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
随机推荐
- python 基础习题
1.8<<2等于? 8 ---> 1000 32 ---> 100000 -----------结果--- 32 2.通过内置函数计算5除以2的余数 print(dir()) ...
- 在写一次epoll
epoll & select & poll只能处理IO相关的操作,epoll每一个操作必须注册到时间监控机制中,并且还需要进程或者线程进行管理. 多进程/多线程 和epoll相比较 e ...
- Spring + MyBatis 多数据源实现
近期,在项目中需要做分库,但是因为某些原因,没有采用开源的分库插件,而是采用了同事之前弄得多数据源形式实现的分库.对于多数据源,本人在实际项目也中遇到的不多,之前的项目大多是服务化,以RPC的形式获得 ...
- xshell 如何连接服务器
https://jingyan.baidu.com/article/ab69b270b0ca3d2ca7189fdc.html 点击“新建”之后就会出现下面这样一个界面,“名称”根据自己的需求填写,“ ...
- Python+Selenium 自动化实现实例-Xpath捕捉元素的几种方法
#coding=utf-8import timefrom selenium import webdriverdriver = webdriver.Chrome()driver.get("ht ...
- 《java并发编程实战》读书笔记2--对象的共享,可见性,安全发布,线程封闭,不变性
这章的主要内容是:如何共享和发布对象,从而使它们能够安全地由多个线程同时访问. 内存的可见性 确保当一个线程修改了对象状态后,其他线程能够看到发生的状态变化. 上面的程序中NoVisibility可能 ...
- Python实例 | 贴吧签到
第一步 查看HTTP请求的内容 首先需要通过浏览器或者其他http包分析软件来观察,签到的时候进行了什么请求. Firefox浏览器就足以做到这一点,Wireshark是更加专业的包分析软件,它除了能 ...
- js对象替换键值名称
js对象替换键值名称 将obj中的id和name字段替换分别替换成为“@id”,“@name” 代码如下: let obj = [{id:,name:,name:"李四"}].ma ...
- CSS布局和居中常用技巧
1.常用居中方法 居中在布局中很常见,我们假设DOM文档结构如下,子元素要在父元素中居中: <div class="parent"> <div class=&qu ...
- vue组件scoped CSS及/deep/深度选择器
参考链接:https://vue-loader.vuejs.org/zh/guide/scoped-css.html#%E5%AD%90%E7%BB%84%E4%BB%B6%E7%9A%84%E6%A ...