iOS发送本地通知

来源:互联网 发布:淘宝二手iphone店铺 编辑:程序博客网 时间:2024/06/14 00:45
//发送本地推送,使用环信sdk- (void)showNotificationWithMessage:(EMMessage *)message{    EMPushOptions *options = [[EMClient sharedClient] pushOptions];    [[UIApplication sharedApplication] cancelAllLocalNotifications];    UILocalNotification *notification = [[UILocalNotification alloc] init];    notification.fireDate = [NSDate date]; //触发通知的时间    if (options.displayStyle == EMPushDisplayStyleMessageSummary) {        EMMessageBody *messageBody = message.body;        NSString *messageStr = nil;        switch (messageBody.type) {            case EMMessageBodyTypeText:{                messageStr = ((EMTextMessageBody *)messageBody).text;            }                break;            case EMMessageBodyTypeImage:{                messageStr = @"发来一张图片";            }                break;            case EMMessageBodyTypeLocation:{                messageStr = @"发来ta的位置";            }                break;            case EMMessageBodyTypeVoice:{                messageStr = @"发来一段语音";            }                break;            default:                break;        }        NSString *fromNameStr;        NSMutableArray *array = [[DBInstance shareInstance] selectCharttingID:message.from];        if([message.from isEqualToString:@"jm_assistant"]){            fromNameStr = @"金脉小助手";        }else if(array.count){            fromNameStr = [array[0] realname];        }else{            fromNameStr = @"新的好友";        }        if (message.chatType == EMChatTypeGroupChat) {            NSArray *groupArray = [[EMClient sharedClient].groupManager getJoinedGroups];            for (EMGroup *group in groupArray) {                if ([group.groupId isEqualToString:message.conversationId]) {                    break;                }            }        }else if (message.chatType == EMChatTypeChatRoom){            NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];            NSString *key = [NSString stringWithFormat:@"OnceJoinedChatrooms_%@", [[EMClient sharedClient] currentUsername]];            NSMutableDictionary *chatrooms = [NSMutableDictionary dictionaryWithDictionary:[ud objectForKey:key]];            NSString *chatroomName = [chatrooms objectForKey:message.conversationId];            if(chatroomName){                            }        }else{            messageStr = [NSString stringWithFormat:@"%@:%@", fromNameStr, messageStr];        }        notification.alertBody = [NSString stringWithFormat:@"%@", messageStr];    }else{        notification.alertBody = @"您有一条新消息";    }    notification.userInfo = @{@"msgtype":@(1),@"mid":@(0),@"f":message.from};    notification.alertAction = NSLocalizedString(@"open", @"Open");    notification.timeZone = [NSTimeZone defaultTimeZone];    notification.repeatInterval = 0;    if([[RKUserDefaults standardUserDefaults] boolForKey:VoiceKey]){        notification.soundName= UILocalNotificationDefaultSoundName;    }    //发送通知    [[UIApplication sharedApplication] scheduleLocalNotification:notification];}

0 0
原创粉丝点击