美文网首页
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推送记录

    APNSiOS端代码实现 APNS推送工具 推送工具: https://github.com/shaojianku...

  • 记录iOS推送

    每一次接推送,每一次必百度。这次记录下来,方便(我自己)下次开发。如有不对或缺了啥,请留言告诉我。 那就开始吧。 ...

  • iOS 推送通知

    iOS 推送通知 iOS 推送通知

  • 记录下接友盟ios的推送

    最近项目要接友盟的ios端推送,所以记录下坑点 1.参考的一些博主的步骤 1.玩 iOS 友盟推送 2.iOS集成...

  • iOS 远程推送通知

    iOS 远程推送通知 分分钟搞定IOS远程消息推送 iOS推送通知的实现步骤 推送通知iOS客户端编写实现及推送服...

  • iOS 玩转推送通知

    iOS 玩转推送通知 iOS 玩转推送通知

  • 细说 iOS 消息推送

    细说 iOS 消息推送 细说 iOS 消息推送

  • iOS8-推送(本地和远程)的简单使用

    参考文章:本地推送:一、iOS推送之本地推送(iOS Notification Of Local Notifica...

  • 【知识总结】(2)远程推送

    推送SDK:极光推送 后台点击推送: iOS 10 以下收到推送点击触发 iOS 10 以上触发: 极光推送中使用...

  • iOS 推送通知及通知扩展

    级别: ★★☆☆☆标签:「iOS 本地推送」「iOS 远程推送」「iOS通知扩展」作者: dac_1033审校: ...

网友评论

      本文标题:iOS推送记录

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