ios中当收到环信消息时设置本地推送的方法

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值