iOS本地推送功能实现

1、首先在AppDelegate注册推送

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


  if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {

    //注册推送, iOS 8以上
    UIUserNotificationSettings *settings = [UIUserNotificationSettings

                                            settingsForTypes:(UIUserNotificationTypeBadge |

                                                              UIUserNotificationTypeSound |

                                                              UIUserNotificationTypeAlert)

                                            categories:nil];

    [application registerUserNotificationSettings:settings];
    [application registerForRemoteNotifications];

} else {
    //注册推送, 适配系统版本iOS 8以下的手机
    UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge |

    UIRemoteNotificationTypeAlert |

    UIRemoteNotificationTypeSound;

    [application registerForRemoteNotificationTypes:myTypes];

}


return YES;
}

2、注册成功与否的回调

1
2
3
4
5
6
7
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
              NSLog(@"device token is %@",deviceToken);
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
      NSLog(@"push fail error is %@",error);
}

3、获取本地通知数据回调

1
2
3
4
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
      NSLog(@"get notification");
      //获取通知后的操作
}

4、发起本地推送

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
-(void)sendLocalNotifation{
  UILocalNotification* notifation = [[UILocalNotification alloc]init];
  //当前时间5秒后
  NSDate* pushDate = [NSDate dateWithTimeIntervalSinceNow:5];
  if (notifation!=nil) {
      //设置推送时间
      notifation.fireDate = pushDate;
      //设置时区
      notifation.timeZone = [NSTimeZone defaultTimeZone];
      //设置重复次数
      notifation.repeatInterval = 0;
      notifation.soundName = UILocalNotificationDefaultSoundName;
      //通知弹窗的文本
      notifation.alertBody = @"推送的文本";
      //设置icon小红点
      notifation.applicationIconBadgeNumber = 1;
      //设置userinfo 方便在之后需要撤销的时候使用
      NSDictionary* info = [NSDictionary dictionaryWithObject:@"通知的名字" forKey:@"key"];
      notifation.userInfo = info;
      //添加推送到UIApplication
      UIApplication* app = [UIApplication sharedApplication];
      [app scheduleLocalNotification:notifation];
  }
}

5、解除本地推送

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-(void)cancleLocalNotifation{
  UIApplication* app = [UIApplication sharedApplication];
  //获取所有本地推送
  NSArray* localArray = [app scheduledLocalNotifications];
  UILocalNotification* notification;
  if (localArray) {
      for(UILocalNotification* noti in localArray){
          NSDictionary* userInfo = noti.userInfo;
          if (userInfo) {
            NSString *keyValue = [userInfo objectForKey:@"key"];
            if ([keyValue isEqualToString:@"通知的名字"]) {
                notification = noti;
                [app cancelLocalNotification:notification];
            }
        }
    }
}

Comments