iOS: 環信的推送

來源:互聯網
上載者:User

標籤:

原文: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: 環信的推送

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.