美文网首页
iOS推送记录

iOS推送记录

作者: 尽斩桃花三千 | 来源:发表于2018-05-31 18:05 被阅读30次

    APNSiOS端代码实现

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        
        //判断是不是通过点击远程通知启动app
        NSDictionary*message = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        self.launchedFromRemoteNotification = (message != nil);
        
        
         [self registerRemoteNotification];
        
        return YES;
    }
    
    
    
    #pragma mark - remote notification
    /** 注册远程通知 */
    - (void)registerRemoteNotification {
        /*
         警告:Xcode8的需要手动开启“TARGETS -> Capabilities -> Push Notifications”
         */
        
        /*
         警告:该方法需要开发者自定义,以下代码根据APP支持的iOS系统不同,代码可以对应修改。
         以下为演示代码,注意根据实际需要修改,注意测试支持的iOS系统都能获取到DeviceToken
         */
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
    #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 // Xcode 8编译会调用
            UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
            center.delegate = self;
            [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionCarPlay) completionHandler:^(BOOL granted, NSError *_Nullable error) {
                if (!error) {
                    NSLog(@"request authorization succeeded!");
                }
            }];
            
            [[UIApplication sharedApplication] registerForRemoteNotifications];
    #else // Xcode 7编译会调用
            UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge);
            UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
            [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
            [[UIApplication sharedApplication] registerForRemoteNotifications];
    #endif
        } else if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
            UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge);
            UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
            [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
            [[UIApplication sharedApplication] registerForRemoteNotifications];
        } else {
            UIRemoteNotificationType apn_type = (UIRemoteNotificationType)(UIRemoteNotificationTypeAlert |
                                                                           UIRemoteNotificationTypeSound |
                                                                           UIRemoteNotificationTypeBadge);
            [[UIApplication sharedApplication] registerForRemoteNotificationTypes:apn_type];
        }
    }
    
    
    //获取DeviceToken成功
    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
    {
        NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
        token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
        NSLog(@"\n>>>[DeviceToken Success]:%@\n\n", token);
        if (![token isEqualToString:[BBPushManager sharedManager].currentToken]) {
            [BBPushManager sharedManager].currentToken = token;
            [[BBPushManager sharedManager] save];
            [[BBPushManager sharedManager] updateTokenToServer];
        }
        
    }
    
    //注册消息推送失败
    - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
    {
        NSLog(@"\n>>>[DeviceToken Error]:%@\n\n", error.description);
    }
    
    
    //处理收到的消息推送
    //#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_10_0
    
    //#endif
    
    #pragma mark - iOS 10中收到推送消息
    
    #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
    //  iOS 10: App在前台获取到通知
    - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
        
        NSLog(@"willPresentNotification:%@", notification.request.content.userInfo);
         self.launchedFromRemoteNotification = NO;
        NSDictionary *message = notification.request.content.userInfo;
        [self handlePushMessage:message];
        // 根据APP需要,判断是否要提示用户Badge、Sound、Alert
        completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
    }
    
    //  iOS 10: 点击通知进入App时触发
    - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler {
        
        self.launchedFromRemoteNotification = YES;
        NSLog(@"didReceiveNotification:%@", response.notification.request.content.userInfo);
        NSDictionary *message = response.notification.request.content.userInfo;
        [self handlePushMessage:message];
        completionHandler();
    }
    #else
    #endif
    // app打开状态收到通知,会调用  已经弃用
    //- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
    //{
    //    [self updateLaunchedFromRemoteNotification:application];
    //    [[BBPushManager sharedManager] deviceShake];
    //}
    // app打开状态收到通知,会调用 iOS10以前
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
        
        [self updateLaunchedFromRemoteNotification:application];
        [self handlePushMessage:userInfo];
        completionHandler(UIBackgroundFetchResultNewData);
    }
    
    
    
    
    /**
     *  @brief ios10以前 判断是否是从通知回到前台还是点击app到前台
     *
     *  @param application UIApplication
     */
    -(void)updateLaunchedFromRemoteNotification:(UIApplication *)application
    {
        switch (application.applicationState) {
            case UIApplicationStateActive:
            {
                self.launchedFromRemoteNotification = NO;
            }
                break;
            case UIApplicationStateBackground:
            {
                self.launchedFromRemoteNotification = YES;
            }
                break;
            case UIApplicationStateInactive:
            {
                self.launchedFromRemoteNotification = YES;
            }
                break;
            default:
                break;
        }
    }
    
    #pragma mark - notify handle
    -(void)handlePushMessage:(NSDictionary *)messageInfo
    {
        [[BBPushManager sharedManager] handlePushMessage:messageInfo launchedFromRemoteNotify:self.launchedFromRemoteNotification];
        self.launchedFromRemoteNotification = NO;
        
    }
    
    

    APNS推送工具

    推送工具: https://github.com/shaojiankui/SmartPush

    相关文章

      网友评论

          本文标题:iOS推送记录

          本文链接:https://www.haomeiwen.com/subject/lnmujftx.html