42

iOS-远程推送,静默推送,自定义消息推送

 5 years ago
source link: http://www.cocoachina.com/ios/20180814/24566.html?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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

  • (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];
            }
        }
    }
}

静默推送

应用想收到静默推送需要满足的条件:

  • 1.应用在前台/后台 (应用被杀死就收不到了)

  • 2.应用实现了

    application:didReceiveRemoteNotification:fetchCompletionHandler:

  • 3.如果仅仅实现了 application:didReceiveRemoteNotification: ,没有实现 application:didReceiveRemoteNotification:fetchCompletionHandler: ,应用只有在前台时才能收到静默推送 , 应用在后台时,收不到静默推送

自定义消息推送

介绍:极光推送提供了自定义消息推送 , 这种推送只有当应用在 前台 时才能收到 ; 当应用在 后台/被杀死 时,这时候的自定义消息被保存下来,直到应用处于前台时,应用才会收到.

使用场景:当需要在前台处理大量数据的时候,可以使用自定义消息,例如应用某个模块需要更新了,这时候后台发送个自定义消息,等到应用启动了就可以自动去下载

// 使用自定义消息,需要在通知中心注册
 // 注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveJPushCustomMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];

// 接收到JPush的自定义消息推送
- (void)receiveJPushCustomMessage:(NSNotification *)notification {

    NSDictionary *userInfo = [notification userInfo];

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

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

    NSDictionary *extra = [userInfo valueForKey:@"extras"];

}

总结:

1.应用在后台/前台/被杀死,都可以收到普通的远程推送

2.应用被杀死时,可以通过Background Fetch短时间唤醒应用

3.应用在后台/前台时,可以通过静默推送,修改一些数据

4.自定义消息应用在


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK