iOS 远程推送封装-优化 remote notification

iOS 远程推送封装-优化 remote notification_第1张图片
notification push

本文抛砖引玉,欢迎各位大佬指点!

  1. 大家在做项目的时候有没有优化过appDelegate,appDelegate主要是实现application的代理方法,SDK的初始化等,这样就会导致delegate代码量多,找函数 调试繁琐等问题。
  2. 当然SDK的初始化可以放到一个自定义的类,这个类专门做初始化使用。
  3. 那application的大量代理方法怎么办呢,接下来先来说说有关推送的一些优化。
先说下远程推送的大概流程
1、本地注册推送
2、appDelegate reveiveDeviceToken代理方法拿到token,交给自己的推送服务器或者三方推送的服务器
3、实现那几个通知到达、点击推送通知的代理方法(冷启动处理)
4、实现跳转+推送到达率的统计

以上就是远程推送的大概流程,比较简单,
接下来说下推送封装的思路,就两点,核心就是第二点,方法交换

  • 注册代码封装到自定义类内部
  • 实现自定义类的代理方法,其实是自定义类内部把有关系统推送的代理方法实现替换成自定义方法的实现

先来看下核心类有哪些东西:

@interface TTPushManager : NSObject

@property (nonatomic, weak) id  delegate;

/**
 * model name
 */
@property (nonatomic, assign, readonly) Class modelClass;

/**
 * 解析 key 默认为 @"extra"
 */
@property (nonatomic, copy, readonly) NSString *analysisKey;

/**
 * 根据 modelClass 解析后的model
 */
@property (nonatomic, strong, readonly) id analysisModel;

/**
 * deviceToken
 */
@property (nonatomic, copy, readonly) NSString *deviceToken;

/**
 * 根据pushMsgIdKey 解析出来的 value
 */
@property (nonatomic, copy, readonly) NSString *pushMsgId;

/**
 * 解析推送到达使用的key
 */
@property (nonatomic, copy, readonly) NSString *pushMsgIdKey;

+ (instancetype)shareInstance;

/**
 注册
 @param delagate 代理 不可为空
 */
- (void)registerRemoteNotification:(nonnull id)delagate;

/**
 @param modelClass 想要解析成哪种模型,modelClass
 @param analysisKey 解析model key
 @param pushIdKey 解析推送到达使用的key
 */
- (void)registerModelName:(nullable Class)modelClass analysisKey:(nullable NSString *)analysisKey pushMsgIdKey:(nullable NSString *)pushIdKey;

/**
 推送统计 iOS10以上版本 可在 push service extension h中使用
 @param msgID msgID
 @param token token
 */
+ (void)uploadPushMsgId:(NSString *)msgID token:(NSString *)token;

@end

注释写的很清楚了,主要是给外部暴露了注册远程通知接口,代理,还有payload内部自定义数据的解析key.

再来看看.m文件:

1、初始化 给默认值
+ (instancetype)shareInstance {
    static TTPushManager *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[[self class] alloc] init];
        instance.analysisKey = @"extra";//解析自己业务数据用到的key
        instance.pushMsgIdKey = @"msgId";//解析推送到达率使用的key
    });
    return instance;
}

2、传递要被解析的model 解析key,推送到达率解析key
- (void)registerModelName:(nullable Class)modelClass analysisKey:(nullable NSString *)analysisKey pushMsgIdKey:(nullable NSString *)pushIdKey {
    if(analysisKey.length > 0) {
        [TTPushManager shareInstance].analysisKey = analysisKey;
    }
    if (modelClass) {
        [TTPushManager shareInstance].modelClass = modelClass;
    }
    if (pushIdKey.length > 0) {
        [TTPushManager shareInstance].pushMsgIdKey = pushIdKey;
    }
}
3、注册远程通知
- (void)registerRemoteNotification:(id)delagate  API_AVAILABLE(ios(10.0)) {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [self exchangeMethod:delagate];
    });
    
    UIApplication *application = [UIApplication sharedApplication];
    CGFloat sysVersion = [[UIDevice currentDevice].systemVersion floatValue];
    if (sysVersion >= 10.0) {
        //iOS10特有
        if (@available(iOS 10.0, *)) {
            UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
            center.delegate = delagate;
            [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {
                if (granted) {
                    NSLog(@"remote notification 注册成功");
                    [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
                        NSLog(@"%@", settings);
                    }];
                } else {
                    NSLog(@"注册失败");
                }
            }];
        }
    } else if (sysVersion > 8.0){
        //iOS8 - iOS10
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge categories:nil]];
    } else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        //iOS8系统以下
        [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];
#pragma clang diagnostic pop
    }
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
4、替换系统代理方法实现
- (void)exchangeMethod:(id)delegate {
    Class appDelegate = [delegate class];
    tt_swizzleMethodImplementation(appDelegate, [self class], @selector(application:didRegisterForRemoteNotificationsWithDeviceToken:), @selector(tt_application:didRegisterForRemoteNotificationsWithDeviceToken:));
    
    tt_swizzleMethodImplementation(appDelegate, [self class], @selector(application:didFailToRegisterForRemoteNotificationsWithError:), @selector(tt_application:didFailToRegisterForRemoteNotificationsWithError:));

    tt_swizzleMethodImplementation(appDelegate,[self class], @selector(application:didRegisterUserNotificationSettings:), @selector(tt_application:didRegisterUserNotificationSettings:));
    
//    tt_swizzleMethodImplementation(appDelegate, [self class], @selector(application:didReceiveRemoteNotification:), @selector(tt_application:didReceiveRemoteNotification:));
    
    tt_swizzleMethodImplementation(appDelegate,[self class], @selector(application:didReceiveRemoteNotification:fetchCompletionHandler:), @selector(tt_application:didReceiveRemoteNotification:fetchCompletionHandler:));
    
    if (@available(iOS 10.0, *)) {
        tt_swizzleMethodImplementation(appDelegate,[self class], @selector(userNotificationCenter:willPresentNotification:withCompletionHandler:), @selector(tt_userNotificationCenter:willPresentNotification:withCompletionHandler:));

        tt_swizzleMethodImplementation(appDelegate,[self class], @selector(userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:), @selector(tt_userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:));
    }
}
5、runtime 替换imp (核心)
void tt_swizzleMethodImplementation(Class originC,Class cusC ,SEL originSEL, SEL cusSEL) {
    Method originMethod = class_getInstanceMethod(originC, originSEL);
    Method cusMethod = class_getInstanceMethod(cusC, cusSEL);
    if (!originMethod) {//如果originMethod没有,给originC添加自定义方法
        BOOL added = class_addMethod(originC, cusSEL, method_getImplementation(cusMethod), method_getTypeEncoding(cusMethod));
        if (added) {//如果添加成功,则替换系统的imp
            class_replaceMethod(originC, originSEL, method_getImplementation(cusMethod), method_getTypeEncoding(cusMethod));
        }
    } else {
        method_exchangeImplementations(originMethod, cusMethod);
    }
}
6、如果需要统计推送到达率,iOS10以上系统版本可以在 push service extension 内部调用留出的接口,但是需要配置以下两点:
1、extension target -> Build settings -> other linker flags -> 添加 -all_load 
2、project -> Info -> configurations -> 添加extension 的 Pods
7、最后在appDelegate中调用
    [[TTPushManager shareInstance] registerRemoteNotification:self];
    [TTPushManager shareInstance].delegate = self;
    [[TTPushManager shareInstance] registerModelName:[xxx class] analysisKey:nil pushMsgIdKey:nil];

以上就是推送部分的简单封装,当然了,也可以把分享、支付封装下,制作成自己的私有库,这样就不用在appDelegate内码一大堆代码了,而且为以后的组件化做准备。

项目中是以私有库的形式使用的,所以没有制作成共有库上传到GitHub。

最后附上demo地址,喜欢就star下吧,如有问题,欢迎交流,谢谢。

点赞是您的态度!

demo地址

你可能感兴趣的:(iOS 远程推送封装-优化 remote notification)