日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS: 环信的推送

發布時間:2025/3/15 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS: 环信的推送 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原文:http://m.blog.csdn.net/article/details?id=38824551

1.先創建一個apns證書,鏈接如下

  http://developer.easemob.com/docs/emchat/ios/push/certificate.html

  創建完證書后,將證書弄成p12文件,然后上傳到環信后臺

2.再創建真機調試證書,和描述文件,保證能進行真機調試。并且appid要又推送功能

3.綁定環信證書和appkey

//注冊 APNS文件的名字, 需要與后臺上傳證書時的名字一一對應 NSString *apnsCertName = @"chatdemo"; [[EaseMob sharedInstance] registerSDKWithAppKey:@“4545521” apnsCertName:apnsCertName]; [[EaseMob sharedInstance] enableBackgroundReceiveMessage]; [[EaseMob sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions]; 4.然后實現環信的幾個方法 // 收到消息回調 -(void)didReceiveMessage:(EMMessage *)message{#if !TARGET_IPHONE_SIMULATOR[self playSoundAndVibration];BOOL isAppActivity = [[UIApplication sharedApplication] applicationState] == UIApplicationStateActive;if (!isAppActivity) {[self showNotificationWithMessage:message];} #endif }- (void)playSoundAndVibration{//如果距離上次響鈴和震動時間太短, 則跳過響鈴NSLog(@"%@, %@", [NSDate date], self.lastPlaySoundDate);NSTimeInterval timeInterval = [[NSDate date]timeIntervalSinceDate:self.lastPlaySoundDate];if (timeInterval < kDefaultPlaySoundInterval) {return;}//保存最后一次響鈴時間self.lastPlaySoundDate = [NSDate date];// 收到消息時,播放音頻 [[EaseMob sharedInstance].deviceManager asyncPlayNewMessageSound];// 收到消息時,震動 [[EaseMob sharedInstance].deviceManager asyncPlayVibration]; }- (void)showNotificationWithMessage:(EMMessage *)message{id<IEMMessageBody> messageBody = [message.messageBodies firstObject];NSString *messageStr = nil;switch (messageBody.messageBodyType) {case eMessageBodyType_Text:{messageStr = ((EMTextMessageBody *)messageBody).text;}break;case eMessageBodyType_Image:{messageStr = @"[圖片]";}break;case eMessageBodyType_Location:{messageStr = @"[位置]";}break;case eMessageBodyType_Voice:{messageStr = @"[音頻]";}break;case eMessageBodyType_Video:{messageStr = @"[視頻]";}break;default:break;}//發送本地推送UILocalNotification *notification = [[UILocalNotification alloc] init];notification.fireDate = [NSDate date]; //觸發通知的時間 NSString *title = message.from;if (message.isGroup) {NSArray *groupArray = [[EaseMob sharedInstance].chatManager groupList];for (EMGroup *group in groupArray) {if ([group.groupId isEqualToString:message.conversation.chatter]) {title = [NSString stringWithFormat:@"%@(%@)", message.groupSenderName, group.groupSubject];break;}}}notification.alertBody = [NSString stringWithFormat:@"%@:%@", title, messageStr];notification.alertAction = @"打開";notification.timeZone = [NSTimeZone defaultTimeZone];notification.soundName = UILocalNotificationDefaultSoundName;//發送通知 [[UIApplication sharedApplication] scheduleLocalNotification:notification];UIApplication *application = [UIApplication sharedApplication];application.applicationIconBadgeNumber += 1; }
這樣就可以進行消息推送了

?

經過自己的研究,個人拓展一下:一般接收消息推送通知放在主控制器去操作,只需要在主控制器注冊代理,即可回調,發送通知

//**************************************************遠程通知部分*******************************************************//

//兩次提示的默認間隔

#import "MainViewController.h"

static const CGFloat kDefaultPlaySoundInterval = 3.0; ?

static NSString *kMessageType = @"MessageType";

static NSString *kConversationChatter = @"ConversationChatter";

@interface MainViewController ()<EMChatManagerChatDelegate>

@property (strong, nonatomic) NSDate *lastPlaySoundDate;

@end

?

#pragma - 遠程通知部分

- (BOOL)needShowNotification:(NSString *)fromChatter {BOOL ret = YES;NSArray *igGroupIds = [[EaseMob sharedInstance].chatManager ignoredGroupIds];for (NSString *str in igGroupIds) {if ([str isEqualToString:fromChatter]) {ret = NO;break;}}return ret; }// 收到消息回調 -(void)didReceiveMessage:(EMMessage *)message {BOOL needShowNotification = (message.messageType != eMessageTypeChat) ? [self needShowNotification:message.conversationChatter] : YES;if (needShowNotification) { #if !TARGET_IPHONE_SIMULATORUIApplicationState state = [[UIApplication sharedApplication] applicationState];switch (state) {case UIApplicationStateActive:[self playSoundAndVibration];break;case UIApplicationStateInactive:[self playSoundAndVibration];break;case UIApplicationStateBackground:[self showNotificationWithMessage:message];break;default:break;} #endif} }
//透傳消息
-(void)didReceiveCmdMessage:(EMMessage *)message {[self showHint:NSLocalizedString(@"receiveCmd", @"receive cmd message")]; }- (void)playSoundAndVibration{NSTimeInterval timeInterval = [[NSDate date]timeIntervalSinceDate:self.lastPlaySoundDate];if (timeInterval < kDefaultPlaySoundInterval) {//如果距離上次響鈴和震動時間太短, 則跳過響鈴NSLog(@"skip ringing & vibration %@, %@", [NSDate date], self.lastPlaySoundDate);return;}//保存最后一次響鈴時間self.lastPlaySoundDate = [NSDate date];// 收到消息時,播放音頻 [[EMCDDeviceManager sharedInstance] playNewMessageSound];// 收到消息時,震動 [[EMCDDeviceManager sharedInstance] playVibration]; }- (void)showNotificationWithMessage:(EMMessage *)message {EMPushNotificationOptions *options = [[EaseMob sharedInstance].chatManager pushNotificationOptions];//發送本地推送UILocalNotification *notification = [[UILocalNotification alloc] init];notification.fireDate = [NSDate date]; //觸發通知的時間if (options.displayStyle == ePushNotificationDisplayStyle_messageSummary) {id<IEMMessageBody> messageBody = [message.messageBodies firstObject];NSString *messageStr = nil;switch (messageBody.messageBodyType) {case eMessageBodyType_Text:{messageStr = ((EMTextMessageBody *)messageBody).text;}break;case eMessageBodyType_Image:{messageStr = NSLocalizedString(@"message.image", @"Image");}break;case eMessageBodyType_Location:{messageStr = NSLocalizedString(@"message.location", @"Location");}break;case eMessageBodyType_Voice:{messageStr = NSLocalizedString(@"message.voice", @"Voice");}break;case eMessageBodyType_Video:{messageStr = NSLocalizedString(@"message.video", @"Video");}break;default:break;}// NSString *title = [[UserProfileManager sharedInstance] getNickNameWithUsername:message.from];NSString *title = message.from;if (message.messageType == eMessageTypeGroupChat) { // NSArray *groupArray = [[EaseMob sharedInstance].chatManager groupList]; // for (EMGroup *group in groupArray) { // if ([group.groupId isEqualToString:message.conversationChatter]) { // title = [NSString stringWithFormat:@"%@(%@)", message.groupSenderName, group.groupSubject]; // break; // } // } }else if (message.messageType == eMessageTypeChatRoom){ // NSUserDefaults *ud = [NSUserDefaults standardUserDefaults]; // NSString *key = [NSString stringWithFormat:@"OnceJoinedChatrooms_%@", [[[EaseMob sharedInstance].chatManager loginInfo] objectForKey:@"username" ]]; // NSMutableDictionary *chatrooms = [NSMutableDictionary dictionaryWithDictionary:[ud objectForKey:key]]; // NSString *chatroomName = [chatrooms objectForKey:message.conversationChatter]; // if (chatroomName) // { // title = [NSString stringWithFormat:@"%@(%@)", message.groupSenderName, chatroomName]; // } }notification.alertBody = [NSString stringWithFormat:@"%@:%@", title, messageStr];}else{notification.alertBody = NSLocalizedString(@"receiveMessage", @"you have a new message");}#warning 去掉注釋會顯示[本地]開頭, 方便在開發中區分是否為本地推送 // notification.alertBody = [[NSString alloc] initWithFormat:@"[本地]%@", notification.alertBody]; notification.alertAction = NSLocalizedString(@"open", @"Open");notification.timeZone = [NSTimeZone defaultTimeZone];NSTimeInterval timeInterval = [[NSDate date] timeIntervalSinceDate:self.lastPlaySoundDate];if (timeInterval < kDefaultPlaySoundInterval) {NSLog(@"skip ringing & vibration %@, %@", [NSDate date], self.lastPlaySoundDate);} else {notification.soundName = UILocalNotificationDefaultSoundName;self.lastPlaySoundDate = [NSDate date];}NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];[userInfo setObject:[NSNumber numberWithInt:message.messageType] forKey:kMessageType];[userInfo setObject:message.conversationChatter forKey:kConversationChatter];notification.userInfo = userInfo;//發送通知 [[UIApplication sharedApplication] scheduleLocalNotification:notification];UIApplication *application = [UIApplication sharedApplication];application.applicationIconBadgeNumber += 1; }

?

轉載于:https://www.cnblogs.com/XYQ-208910/p/5447751.html

總結

以上是生活随笔為你收集整理的iOS: 环信的推送的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。