美文网首页
iOS - 后台保活

iOS - 后台保活

作者: Mn_Su | 来源:发表于2020-04-27 15:26 被阅读0次

一、正常延长版保活(时间3min-40min不等)

1.定义定时器属性
   @property (nonatomic,strong)NSTimer *myTimer;
   @property (nonatomic,assign)UIBackgroundTaskIdentifier backgroundTaskIdentifier;

2.代码实现
- (void)applicationDidEnterBackground:(UIApplication *)application{
    self.backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^{
      [application endBackgroundTask:self.backgroundTaskIdentifier];
      self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
    }];

//开启定时器 不断向系统请求后台任务执行的时间

     self.myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(makeMoreTime) userInfo:nil repeats:YES];

     [self.myTimer fire]
}

-(void)makeMoreTime {

//如果系统给的剩余时间小于60秒 就终止当前的后台任务,再重新初始化一个后台任务,重新让系统分配时间,这样一直循环下去,保持APP在后台一直处于active状态。

MSULog(@"1 --- %.2f",[UIApplication sharedApplication].backgroundTimeRemaining);

if ([UIApplication sharedApplication].backgroundTimeRemaining < 60) {

    MSULog(@"Background Time Remaining = %.02f Seconds", [UIApplication sharedApplication].backgroundTimeRemaining);

    [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskIdentifier];

    self.backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{

        MSULog(@"重新");
        [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskIdentifier];

        self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;

    }];
}

二、永久版保活

第一种方式:利用后台定位功能实现,此模式只适合选择“一直允许使用定位”,但是在新系统上会出现如果用户选择“使用app时定位”,就会出现退到后台时候,手机顶部会出现蓝色定位标识,影响用户体验;如果用户拒绝使用定位,就尴尬了,除地图类app,其他app不建议此方式;

第二种方式:利用voip功能实现,此模式适合音视频类app,常规类app也可以考虑使用;voip推送和apns推送是苹果两大推送方式,和apns推送不一样的地方在于,voip推送属于强制推送,用户无法拒绝此推送模式(故在iOS13后,苹果限制voip推送使用,但不影响后台保活);实现voip推送,然后后台默认播放铃声!

  1. 选择后台模式
图片.png
  2. 应用AVFoundation和<PushKit/PushKit.h>框架、定义属性
    @property (nonatomic, strong) AVAudioPlayer *player;
    @property (nonatomic,assign)UIBackgroundTaskIdentifier backgroundTaskIdentifier;

  3. 后台铃声代码实现
    - (AVAudioPlayer *)player{
        if (!_player){
            NSURL *url=[[NSBundle mainBundle]URLForResource:@"work5.mp3" withExtension:nil];
            _player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
            [_player prepareToPlay];
    
            _player.volume = 0.00;
            //一直循环播放
            _player.numberOfLoops = -1;
            [[AVAudioSession sharedInstance] setActive:YES error:nil];
            [[AVAudioSession sharedInstance]setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
        }
        return _player;
    }

    - (void)startBgTask{
        UIApplication *application = [UIApplication sharedApplication];
        __block    UIBackgroundTaskIdentifier bgTask;
        bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
            //这里延迟的系统时间结束
            [application endBackgroundTask:bgTask];
            NSLog(@"%f",application.backgroundTimeRemaining);
        }];
    }

    - (void)applicationDidEnterBackground:(UIApplication *)application{
        [self startBgTask];
        [self.player play];
    }

     4. voip代码实现(配置voip证书省略。。。。)

      -(void)startVoIPPush{
          NSString * identifier = [[NSBundle mainBundle] bundleIdentifier];

          if ([identifier isEqualToString:@"com.eccalc.chat2u"] && [[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
                //if([[UIDevice currentDevice].systemVersion floatValue] < 10)
          {
                PKPushRegistry * pushRegistry = [[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()];
                pushRegistry.delegate = self;
                pushRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
          //}else{
  //            UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  //            center.delegate = self;
  //            [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)
  //                                  completionHandler:^(BOOL granted, NSError * _Nullable error) {
  //                                      // Enable or disable features based on authorization.
  //                                  }];
  //            [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
  //            }];
          }
    
  //        UIUserNotificationType types = (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert);
 //        UIUserNotificationSettings * notificationSettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
//        [[UIApplication sharedApplication]registerUserNotificationSettings:notificationSettings];
      }else{
          g_default removeObjectForKey:@"voipToken"];
      }
  }

  #pragma mark - PKPushRegistryDelegate
  -(void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(PKPushType)type{
      if ([credentials.token length] == 0) {
  //        MSULog(@"voip token NULL");
          return;
      }
      NSString * voipToken;

      if (@available(iOS 13.0, *)) {
          voipToken = [self getHexStringForData:credentials.token];
      }else {    
          voipToken = [[[[credentials.token description] stringByReplacingOccurrencesOfString:@"<" withString:@""] stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""];
      }
      //voipToken 需要提交给服务器
      [g_default setObject:voipToken forKey:@"voipToken"];
  //    MSULog(@"voipToken:%@",voipToken);
  }

  #pragma mark - 但在iOS11.0之后,系统也提供了另外一个方法实现唤醒后的操作,此方法开启后,我项目iOS13以下机型会出现受收不到voip消息情况,所以注释了
  //- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(PKPushType)type withCompletionHandler:(void (^)(void))completion
  //{
  //    if (type != PKPushTypeVoIP) {
  //        return;
  //    }
  //    [self dealWithPushkitDidReceiveIncomingPushWithPayload:payload];
  //}
  #pragma mark - iOS8.0~iOS11.0是使用上面这个代理方法来实现唤醒后的操作,
  -(void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(PKPushType)type{
      if (type != PKPushTypeVoIP) {
          return;
      }
      //此处写相关处理逻辑
      //[self dealWithPushkitDidReceiveIncomingPushWithPayload:payload];   
  }

相关文章

  • iOS app进入后台后 应用保活 后台保活

    iOS app进入后台后 应用保活 后台保活

  • iOS后台保活

    iOS后台保活按时间可分为短时保活和长时间保活 短时保活的方式通过beginBackgroundTaskWithN...

  • iOS 后台收到推送语音播报

    iOS App后台保活[http://www.cocoachina.com/articles/896173]iOS...

  • iOS App后台保活

    级别:★☆☆☆☆标签:「iOS App 后台保活」「BackgroundTasks」「后台下载资源」作者: WYW...

  • ios 后台保活

    后台保活就是在给APP添加了后台播放音乐的功能,需要在info.plist里面配置UIBackgroundMode...

  • iOS - 后台保活

    一、正常延长版保活(时间3min-40min不等) 二、永久版保活

  • iOS 后台保活

    一想到后台保活,我们最常见的就是音乐播放软件了,那在我们不是音乐软件的情况下我们要如何后台保活呢? 首先我们就要在...

  • ios后台保活

    原理:1.开启后台任务权限,播放音乐 2.app后台后,开启后台任务,定时轮询后台剩余时间,低于20秒时候再申请新...

  • iOS后台保活

    问题描述: app需要在收到MQTT消息的时候震动30秒和通知栏展示本地通知,一分钟之后移除通知栏消息,这必然涉及...

  • iOS蓝牙后台保活

    Xcode设置如图: 在实践中,主要的开发流程有以下: 新建Central Manager实例并进行监听蓝牙设备状...

网友评论

      本文标题:iOS - 后台保活

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