EMChatManagerDelegate这个代理要设置,
//收到消息
-(void)didReceiveMessages:(NSArray *)aMessages{
//判断是不是后台,如果是后台就发推送
if (aMessages.count==0) {
return ;
}
//设置声音
AudioServicesPlaySystemSound(1312);
// //音效文件路径
// NSString *path = [[NSBundle mainBundle] pathForResource:@"haoyebao" ofType:@"wav"];
// NSLog(@"path---%@",path);
// //组装并播放音效
// SystemSoundID soundID;
// NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
// AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID);
// AudioServicesPlaySystemSound(soundID);
//声音停止
// AudioServicesDisposeSystemSoundID(soundID);
for (EMMessage*message in aMessages) {
UIApplicationState state =[[UIApplication sharedApplication] applicationState];
switch (state) {
//前台运行
case UIApplicationStateActive:
[self showPushNotificationMessage:message];
break;
//待激活状态
case UIApplicationStateInactive:
break;
//后台状态
case UIApplicationStateBackground:
[self showPushNotificationMessage:message];
break;
default:
break;
}
}
}
-(void)showPushNotificationMessage:(EMMessage *)message{
EMPushOptions *options = [[EMClient sharedClient] pushOptions];
NSString *alertBody = nil;
if (options.displayStyle == EMPushDisplayStyleMessageSummary) {
EMMessageBody *messageBody = message.body;
NSString *messageStr = nil;
switch (messageBody.type) {
case EMMessageBodyTypeText:
{
messageStr = ((EMTextMessageBody *)messageBody).text;
}
break;
case EMMessageBodyTypeImage:
{
messageStr = NSLocalizedString(@"message.image", @"Image");
}
break;
case EMMessageBodyTypeLocation:
{
messageStr = NSLocalizedString(@"message.location", @"Location");
}
break;
case EMMessageBodyTypeVoice:
{
messageStr = NSLocalizedString(@"message.voice", @"Voice");
}
break;
case EMMessageBodyTypeVideo:{
messageStr = NSLocalizedString(@"message.video", @"Video");
}
break;
default:
break;
}
}else{
alertBody = NSLocalizedString(@"您有新的消息,请点击查看", @"you have a new message");
}
NSTimeInterval timeInterval = [[NSDate date] timeIntervalSinceDate:self.lastPlaySoundDate];
BOOL playSound = NO;
if (!self.lastPlaySoundDate || timeInterval >= kDefaultPlaySoundInterval) {
self.lastPlaySoundDate = [NSDate date];
playSound = YES;
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
[userInfo setObject:[NSNumber numberWithInt:message.chatType] forKey:kMessageType];
[userInfo setObject:message.conversationId forKey:kConversationChatter];
//发送本地推送
UILocalNotification *notification = [[UILocalNotification alloc] init];
if (NSClassFromString(@"UNUserNotificationCenter")) {
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:0.01 repeats:NO];
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
if (playSound) {
content.sound = [UNNotificationSound defaultSound];
}
content.body =alertBody;
content.userInfo = userInfo;
[UIApplication sharedApplication].applicationIconBadgeNumber +=1;
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:message.messageId content:content trigger:trigger];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:nil];
}
else {
//发现没走
// UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate date]; //触发通知的时间
notification.alertBody = alertBody;
notification.alertAction = NSLocalizedString(@"open", @"Open");
notification.timeZone = [NSTimeZone defaultTimeZone];
if (playSound) {
notification.soundName = UILocalNotificationDefaultSoundName;
}
notification.userInfo = userInfo;
[UIApplication sharedApplication].applicationIconBadgeNumber +=1;
//发送通知
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}
}
如有疑问,可联系环信客服