标签:
环境基于XMPP模块练习。
在ios8以前,不包括ios8,socket 是不支持后台运行。且在ios7要做配置info.plist文件。

添加 Required background modes = voip; //使用soket在后台运行
// 若想要IOS7支持后台socket连接必须设置以下参数,并且在info。plist中设置Required background modes为VOIP模式 _xmppStream.enableBackgroundingOnSocket = YES;
首先在XMPP的接收消息的代理方法中声明本地通知的对象
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{
// XMPPMessage *msg = message
NSString *str = [message body];
//判断是否为后台
if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive )
{
NSLog(@"后台");
// 创建本地通知
UILocalNotification *locati = [[UILocalNotification alloc ]init];
// 提醒内容信息
locati.alertBody = [NSString stringWithFormat:@"%@\n%@",message.fromStr,message.body];
// 提醒声音
locati.soundName = @"default";
locati.fireDate = [NSDate date];
// 执行本地通知
[[UIApplication sharedApplication] scheduleLocalNotification:locati];
//将其添加到本地通知
}
}
另外还需要注册通知,可以放在AppDelegate中注册,当程序加载完毕注册本地通知
/**
*
UIUserNotificationTypeBadge = 1 << 0,
UIUserNotificationTypeSound = 1 << 1,
UIUserNotificationTypeAlert = 1 << 2,
设置通知类型声音,角标,提醒alert
*/
if ([UIDevice currentDevice].systemVersion.doubleValue > 8.0) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];
[application registerUserNotificationSettings:settings];
}
标签:
原文地址:http://my.oschina.net/wupengnash/blog/421170