iOS8 注册推送
来源:互联网 发布:淘宝网充话费怎样开店 编辑:程序博客网 时间:2024/05/16 12:33
一直更新了iOS8,但是一直没有开始研究这个iOS8,今天因为项目用到了推送,于是体验了iOS8的推送,先讲讲这个推送。目前分为四个推送:用户推送,本地推送,远程推送,地理位置推送。
用户推送
我们先开始讲这个用户推送,我们要使用之前必须先注册这个推送,用户要允许这个程序进行推送
注册过程:
- if (IS_IOS8) {
- //1.创建消息上面要添加的动作(按钮的形式显示出来)
- UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
- action.identifier = @"action";//按钮的标示
- action.title=@"Accept";//按钮的标题
- action.activationMode = UIUserNotificationActivationModeForeground;//当点击的时候启动程序
- // action.authenticationRequired = YES;
- // action.destructive = YES;
- UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];
- action2.identifier = @"action2";
- action2.title=@"Reject";
- action2.activationMode = UIUserNotificationActivationModeBackground;//当点击的时候不启动程序,在后台处理
- action.authenticationRequired = YES;//需要解锁才能处理,如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略;
- action.destructive = YES;
- //2.创建动作(按钮)的类别集合
- UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
- categorys.identifier = @"alert";//这组动作的唯一标示,推送通知的时候也是根据这个来区分
- [categorys setActions:@[action,action2] forContext:(UIUserNotificationActionContextMinimal)];
- //3.创建UIUserNotificationSettings,并设置消息的显示类类型
- UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIRemoteNotificationTypeSound) categories:[NSSet setWithObjects:categorys, nil nil]];
- [application registerUserNotificationSettings:notiSettings];
- }else{
- [application registerForRemoteNotificationTypes: UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
- }
- - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
- {
- // UIUserNotificationSettings *settings = [application currentUserNotificationSettings];
- // UIUserNotificationType types = [settings types];
- // //只有5跟7的时候包含了 UIUserNotificationTypeBadge
- // if (types == 5 || types == 7) {
- // application.applicationIconBadgeNumber = 0;
- // }
- //注册远程通知
- [application registerForRemoteNotifications];
- }
我们如何能进行这些行为,首先我们需注册这些行为。
- UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init];
- acceptAction.identifier = @"RickAction";
- acceptAction.title = @"Accept";
- acceptAction.activationMode = UIUserNotificationActivationModeBackground;
- acceptAction.destructive = NO;
- acceptAction.authenticationRequired = NO;
- UIMutableUserNotificationCategory *inviteCategory = [[UIMutableUserNotificationCategory alloc] init];
- inviteCategory.identifier = @"INVITE_CATEGORY";
- [inviteCategory setActions:@[acceptAction] forContext:UIUserNotificationActionContextDefault];
我们需要注意这个UIUserNotificationActionContextDefault,如果我们使用这个,我们会得到这个推送行为,Maybe和Accept
我们还可以使用UIUserNotificationActionContextMinimal得到的是Decline和Accept行为
在这些行为注册之后,我们加上之前提到的推送设置就完成了注册推送的这个流程了
- NSSet *categories = [NSSet setWithObjects:inviteCategory, nil nil];
- UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert ;
- UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:categories];
- [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
远程推送
远程推送,所有消息大小不超过2KB,我们获取远程推送的json格式的消息,解析这个消息就是我们的远程推送了:
- {
- “aps”: {
- "content-available": 1,
- "alert": "This is the alert text",
- "badge": 1,
- "sound": "default"
- }
- }
若要使用远程推送,满足两个条件:一、用户需要调用注册用户推送registerUserNotificationSettings;二、在info.plist文件中UIBackgroundModes必须包含远程通知。
- <span style="font-family: Helvetica, Arial, Geneva, sans-serif;">[[UIApplication sharedApplication] registerForRemoteNotifications];</span>
- - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
- NSString *token=[NSString stringWithFormat:@"%@",deviceToken];
- token=[token stringByReplacingOccurrencesOfString:@"<" withString:@""];
- token=[token stringByReplacingOccurrencesOfString:@">" withString:@""];
- token=[token stringByReplacingOccurrencesOfString:@" " withString:@""];
- }
- - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
- }
iOS7通知代理方法
后来又增加了本地通知的代理方法
iOS8的推送代理方法只有两个了
- - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
- {
- }
- - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler
- {
- }
- - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
- {
- if ([identifier isEqualToString:@"RickAction"]) {
- [self handleAcceptActionWithNotification:notification];
- }
- completionHandler();
- }
- - (void)handleAcceptActionWithNotification:(UILocalNotification*)notification
- {
- }
地理位置推送
这个推送是新的API才有的特性,必须配合CLLocation定位一起使用。
- //Location Notification
- CLLocationManager *locMan = [[CLLocationManager alloc] init];
- locMan.delegate = self;
- [locMan requestWhenInUseAuthorization];
- #pragma mark - CLLocationManager
- - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
- {
- BOOL canUseLocationNotifications = (status == kCLAuthorizationStatusAuthorizedWhenInUse);
- if (canUseLocationNotifications) {
- [self startShowLocationNotification];
- }
- }
- - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
- {
- CLRegion *region = notification.region;
- if (region) {
- }
- }
- - (void)startShowLocationNotification
- {
- CLLocationCoordinate2D local2D ;
- local2D.latitude = 123.0;
- local2D.longitude = 223.0;
- UILocalNotification *locNotification = [[UILocalNotification alloc] init];
- locNotification.alertBody = @"你接收到了";
- locNotification.regionTriggersOnce = YES;
- locNotification.region = [[CLCircularRegion alloc] initWithCenter:local2D radius:45 identifier:@"local-identity"];
- [[UIApplication sharedApplication] scheduleLocalNotification:locNotification];
- }
如果没有开启Core Location 那么上面的didReceiveLocalNotification不会被调用
最后再总结一下,整个推送流程我觉得是这样子的,先注册推送,然后推送消息,客户端接收推送消息,执行推送行为。如果有错误,还请在文章下面评论,欢迎指正。
这个博客写的也不错:http://blog.csdn.net/yujianxiang666/article/details/35260135
0 0
- iOS8 注册推送
- iOS8注册推送
- ios8注册推送通知
- IOS8注册推送消息
- iOS开发----iOS8 注册推送
- iOS8推送消息、注册通知
- ios8 注册本地推送通知
- 【IOS】ios8推送消息注册
- 【IOS】IOS8推送消息注册 通知
- IOS开发之 ---- IOS8推送消息注册
- iOS8.0推送通知的注册方法
- iOS8注册推送失败的探究
- IOS开发之 ---- IOS8推送消息注册
- ios7与ios8并存,注册推送通知
- ios8 远程推送的注册代码
- ios8 推送
- iOS8推送
- ios8推送问题
- linux 环境变量
- phpcms如何获取当前文章的地址或url,以及解决方法。
- xcode armv6 armv7 armv7s arm64
- android 开发实例 底部导航栏(1)
- 乔布斯:这九本书每个人都该读一读
- iOS8 注册推送
- 关于Android 4.0 以上Service被kill后的生命周期 以及onTaskRemoved和OnTrimMemory调用
- CruiseControl初探
- linux下自启动服务的管理及chkconfig的应用(ubuntu 和 RedHat为例)
- am335x开发板的疑问以及解答
- 事务的使用
- 分配任务,汇报进度,以及日常会议事项
- 判断是否为日期类型
- 5.1 什么是值?