標籤:
原文: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;}
這樣就可以進行訊息推送了
iOS: 環信的推送