(十三)即时通讯之XMPPFramework本地通知

前言

这也是本人第一次琢磨关于即时通讯方面的内容,结合网上查看的相关资料搭建出来的仿微信小demo,如有意见请多多指教

具体项目可以在githubWeiChat下载进行查看,如若各位看官老爷觉得还可以请点star

续前篇(十)即时通讯之XMPPFramework登陆注册

续前篇(十一)即时通讯之XMPPFramework电子名片

续前篇(十二)即时通讯之XMPPFramework花名册

续前篇(十三)即时通讯之XMPPFramework文字语音图片聊天

服务器地址
**Attention,这里需要改成自己的服务器地址和端口号
并且数据库和服务器一定要开启哟,不然没法登陆的哟**
#ifdef DEBUG
#define kHostName @"192.168.199.111"
//#define kHostName @"127.0.0.1"
#define kHostPort 5222
#else
#define kHostNanme @""
#define kHostPort 5222
#endif

本地通知

**本地通知**
****
**入口方法中调用以下代码**
- (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];
        // 设置通知内容
        localNote.alertBody = [NSString stringWithFormat:@"%@\n%@",message.fromStr,message.body];
        // 设置时间
        localNote.fireDate = [NSDate date];
        // 设置声音
        localNote.soundName = @"default";
        // 执行
        [[UIApplication sharedApplication] scheduleLocalNotification:localNote];
    } else {
        // 什么也不做
    }
}

你可能感兴趣的:((十三)即时通讯之XMPPFramework本地通知)