博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ios中当收到环信消息时设置本地推送的方法
阅读量:6921 次
发布时间:2019-06-27

本文共 3773 字,大约阅读时间需要 12 分钟。

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];

    }

 

}

}

 

如有疑问,可联系环信客服

转载于:https://www.cnblogs.com/xiwanxiang190351/p/6558057.html

你可能感兴趣的文章
elasticsearch集群搭建手册
查看>>
如何正确认识XMind软件格式
查看>>
OSChina 周一乱弹 ——有2个小混蛋大晚上……
查看>>
JNI/NDK开发指南(二)——JVM查找java native方法的规则
查看>>
Installing / Updating Python on OS X
查看>>
Android解析XML文件
查看>>
Spring AOP前置通知和后置通知
查看>>
Windows 之间用rsync同步数据(cwRsyncServer配置)
查看>>
android gps定位
查看>>
JFinal AOP学习笔记
查看>>
idea 查看Java类字节码 自定义工具的使用
查看>>
“效果系列一”:jQuery 手风琴效果
查看>>
java相对路径获取(读取)文件
查看>>
VS上写helloworld
查看>>
反射机制——获取Class对象的三种方式
查看>>
react-native 初始化指定版本
查看>>
怎样看服务器负载状况好坏
查看>>
NSCopying
查看>>
总结&终结帖:博客迁移到poos.github.io
查看>>
使用 Postman 与 Kotlin 交互REST API接口数据
查看>>