iOS APNs消息推送一直是各APP打造的一个亮点,用于把一些实时,重要的消息及时推送给客户。但是现实开发时,APNs服务器并不保证消息能及时送达APP端,这其中由网络原因,苹果APNs本身也会有一定的算法(猜测),iOS系统的的一些限制(iOS8之前不包含iOS8实际推送内容大小为256B,iOS8.0之后iOS9.0之前推送内容大小为2K,iOS9.0及以后推送内容为4K)等诸多原因,导致消息推送触达率不高。
在基于IM的聊天的APP中,如果APP在打开状态时,且APP的socket连接处于连接状态时,可以通过socket下发消息不走APNs提高消息送达率。消息到达APP端之后再以本地通知的方式,弹窗通知用户,消息到达APP时发送本地通知的代码如下:
if (UIApplicationStateBackground == SBApplication.applicationState){
UILocalNotification *notification = [UILocalNotification new];
// NSDate *now = [self curTimeZoneDate];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0];
notification.timeZone = [NSTimeZone defaultTimeZone];
NSString *content = @"content";
notification.alertBody = @"body";
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
NSString *appName = [infoDictionary objectForKey:@"CFBundleDisplayName"];
//这个是 8.2 的属性 我们的app 是支持 8.0
if ([notification respondsToSelector:@selector(setAlertTitle:)]) {
notification.alertTitle = appName;
}
notification.userInfo = [self constructLocalNotificationWithAlert:content dataItemDetail:detail];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
iOS本地通知最多能展示64条。