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

    }

 

}

}

 

如有疑問,可聯系環信客服


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM