Skip to content

Commit

Permalink
移除推送注册模块,推送注册归入Delegate单独处理
Browse files Browse the repository at this point in the history
  • Loading branch information
ginhoor committed Apr 3, 2019
1 parent cfce039 commit 44577b4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 86 deletions.
11 changes: 4 additions & 7 deletions GHApplicationMediator/Classes/GHApplicationMediator.h
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
//
// GHApplicationMediator.h
// CommercialVehiclePlatform
// GinhoorFramework
//
// Created by JunhuaShao on 2019/3/4.
// Copyright © 2019 JunhuaShao. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <UserNotifications/UserNotifications.h>

NS_ASSUME_NONNULL_BEGIN
#define GHAPPLICATIONMEDIATOR_DEBUG_MODE 0

@interface GHApplicationMediator : UIResponder<UIApplicationDelegate, UNUserNotificationCenterDelegate>
NS_ASSUME_NONNULL_BEGIN

@interface GHApplicationMediator : UIResponder <UIApplicationDelegate>

+ (instancetype)sharedInstance;

+ (NSArray *)applicationModuleDelegates;

+ (void)registerAppilgationModuleDelegate:(id<UIApplicationDelegate>)moduleDelegate;
+ (void)registerNotificationModuleDelegate:(id<UIApplicationDelegate, UNUserNotificationCenterDelegate>)moduleDelegate;
+ (BOOL)removeModuleDelegateByClass:(Class)moduleClass;

@property (nonatomic, assign) UNNotificationPresentationOptions defaultNotificationPresentationOptions;

@end

NS_ASSUME_NONNULL_END
Expand Down
87 changes: 8 additions & 79 deletions GHApplicationMediator/Classes/GHApplicationMediator.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// GHApplicationMediator.m
// CommercialVehiclePlatform
// GinhoorFramework
//
// Created by JunhuaShao on 2019/3/4.
// Copyright © 2019 JunhuaShao. All rights reserved.
Expand All @@ -9,19 +9,11 @@
#import "GHApplicationMediator.h"

@interface GHApplicationMediator()

@property (nonatomic, strong) NSMutableArray *applicationModuleDelegates;

@end

@implementation GHApplicationMediator

- (void)setupDefaultValues
{
// 根据APP需要,判断是否要提示用户Badge、Sound、Alert
self.defaultNotificationPresentationOptions = UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert;
}

+ (instancetype)sharedInstance
{
static dispatch_once_t onceToken;
Expand All @@ -45,49 +37,32 @@ + (void)registerAppilgationModuleDelegate:(id<UIApplicationDelegate>)moduleDeleg
[self addModuleDelegate:moduleDelegate];
}

+ (void)registerNotificationModuleDelegate:(id<UIApplicationDelegate, UNUserNotificationCenterDelegate>)moduleDelegate
{
NSAssert(moduleDelegate, @"ERROR:添加的AppDelegate为空", [moduleDelegate class]);
NSAssert([moduleDelegate conformsToProtocol:@protocol(UIApplicationDelegate)], @"ERROR:添加的AppDelegate未实现<UIApplicationDelegate>", [moduleDelegate class]);
NSAssert([moduleDelegate conformsToProtocol:@protocol(UNUserNotificationCenterDelegate)], @"ERROR:添加的AppDelegate未实现<UNUserNotificationCenterDelegate>", [moduleDelegate class]);

[self addModuleDelegate:moduleDelegate];
}

+ (void)addModuleDelegate:(id)moduleDelegate
{
GHApplicationMediator *mediator = [GHApplicationMediator sharedInstance];
#ifdef DEBUG

#if GHAPPLICATIONMEDIATOR_DEBUG_MODE
[mediator.applicationModuleDelegates enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

NSAssert(![obj isKindOfClass:[moduleDelegate class]], @"ERROR:重复添加的delegate:%@", [moduleDelegate class]);
}];
#endif
[mediator.applicationModuleDelegates addObject:moduleDelegate];
#ifdef DEBUG
#if GHAPPLICATIONMEDIATOR_DEBUG_MODE
NSLog(@"applicationModuleDelegates:\n%@",mediator.applicationModuleDelegates);
#endif
}

+ (BOOL)removeModuleDelegateByClass:(Class)moduleClass
{
GHApplicationMediator *mediator = [GHApplicationMediator sharedInstance];

BOOL result = NO;
NSInteger i = 0;
NSInteger count = mediator.applicationModuleDelegates.count;
while (i < count) {
id delegate = mediator.applicationModuleDelegates[i];
if ([delegate isKindOfClass:moduleClass]) {
__block BOOL result = NO;
[mediator.applicationModuleDelegates enumerateObjectsUsingBlock:^(id _Nonnull delegate, NSUInteger idx, BOOL * _Nonnull stop) {
if ([delegate isMemberOfClass:moduleClass]) {
[mediator.applicationModuleDelegates removeObject:delegate];
count--;
result = YES;
break;
*stop = YES;
}
i++;
}

}];
return result;
}

Expand All @@ -103,7 +78,6 @@ - (instancetype)init
- (void)setup
{
_applicationModuleDelegates = [NSMutableArray array];
[self setupDefaultValues];
}

#pragma mark- Handle Method
Expand Down Expand Up @@ -168,51 +142,6 @@ - (void)forwardInvocation:(NSInvocation *)anInvocation
}
}

#pragma mark- Delegate

#pragma mark iOS10以下 收到推送消息
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
{
__block UIBackgroundFetchResult fetchResult = UIBackgroundFetchResultNewData;
[self notifySelectorOfAllDelegates:@selector(application:didReceiveRemoteNotification:fetchCompletionHandler:) nofityHandler:^(id<UIApplicationDelegate> delegate) {
[delegate application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:^(UIBackgroundFetchResult result) {
//接受最后一个delegate的result,最后统一回调完成
fetchResult = result;
}];
}];
completionHandler(fetchResult);
}

#pragma mark iOS10以上 收到推送消息 <UserNotifications/UserNotifications.h>

// App在前台获取到通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
#ifdef DEBUG
NSLog(@"willPresentNotification:%@", notification.request.content.userInfo);
#endif
__block UNNotificationPresentationOptions completionOptions = self.defaultNotificationPresentationOptions;
[self notifySelectorOfAllDelegates:@selector(userNotificationCenter:willPresentNotification:withCompletionHandler:) nofityHandler:^(id delegate) {
[delegate userNotificationCenter:center willPresentNotification:notification withCompletionHandler:^(UNNotificationPresentationOptions options) {
//接受最后一个delegate设置的options,且options不为空。
if (options != 0) {
completionOptions = options;
}
}];
}];
completionHandler(completionOptions);
}

// 应用在前台点击通知进入App时触发
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
{
[self notifySelectorOfAllDelegates:@selector(userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:) nofityHandler:^(id delegate) {
[delegate userNotificationCenter:center didReceiveNotificationResponse:response withCompletionHandler:^{
//不执行任何回调,统一最后回调完成。
}];
}];
completionHandler();
}

#pragma mark- Private Method

Expand Down
3 changes: 3 additions & 0 deletions repo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# !/bin/bash
pod repo push GinhoorSpecs GHApplicationMediator.podspec
echo "push finished!"

0 comments on commit 44577b4

Please sign in to comment.