原生通知

框架 UserNotifications

推送配置:

  1. UNUserNotificationCenter注册推送服务
  2. 通过UIApplicationDelegate获取token
  3. 将token转字符串并Alias上传到推送服务器

响应推送:

  1. APP由推送启动:didFinishLaunchingWithOptions{launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]}
  2. 前台时收到推送iOS 10:UNUserNotificationCenterDelegate -didReceiveNotificationResponse 响应通知(response.actionIdentifier包括通过通知启动、选择通知、遗弃通知)。
  3. 前台时收到推送iOS10以前:didReceiveRemoteNotification,要执行handle

通知的类型:

  • 远程通知:UNPushNotificationTrigger
  • 本地通知(指定多久时间后执行):UNTimeIntervalNotificationTrigger
  • 本地通知(指定某一个时刻通知):UNCalendarNotificationTrigger
  • 本地通知(UNLocationNotificationTrigger):电子围栏通知
// 导入头文件
#import 
// 实现协议


// 1.注册服务
Appdelegate didFinishLaunchingWithOptions {
    UNUserNotificationCenter -> requestAuthorizationWithOptions(iOS 10之后有不同的用法)
}

// 2.获取(NSData*)device token,并上传推送服务器
/*
 用deviceToken注册后台远程服务,一般还要加个别名Alias
 */
// 获得Device Token
 - (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {}
// 获得Device Token失败
- (void)application:(UIApplication *)application
didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {}
{
    //解析NSData获取字符串
    NSString *deviceString;
    //Xcode11打的包,iOS13获取Token有变化
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 13) {
        if (![deviceToken isKindOfClass:[NSData class]]) {
            //记录获取token失败的描述
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"SGSNetworkAPNSDeviceToken"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            return;
        }
        const unsigned *tokenBytes = (const unsigned *)[deviceToken bytes];
        NSString *strToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
                              ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
                              ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
                              ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
        deviceString = strToken;
        NSLog(@"deviceToken1:%@", strToken);
    } else {
        NSString *token = [NSString
                       stringWithFormat:@"%@",deviceToken];
        token = [token stringByReplacingOccurrencesOfString:@"<" withString:@""];
        token = [token stringByReplacingOccurrencesOfString:@">" withString:@""];
        token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
        deviceString = token;
        NSLog(@"deviceToken2 is: %@", token);
    }
}

// 3. 处理通知 
- willPresentNotification {
    // 处于前台时
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        // UNPushNotificationTrigger = 远程通知
        // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以设置
        // 如果不实现,将会忽略这个消息
        completionHandler(UNNotificationPresentationOptionBadge);
    } else {
        // 本地通知有三种,按时间(多久之后、固定某一时刻)、按电子围栏 UNTimeIntervalNotificationTrigger UNCalendarNotificationTrigger 、 UNLocationNotificationTrigger
        completionHandler(UNNotificationPresentationOptionBadge|
                          UNNotificationPresentationOptionSound|
                          UNNotificationPresentationOptionAlert);
    }
}
// 4. 点击通知
- didReceiveNotificationResponse {
    // 处理通知的点击
}

你可能感兴趣的:(原生通知)