環信app的推送功能實現
今天 花了一天的時間終於將推送給弄好了,才發現其實很簡單,就是因為自己的幾個方法木有實現才導致花了那麼多的冤枉時間
推送的步驟如下
1.先建立一個apns推送證書,請參照:http://developer.easemob.com/docs/emchat/ios/push/certificate.html
建立證書後將apns證書打包成p12檔案,然後上傳到環信的後臺
2.在建立開發者證書和描述檔案,這個證書跟上面的apns證書是不同的,這個證書是用來真機除錯的,appid要有推送功能
3.將環信的appkey和證書寫入工程中,程式碼如下
//註冊 APNS檔案的名字, 需要與後臺上傳證書時的名字一一對應
NSString
[[EaseMobsharedInstance] registerSDKWithAppKey:@“4dasfdas”apnsCertName:apnsCertName];
[[EaseMobsharedInstance] enableBackgroundReceiveMessage];
[[EaseMobsharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
4.實現收到訊息的幾個回撥方法就可以了
// 收到訊息回撥
-(void
#if !TARGET_IPHONE_SIMULATOR
[selfplaySoundAndVibration];
BOOL isAppActivity = [[UIApplicationsharedApplication] applicationState] == UIApplicationStateActive;
if (!isAppActivity) {
[selfshowNotificationWithMessage:message];
}
#endif
}
- (void
//如果距離上次響鈴和震動時間太短, 則跳過響鈴
NSLog(@"%@, %@", [NSDatedate], self.lastPlaySoundDate);
NSTimeInterval timeInterval = [[NSDate date]
timeIntervalSinceDate:self.lastPlaySoundDate];
if (timeInterval < kDefaultPlaySoundInterval) {
return;
}
//儲存最後一次響鈴時間
self.lastPlaySoundDate = [NSDatedate];
// 收到訊息時,播放音訊
[[EaseMobsharedInstance].deviceManagerasyncPlayNewMessageSound];
// 收到訊息時,震動
[[EaseMobsharedInstance].deviceManagerasyncPlayVibration];
}
- (void)showNotificationWithMessage:(EMMessage *)message
{
EMPushNotificationOptions *options = [[EaseMobsharedInstance].chatManagerpushNotificationOptions];
//傳送本地推送
UILocalNotification *notification = [[UILocalNotificationalloc] init];
notification.fireDate = [NSDate date]; //觸發通知的時間
if (options.displayStyle == ePushNotificationDisplayStyle_messageSummary) {
id<IEMMessageBody> messageBody = [message.messageBodies firstObject];
NSString *messageStr = nil;
switch (messageBody.messageBodyType) {
caseeMessageBodyType_Text:
{
messageStr = ((EMTextMessageBody *)messageBody).text;
}
break;
caseeMessageBodyType_Image:
{
messageStr = @"[圖片]";
}
break;
caseeMessageBodyType_Location:
{
messageStr = @"[位置]";
}
break;
caseeMessageBodyType_Voice:
{
messageStr = @"[音訊]";
}
break;
caseeMessageBodyType_Video:{
messageStr = @"[視訊]";
}
break;
default:
break;
}
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];
}
else{
notification.alertBody = @"您有一條新訊息";
}
#warning 去掉註釋會顯示[本地]開頭, 方便在開發中區分是否為本地推送
//notification.alertBody = [[NSString alloc] initWithFormat:@"[本地]%@", notification.alertBody];
notification.alertAction = @"開啟";
notification.timeZone = [NSTimeZone defaultTimeZone];
//傳送通知
[[UIApplicationsharedApplication] scheduleLocalNotification:notification];
UIApplication *application = [UIApplicationsharedApplication];
application.applicationIconBadgeNumber += 1;
}