码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 推送的实现

时间:2015-06-12 11:24:20      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

- (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;

}

 

iOS 推送的实现

标签:

原文地址:http://www.cnblogs.com/leishuai/p/4571133.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!