当前位置 主页 > 服务器问题 > Linux/apache问题 >

    iOS学习笔记之远程推送、静默推送与自定义消息推送

    栏目:Linux/apache问题 时间:2019-12-31 00:40

    远程推送时 , 应用可能处于下列三种状态:

         (1) . 应用开启时 , 应用在前台

        (2) . 应用开启时 , 应用在后台

        (3) . 应用未启动(应用被杀死)

    从苹果APNS服务器远程推送时:

    1 . 如果应用处于 (1) 状态 , 则不会发出声音 , 会直接调用appDelegate的代理方法didReceiveRemoteNotification,此时如果想收到类似系统的弹窗提示,则需要自定义弹窗,提示音,振动(弹窗可以参考 : ForeNotification  (本地下载))

    AudioServicesPlaySystemSound(1007);//系统提示音
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//震动

    2 . 如果应用处于 (2) 状态 , 则会发出提示音, 点击推送消息 , 则会调用appDelegate的代理方法didReceiveRemoteNotification

    3 . 如果应用处于 (3) 状态,则会发出提示音 , 点击推送消息 , 则会开启应用 , 在下面这个方法中会带上launchOptions这个参数,如果实现了application:didReceiveRemoteNotification:fetchCompletionHandler:这个方法,则会调用这个方法

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
     NSDictionary *remoteNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
    
     if (remoteNotification) {
     //自定义的BOOL值,用来标记是从通知启动的应用
     self.isLaunchedByNotification = YES;
     }else{
    
     }
     [self checkIsLaunchedByNotification];
    
     return YES;
    }

    收到远程推送后 , 可以跳转到消息界面 :

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
    {
     NSDictionary *aps = [userInfo valueForKey:@"aps"];
    
     NSDictionary *alert = [aps valueForKey:@"alert"];
    
     NSString * body = alert[@"body"];
    
     if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
     //处于前台时
     
     [EBForeNotification handleRemoteNotification:@{@"aps":@{@"alert":[NSString stringWithFormat:@"%@",body]}} soundID:1312];
     
     }else{
     
     //处于后台时
     
     [self gotoMessageVC];
     }
    }
    
    #pragma mark - 检测是否从通知栏启动得应用
    
    - (void)checkIsLaunchedByNotification{
     
     if (self.isLaunchedByNotification) {
     
     [self gotoMessageVC];
     
     }
    }
    
    #pragma mark - 点击了通知菜单(当应用在前台时,收到推送,点击了自定义的弹窗,调用的方法)
    
    - (void)clickBannerView:(NSNotification *)notification{
     
     NSDictionary * dict = notification.object;
     
     [self gotoMessageVC];
    }
    
    #pragma mark - 跳转到消息界面(点击通知菜单/点击通知栏启动应用时)
    
    - (void)gotoMessageVC{
     
     if([self.window.rootViewController isEqual:self.tabBarController]){
     
     if([self.tabBarController.selectedViewController isKindOfClass:[UINavigationController class]]){
      UINavigationController * nav = self.tabBarController.selectedViewController;
      
      if (![nav.topViewController isKindOfClass:[MessagesViewController class]]) {
      
      MessagesViewController *messageVC = [[MessagesViewController alloc] init];
      
      messageVC.hidesBottomBarWhenPushed = YES;
      
      [nav.visibleViewController.navigationController pushViewController:messageVC animated:YES];
      }
     }
     }
    }