iOS 远程推送APNS从0至发布-代码集成篇

2017-01-10  本文已影响342人  cyh老崔

说明

头文件

#import "AppDelegate.h"
#import "JPUSHService.h"

// iOS10注册APNs所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif

//JPush
static NSString *const appKey = @"5a6e94518f6bd85ba173cf77";
static NSString *const channel = @"App Store";
static BOOL const isProduction = YES;

@interface AppDelegate ()<JPUSHRegisterDelegate>

@end

辅助测试的UI代码:


#pragma mark --------------------------------------------------------
#pragma mark observe & addView
- (void)networkDidReceiveMsg:(NSNotification *)note{
    
//    NSDictionary * userInfo = [note userInfo];
//    NSString *content = [userInfo valueForKey:@"content"];
//    NSDictionary *extras = [userInfo valueForKey:@"extras"];
//    NSString *customizeField1 = [extras valueForKey:@"customizeField1"]; //服务端传递的Extras附加字段,key是自己定义的
    
    NSString *text = @"NSNotification";
    [self addTestViewRed:text];
}

- (void)addTestViewYellow:(NSString *)text{
    UIView *testView = [[UIView alloc] init];
    testView.backgroundColor = [[UIColor yellowColor] colorWithAlphaComponent:0.5];
    testView.yh_size = CGSizeMake(100, 100);
    testView.center = [UIApplication sharedApplication].keyWindow.center;
    UITextView *textView = [[UITextView alloc] init];
    textView.bounces = YES;
    textView.text = text;
    textView.frame = testView.bounds;
    textView.yh_height -= 20;
    [testView addSubview:textView];
    [self.window.rootViewController.view addSubview:testView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(testGes:)];
    [testView addGestureRecognizer:tap];
}
- (void)addTestViewRed:(NSString *)text{
    UIView *testView = [[UIView alloc] init];
    testView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.5];
    testView.yh_size = CGSizeMake(100, 100);
    testView.center = self.window.center;
    UITextView *textView = [[UITextView alloc] init];
    textView.bounces = YES;
    textView.text = text;
    textView.frame = testView.bounds;
    textView.yh_height -= 20;
    [testView addSubview:textView];
    [self.window.rootViewController.view addSubview:testView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(testGes:)];
    [testView addGestureRecognizer:tap];
}
- (void)testGes:(UITapGestureRecognizer *)tapGes{
    
    [tapGes.view removeFromSuperview];
}

注册代码:


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    application.applicationIconBadgeNumber = 0;
    //设置推送
    [self setupJPush:launchOptions];
    return YES;
}

//建立JPush
- (void)setupJPush:(NSDictionary *)launchOptions{
    
    
    //Required
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
        JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
        entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;
        [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
    }
    else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        //可以添加自定义categories
        [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                          UIUserNotificationTypeSound |
                                                          UIUserNotificationTypeAlert)
                                              categories:nil];
    } else {
        //categories 必须为nil
        [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                          UIRemoteNotificationTypeSound |
                                                          UIRemoteNotificationTypeAlert)
                                              categories:nil];
    }
    
    //Required
    [JPUSHService setupWithOption:launchOptions appKey:appKey
                          channel:channel
                 apsForProduction:isProduction];
    
    
    ///极光文档:只有在前端运行的时候才能收到自定义消息的推送。
   //笔者并没有测试到执行这个通知方法
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkDidReceiveMsg:) name:kJPFNetworkDidReceiveMessageNotification object:nil];
    
    if (launchOptions) {
        
        NSString *alert = launchOptions[@"aps"][@"alert"];
        NSString *string = [NSString stringWithFormat:@"%@%@", @"有通知: setupJPush", alert];
        [self addTestViewRed:string];
    }
    
}

处理推送的各方法

#pragma mark ------------------------------------------
#pragma mark JPush methods
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    /// Required - 注册 DeviceToken
    [JPUSHService registerDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    
    NSString *alert = userInfo[@"aps"][@"alert"];
    if (application.applicationState == UIApplicationStateActive) {
        YHLog(@"active,接收到远程通知userInfo--");
        
        application.applicationIconBadgeNumber = 0;
        
        ///接收到远程通知后的操作
        NSString *text = [NSString stringWithFormat:@"%@%@", @"fetchCompletionHandler -- UIApplicationStateActive---", alert];
        [self addTestViewRed:text];
        
    }else if(application.applicationState == UIApplicationStateBackground){
        //程序处于后台,推送时不勾选`content avaliable` 时没有这个模式
        NSString *text = [NSString stringWithFormat:@"%@%@", @"fetchCompletionHandler -- UIApplicationStateBackground---", alert];
        [self addTestViewRed:text];
        
    }else if (application.applicationState == UIApplicationStateInactive){
        YHLog(@"UIApplicationStateInactive");
        
        ///接收到远程通知后的操作
        NSString *text = [NSString stringWithFormat:@"%@%@", @"fetchCompletionHandler -- UIApplicationStateInactive---", alert];

        [self addTestViewRed:text];
    }
    
    //     IOS 7 Support Required
    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}

#pragma mark JPUSHRegisterDelegate
// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
    
    // Required
    NSDictionary * userInfo = notification.request.content.userInfo;
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
    }
    completionHandler(UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置
    
    NSString *text = [NSString stringWithFormat:@"%@%@", @"JPUSHRegisterDelegate -- willPresentNotification---", userInfo[@"aps"][@"alert"]];
    [self addTestViewYellow:text];
}

// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
    
    // Required
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
    }
    completionHandler();  // 系统要求执行这个方法
    
    NSString *text = [NSString stringWithFormat:@"%@%@", @"JPUSHRegisterDelegate -- didReceiveNotificationResponse---", userInfo[@"aps"][@"alert"]];
    [self addTestViewYellow:text];
    
}

不同情况下调用的方法总结

iOS 10

1.应用在前台,

1.1 没有设置 content-available ,
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center 
willPresentNotification:(UNNotification *)notification 
withCompletionHandler:(void (^)(NSInteger))completionHandler
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center 
didReceiveNotificationResponse:(UNNotificationResponse *)response 
withCompletionHandler:(void (^)())completionHandler
1.2 设置了 content-available ,
- (void)application:(UIApplication *)application 
didReceiveRemoteNotification:(NSDictionary *)userInfo 
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center 
willPresentNotification:(UNNotification *)notification 
withCompletionHandler:(void (^)(NSInteger))completionHandler
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center 
didReceiveNotificationResponse:(UNNotificationResponse *)response 
withCompletionHandler:(void (^)())completionHandler 

2.应用在后台,

2.1 没有设置 content-available :
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center 
didReceiveNotificationResponse:(UNNotificationResponse *)response 
withCompletionHandler:(void (^)())completionHandler 
2.2 设置 content-available :
- (void)application:(UIApplication *)application 
didReceiveRemoteNotification:(NSDictionary *)userInfo 
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler 
 - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center 
didReceiveNotificationResponse:(UNNotificationResponse *)response 
withCompletionHandler:(void (^)())completionHandler

3.应用由死到生,

3.1 无论是否设置 content-available ,
 - (BOOL)application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center 
didReceiveNotificationResponse:(UNNotificationResponse *)response 
withCompletionHandler:(void (^)())completionHandler 

一. iOS 9

应用在前台,

无论是否设置 content-available , 都会调用方法,其中 application.applicationState == UIApplicationStateActive
- (void)application:(UIApplication *)application 
didReceiveRemoteNotification:(NSDictionary *)userInfo 
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

应用在后台,

没有设置 content-available , 有通知栏提示
- (void)application:(UIApplication *)application 
didReceiveRemoteNotification:(NSDictionary *)userInfo 
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
2.2 设置 content-available , 有通知栏提示
- (void)application:(UIApplication *)application 
didReceiveRemoteNotification:(NSDictionary *)userInfo 
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
- (void)application:(UIApplication *)application 
didReceiveRemoteNotification:(NSDictionary *)userInfo 
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

应用由死到生,

3.1 无论是否设置 content-available
  - (BOOL)application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
- (void)application:(UIApplication *)application 
didReceiveRemoteNotification:(NSDictionary *)userInfo 
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

注: 转载就注明出处: http://www.jianshu.com/p/9e2464be0fd5

上一篇 下一篇

猜你喜欢

热点阅读