前言
這也是本人第一次琢磨關(guān)于即時通訊方面的內(nèi)容,結(jié)合網(wǎng)上查看的相關(guān)資料搭建出來的仿微信小demo,如有意見請多多指教
具體項目可以在githubWeiChat下載進行查看,如若各位看官老爺覺得還可以請點star
續(xù)前篇(十)即時通訊之XMPPFramework登陸注冊
續(xù)前篇(十一)即時通訊之XMPPFramework電子名片
續(xù)前篇(十二)即時通訊之XMPPFramework花名冊
續(xù)前篇(十三)即時通訊之XMPPFramework文字語音圖片聊天
**Attention,這里需要改成自己的服務(wù)器地址和端口號
并且數(shù)據(jù)庫和服務(wù)器一定要開啟喲,不然沒法登陸的喲**
#ifdef DEBUG
#define kHostName @"192.168.199.111"
//#define kHostName @"127.0.0.1"
#define kHostPort 5222
#else
#define kHostNanme @""
#define kHostPort 5222
#endif
本地通知
**本地通知**
****
**入口方法中調(diào)用以下代碼**
- (void)registerPush{
float sysVer = [[[UIDevice currentDevice] systemVersion] floatValue];
if(sysVer < 8){
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
}else{
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_
UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
UIUserNotificationSettings *userSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert
categories:[NSSet setWithObject:categorys]];
[[UIApplication sharedApplication] registerUserNotificationSettings:userSettings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
#endif
}
}
****
**添加plist選項**
打開后臺模式,添加一個Voice over IP
****
**允許XMPPStream的Socket后臺運行**
_xmppStream.enableBackgroundingOnSocket = YES;
****
**XMPPManaget中添加一個代理方法**
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message {
NSLog(@"收到消息");
if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) {
NSLog(@"我在后臺收到消息");
[UIApplication sharedApplication].applicationIconBadgeNumber = 5;
// 本地通知
UILocalNotification *localNote = [[UILocalNotification alloc] init];
// 設(shè)置通知內(nèi)容
localNote.alertBody = [NSString stringWithFormat:@"%@\n%@",message.fromStr,message.body];
// 設(shè)置時間
localNote.fireDate = [NSDate date];
// 設(shè)置聲音
localNote.soundName = @"default";
// 執(zhí)行
[[UIApplication sharedApplication] scheduleLocalNotification:localNote];
} else {
// 什么也不做
}
}