iOS(本地通知与远程通知)
Posted 探出的头
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS(本地通知与远程通知)相关的知识,希望对你有一定的参考价值。
ios 推送通知有两种:本地推送、远程推送.
本地推送 : 在不需要联网的情况下,由APP发出推送,常用于某一时刻的通知,如闹钟。本地通送有局限性在于当APP处于后台或者退出时就无法发出通知。
远程推送: APNs和第三方推送,第三方推送最终也需要APNs转发,
本地推送实现
注册通知:
float sysVer = [[UIDevice currentDevice].systemVersion floatValue]; if (sysVer < 10) { //设置通知类型 弹框、脚标、声音 UIUserNotificationSettings* setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:setting]; }else{ UNUserNotificationCenter* center =[UNUserNotificationCenter currentNotificationCenter]; [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted) { NSLog(@"通知注册成功"); } }]; }
发送通知:
float sysVer = [[UIDevice currentDevice].systemVersion floatValue]; if(sysVer < 10){ UILocalNotification* local = [[UILocalNotification alloc] init]; //给这些属性赋值才能让通知有特定的内容 local.alertBody=@"航海王:One Piece"; //特定的时间让显示出来() NSString* dateStr = @"2016-12-01 16:30:00"; NSDateFormatter* dateFormatter = [[NSDateFormatter alloc]init]; dateFormatter.dateFormat = @"yyyy-MM-dd hh:mm:ss"; local.fireDate=[NSDate dateWithTimeIntervalSinceNow:60]; //滑动解锁的文字(在推送通知信息的下面一小行字) local.alertAction =@" "; //有声音给声音,没声音用默认的 local.soundName=@"UILocalNotificationDefaultSoundName"; //设置图标右上角数字 local.applicationIconBadgeNumber=1; //用户信息 local.userInfo=@{@"name":@"航海王",@"content":@"One Piece",@"time":@"20161201"}; //3:定制一个通知 [[UIApplication sharedApplication]scheduleLocalNotification:local]; }else{ UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init]; content.title = [NSString localizedUserNotificationStringForKey:@"Hello Title" arguments:nil]; content.body = [NSString localizedUserNotificationStringForKey:@"Hello Body" arguments:nil]; content.sound = [UNNotificationSound defaultSound]; //设定通知时间 UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO]; UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"notificationIdentifier" content:content trigger:trigger]; UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter]; [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { }]; }
远程推送实现
服务器向客户端发送推送的过程:
每个请求中都有设备令牌,APNs通过设备令牌去识别设备和APP,每次运行APP,都会向服务器发送设备令牌,服务器再使用设备令牌发送推送,只有当推送设置改变时设备令牌才会被改变 ,只有APNs 可以解码设备令牌.
服务器与APNS 连接过程:
头晕,这个还是看官方的文档:https://developer.apple.com/library/prerelease/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/APNSOverview.html#//apple_ref/doc/uid/TP40008194-CH8-SW1
信鸽 使用流程:
1、配置真机调试证书, 推送测试证书 (度娘·······)
2、注册iOS推送
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 }
3、注册设备信息(deviceToken)
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { //注册设备 NSString * deviceTokenStr = [XGPush registerDevice: deviceToken]; //打印获取的deviceToken的字符串 NSLog(@"deviceTokenStr is %@",deviceTokenStr); }
3、注册信鸽
[XGPush startApp:@“10086” appKey:@"key"];
//设置账户[XGPush setAccount:@
"123456"
];
//推送统计- (
void
)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
[XGPush handleReceiveNotification:userInfo];
}
以上是关于iOS(本地通知与远程通知)的主要内容,如果未能解决你的问题,请参考以下文章