Objective-C 推送通知

系统 iOS 10.x
本文简单记录远程通知,更多细节看心情整理


1.注册通知

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    if (iOS系统大于10.0) {
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
            if (granted) {
                NSLog(@"register success");
            } else {
                NSLog(@"register failure");
            }
        }];
    } else {
        UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:setting];

    }
    [[UIApplication sharedApplication] registerForRemoteNotifications];

    return YES;
}

2.处理token

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    // 自定义处理token
    NSLog(@"%@",deviceToken);
}

3.接收远程通知

/**
 iOS3.0 ~ 10.0

 前台状态下,当接收到远程通知
 点击通知
 完全退出情况下,点击通知,不会执行
 */
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

    double version = [[[UIDevice currentDevice] systemVersion] doubleValue];
    if (version < 10.0) {
        NSLog(@"收到远程通知");
        self.window.rootViewController.view.backgroundColor = [UIColor redColor];
    }
}

/*
 iOS7.0 ~ 10.0

 如果实现了该方法,则不执行application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

 前台状态下,当接收到远程通知
 点击通知
 完全退出情况下,点击通知,也会执行

 只要接收到了通知,不管当前APP的状态,不管后台,还是锁屏,都直接执行这个方法
 要求:
 1.必须勾选后台模式Remote Notification
 2.告诉系统是否有新的内容更新(执行完成代码块)
 3.设置发送通知的格式("content-available":"xxx")
 {
 "aps" : {
 "alert":"This is some fancy message.",
 "badge":1,
 "content-available":"xxx"
 }
 }

 调用系统回调代码块的作用
 1.系统会估量App消耗的电量,并根据传递的UIBackgroundFetchResult参数记录新数据是否可用
 2.调用完成的处理代码时,应用的界面缩略图会自动更新

 */
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

    double version = [[[UIDevice currentDevice] systemVersion] doubleValue];
    if (version < 10.0) {
        NSLog(@"收到远程通知-----2");
        self.window.rootViewController.view.backgroundColor = [UIColor blueColor];
    }

    completionHandler(UIBackgroundFetchResultNewData);
}

/*
 前台状态下
 完全退出,点击通知,不会执行这个方法
 */
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {

    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        NSLog(@"收到远程通知-----3.remote");
        self.window.rootViewController.view.backgroundColor = [UIColor yellowColor];
    }
    else {
        NSLog(@"收到本地通知-----3.local");
        self.window.rootViewController.view.backgroundColor = [UIColor orangeColor];
    }

    // UNNotificationPresentationOptionAlert 在前台状态下会出现通知栏
    completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionAlert);
}

/*
 点击通知
 完全退出,点击通知,也会执行这个方法
 */
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler {

    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        NSLog(@"收到远程通知-----4.remote");
        self.window.rootViewController.view.backgroundColor = [UIColor purpleColor];
    }
    else {
        NSLog(@"收到本地通知-----4.local");
        self.window.rootViewController.view.backgroundColor = [UIColor greenColor];
    }
    completionHandler();
}

你可能感兴趣的:(objective-c学习笔记)