ios推送的那几个方法

方法介绍

( >= IOS 10 )userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:

ios10以上调用,后台模式或者应用关闭状态。

( >= IOS 10 )userNotificationCenter:willPresentNotification:withCompletionHandler:

ios10以上调用,应用打开状态下调用。

( >= IOS 7 )application:didReceiveRemoteNotification:fetchCompletionHandler:

ios7到ios10之间调用。在方法内部可以判断[application applicationState]确定应用是前台、后台或者关闭状态,然后做出响应的响应。

( < IOS 7 )application:didReceiveRemoteNotification:

ios7之前调用,目前ios9以下的用户不到3%,这个方法可以放弃了。

友盟推送代码示例

//
//  AppDelegate+UMeng.m
//  SSWG
//
//  Created by DaLei on 2017/8/29.
//  Copyright © 2017年 DaLei. All rights reserved.
//

#import "AppDelegate+UMeng.h"
#import "UtilMacros.h"
#import "UMMobClick/MobClick.h"
#import "UMessage.h"
#import //仅作为调试引用引用的
#import "MessageViewController.h"

@implementation AppDelegate (UMeng)

#pragma mark - application 推送相关

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    DLog(@"application didRegisterForRemoteNotificationsWithDeviceToken");
    //1.2.7版本开始不需要用户再手动注册devicetoken,SDK会自动注册
    //[UMessage registerDeviceToken:deviceToken];
    NSString *tokenString = [self stringDevicetoken:deviceToken];
    DLog(@"tokenString = %@",tokenString);
}

-(void)application:(UIApplication *)application didReceiveRemoteNotification:userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler{
    //关闭友盟自带的弹出框
    [UMessage setAutoAlert:NO];
    
    //必须加这句代码
    [UMessage didReceiveRemoteNotification:userInfo];
    
    DLog(@"%@",userInfo);
    
    UIApplicationState state = [application applicationState];
    //关闭或者后台模式
    if (state == UIApplicationStateInactive || state == UIApplicationStateBackground) {
        //处理消息跳转
        [self pushNotificationNavigation:userInfo];
        
    } else {//前台模式
        
        /////////////////////////////////////////////////////////////////////
        ////////////////////////实现给tabbar图标增加角标////////////////////////
        ////////////////////////////////////////////////////////////////////
    }
}


#pragma mark - 友盟统计

/**
 友盟统计功能
 */
- (void)setupUMengStatistic {
    UMConfigInstance.appKey = kAppKey_UMeng;
    UMConfigInstance.channelId = @"App Store";
    //配置以上参数后调用此方法初始化SDK!
    [MobClick startWithConfigure:UMConfigInstance];
    //打开调试模式
    [MobClick setLogEnabled:YES];
}

#pragma mark - 友盟推送

/**
 友盟推送
 */
- (void)setupUMengPushWithOptions:(NSDictionary *)launchOptions{
    
    //设置AppKey
    [UMessage startWithAppkey:kAppKey_UMeng launchOptions:launchOptions];
    
    //注册通知
    [UMessage registerForRemoteNotifications];
    
    //设置debug模式
    [UMessage openDebugMode:YES];
    
    //iOS10必须加下面这段代码。
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate=self;
    UNAuthorizationOptions types10 = UNAuthorizationOptionBadge|UNAuthorizationOptionAlert|UNAuthorizationOptionSound;
    [center requestAuthorizationWithOptions:types10 completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted) {
            //点击允许
        } else {
            //点击不允许
        }
    }];
    
    //for log
    [UMessage setLogEnabled:YES];
}

#pragma mark UNUserNotificationCenterDelegate

//iOS10新增:处理前台收到通知的代理方法
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
    NSDictionary *userInfo = notification.request.content.userInfo;
    DLog(@"%@",userInfo);
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        //应用处于前台时的远程推送接受
        
        //关闭友盟自带的弹出框
        [UMessage setAutoAlert:NO];
        
        //必须加这句代码
        [UMessage didReceiveRemoteNotification:userInfo];
        
        /////////////////////////////////////////////////////////////////////
        ////////////////////////实现给tabbar图标增加角标////////////////////////
        ////////////////////////////////////////////////////////////////////
        
    } else {
        //应用处于前台时的本地推送接受
        
    }
}

//iOS10新增:处理后台点击通知的代理方法
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    DLog(@"%@",userInfo);
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        //应用处于后台时的远程推送接受
        
        //必须加这句代码
        [UMessage didReceiveRemoteNotification:userInfo];
        
        //处理消息跳转
        [self pushNotificationNavigation:userInfo];
        
    } else {
        //应用处于后台时的本地推送接受
        
    }
}

#pragma mark - 收到通知后的处理页面

- (void)pushNotificationNavigation:(NSDictionary *)userInfo{
    
    UIViewController *homeVc = [self.window.rootViewController.childViewControllers firstObject];
    
    if ([homeVc isKindOfClass:[UINavigationController class]]) {
        
        UINavigationController *nav = (UINavigationController *)homeVc;
        
        UIViewController *vc = [nav.viewControllers objectAtIndex:0 ];
        
        MessageViewController *vcnext = MessageViewController.new;
        
        vcnext.view.backgroundColor = [UIColor whiteColor];
        
        [vc.navigationController pushViewController:vcnext animated:YES];
    }
}

#pragma mark - 以下的方法仅作调试使用

-(NSString *)stringDevicetoken:(NSData *)deviceToken {
    NSString *token = [deviceToken description];
    NSString *pushToken = [[[token stringByReplacingOccurrencesOfString:@"<"withString:@""]
                            stringByReplacingOccurrencesOfString:@">"withString:@""]
                           stringByReplacingOccurrencesOfString:@" "withString:@""];
    return pushToken;
}

-(NSString *)openUDID {
    NSString* openUdid = nil;
    if (openUdid==nil) {
        CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
        CFStringRef cfstring = CFUUIDCreateString(kCFAllocatorDefault, uuid);
        const char *cStr = CFStringGetCStringPtr(cfstring,CFStringGetFastestEncoding(cfstring));
        unsigned char result[16];
        CC_MD5( cStr,(CC_LONG)strlen(cStr), result );
        CFRelease(uuid);
        CFRelease(cfstring);
        openUdid = [NSString stringWithFormat:
                    @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%08lx",
                    result[0], result[1], result[2], result[3],
                    result[4], result[5], result[6], result[7],
                    result[8], result[9], result[10], result[11],
                    result[12], result[13], result[14], result[15],
                    (unsigned long)(arc4random() % NSUIntegerMax)];
    }
    return openUdid;
}

@end

你可能感兴趣的:(ios推送的那几个方法)