iOS推送通知

注册推送

-(void)registerForRemoteNotification:(UIApplication *)application {
    
    if ([[UIDevice currentDevice].systemVersion doubleValue] >= 10.0) {
        //iOS10特有
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        // 必须写代理,不然无法监听通知的接收与点击
        center.delegate = self;
        [center setNotificationCategories:[self createNotificationCategoryActions]];
UNAuthorizationOptions authOptions =
        UNAuthorizationOptionAlert
        | UNAuthorizationOptionSound
        | UNAuthorizationOptionBadge;
        [center requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) {
            if (granted) {
                // 点击允许
                DLog(@"注册成功");
                
            } else {
                // 点击不允许
                DLog(@"注册失败");
            }
        }];
    }else if ([[UIDevice currentDevice].systemVersion doubleValue] >=8.0){
        //iOS 8-10
     
        NSMutableSet *categories = [NSMutableSet set];
        UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];
        category.identifier = @"identifier";
        UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
        action.identifier = @"refuseBtn";
        action.title = FSLocalizedString(@"reject");
        action.activationMode = UIUserNotificationActivationModeBackground;
        action.authenticationRequired = YES;
        UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init];
        action1.identifier = @"agreeBtn";
        action1.title = FSLocalizedString(@"agree");
        action1.activationMode = UIUserNotificationActivationModeBackground;
        action1.authenticationRequired = YES;
        //YES显示为红色,NO显示为蓝色
        action.destructive = NO;
        NSArray *actions = @[ action,action1];
        [category setActions:actions forContext:UIUserNotificationActionContextMinimal];
        [categories addObject:category];
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge categories:categories]];
    }
    // 注册获得device Token
   
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}

-(NSSet *)createNotificationCategoryActions{
    //定义按钮的交互button action
    UNNotificationAction * agreeBtn = [UNNotificationAction actionWithIdentifier:@"agreeBtn" title:FSLocalizedString(@"agree") options:UNNotificationActionOptionDestructive];
    UNNotificationAction * refuseBtn = [UNNotificationAction actionWithIdentifier:@"refuseBtn" title:FSLocalizedString(@"reject") options:UNNotificationActionOptionDestructive];
    //将这些action带入category
    UNNotificationCategory * choseCategory = [UNNotificationCategory categoryWithIdentifier:@"identifier" actions:@[agreeBtn,refuseBtn] intentIdentifiers:@[@"agreeBtn",@"refuseBtn"] options:UNNotificationCategoryOptionCustomDismissAction];
//    [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithArray:@[choseCategory]]];
    return [NSSet setWithObjects:choseCategory,nil];
}

注册token回调

//注册token成功
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSString *pushToken = [[[[_pushDeviceToken description] stringByReplacingOccurrencesOfString:@"<" withString:@""] stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""] ;
    DLog(@"%@",pushToken);
}

//处理token处理注册失败
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    ...
}

推送消息处理

//iOS10 app在前台时收到推送
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
    ...
    completionHandler();
}

//iOS10 app在后台时收到推送,点击打开app
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler{

    ...//按钮的事件也在这里处理
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    
    DLog(@"推送返回的信息是:%@",userInfo);
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    NSDictionary * dict  = [userInfo objectForKey:@"extension"];
 
    if ([response.actionIdentifier isEqualToString:@"agreeBtn"]) {
        DLog(@"同意按钮");
      
    }else if ([response.actionIdentifier isEqualToString:@"refuseBtn"]) {
        DLog(@"拒绝按钮");
     
    }else {
        UIViewController * vc  = [self getCurrentVC];
        PushNotificationUtil *util = [[PushNotificationUtil alloc]init];
        [util handleDidReceiveNotificationResponse:dict VC:vc];
    }
    
    if (completionHandler) {
        completionHandler();
    }
}

//iOS 8-10收到推送消息
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    //判断app是否在前台
    if (application.applicationState == UIApplicationStateActive) {
        
    }else {
        
    }
}

//iOS 8-10推送按钮点击
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo
  completionHandler:(void (^)())completionHandler {
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;

    NSDictionary * dict  = [userInfo objectForKey:@"extension"];
    if ([identifier isEqualToString:@"refuseBtn"]) {
        
        ...
        if (completionHandler) {
            completionHandler();
        }
    }else if ([identifier isEqualToString:@"agreeBtn"]) {
       ...
        if (completionHandler) {
            completionHandler();
        }
    }
}

你可能感兴趣的:(iOS推送通知)