iOS 推送的实现

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

        

    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

 

   

    // 处理远程推送

    if (launchOptions && [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey]) {

        [self checkRemoteNotification:[launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey]];

    }

    

    //后台(未激活状态)接到服务器消息时执行

    if ([UIApplication sharedApplication].applicationIconBadgeNumber != 0) {

         添加接到推送消息时需求

    }

    //进图APP将对通知相应的提示去掉

    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;

    

    // 开启远程推送

    [self openRemoteNotification];

    

    return YES;

}

 

// 开启推送

- (void)openRemoteNotification {

      // 开启推送

    if (!iOS7) {

        

        [[UIApplication sharedApplication] registerForRemoteNotifications];

        UIUserNotificationSettings* requestedSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge)  categories:nil];

        [[UIApplication sharedApplication] registerUserNotificationSettings:requestedSettings];

        

    } else {

        

        // Enable APNS

        [[UIApplication sharedApplication]

         registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];

        

    }

    

}

 

#pragma mark - ANPS

注册成功 

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

    

    if (deviceToken) {

        NSString * deviceTokenString = [[[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] stringByReplacingOccurrencesOfString:@" " withString:@""];

    .............

        此处将deviceTokenString上传给服务器

        

    }

}

 

//接收到远程推送

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

    

    // 远程推送处理

    [self checkRemoteNotification:userInfo];

    

}

注册设备失败

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error

{

    NSLog(@"Failed to get token, error: %@", error);

}

从后台点击APP图标进入     

- (void)applicationWillEnterForeground:(UIApplication *)application {

判断是否有接到消息通知    

    if ([UIApplication sharedApplication].applicationIconBadgeNumber != 0) {

        添加接到推送消息时需求

    }

    

    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;

}

远程推送处理

- (void)checkRemoteNotification:(NSDictionary*)userInfo {

    

    if (userInfo == nil || [userInfo isKindOfClass:[NSNull class]]) {

        return;

    }

#if !TARGET_IPHONE_SIMULATOR

    

NSLog(@"remote notification: %@",[userInfo description]);

    

    NSDictionary *apsInfo = [userInfo valueForKey:@"aps"];

    NSString *alert = [apsInfo valueForKey:@"alert"];

    NSNumber *badge = [apsInfo valueForKey:@"badge"];此body中必须有 

    

    NSNumber *type = [userInfo valueForKey:@"type"];

    NSNumber *menu = [userInfo valueForKey:@"menu"];

    NSString *text = [userInfo valueForKey:@"text"];

    NSString *link = [userInfo valueForKey:@"link"];

    NSString *val  = [userInfo valueForKey:@"val"];

    

    if ((!menu || [menu isKindOfClass:[NSNull class]]) ||

        (!type || [type isKindOfClass:[NSNull class]]) ){

        return;

    }

    

    //APP icon消息提示的显示

    if ([apsInfo valueForKey:@"badge"] && ![[apsInfo valueForKey:@"badge"] isKindOfClass:[NSNull class]]) {

        if([self canSendNotifications]) {

            [UIApplication sharedApplication].applicationIconBadgeNumber = [[apsInfo valueForKey:@"badge"] integerValue];

        }

    }

    

    // 服务器喊我打开应用去干活啦

    if ([type integerValue] == 3) {

     

        NSMutableDictionary *apnsInfo = [NSMutableDictionary dictionaryWithDictionary:userInfo];

        [apnsInfo setObject:@([UIApplication sharedApplication].applicationState == UIApplicationStateActive) forKey:@"isActive"];

        

        // 推送通知

        [[NSNotificationCenter defaultCenter] postNotificationName:k_NOTI_APNS_PUSH object:apnsInfo];

 

    }

    

#endif

 

}

 

// 判断是否能接收推送

- (BOOL)canSendNotifications;

{

    if (iOS7) {

        return YES;

    }

    

    UIUserNotificationSettings* notificationSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];

    BOOL canSendNotifications = NO;

    if (notificationSettings.types == (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)) {

        canSendNotifications =YES;

    }

    return canSendNotifications;

}

 

转载于:https://www.cnblogs.com/leishuai/p/4571133.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值