問題描述:
app需要在收到MQTT消息的時(shí)候震動(dòng)30秒和通知欄展示本地通知黍瞧,一分鐘之后移除通知欄消息原杂,這必然涉及到倒計(jì)時(shí),出現(xiàn)的問題是當(dāng)app在后臺(tái)的時(shí)候會(huì)出現(xiàn)會(huì)出現(xiàn)30秒左右的卑蛘埽活被碗,定時(shí)器不走了。兴喂。焚志。酱酬。。
官網(wǎng)鏈接:
解決辦法:
應(yīng)用在后臺(tái)時(shí)可以播放聲音信息汗菜。
可以利用此模式播放無聲音樂挑社,App 進(jìn)入后臺(tái)后,播放無聲音樂菌瘪,配合beginBackgroundTaskWithName對(duì)系統(tǒng)申請(qǐng)后臺(tái)使用時(shí)間阱当,可以使APP在后臺(tái)長時(shí)間保活动猬。
應(yīng)用提供位置信息 應(yīng)用場景:在后臺(tái)時(shí)需要不斷通知用戶位置更新信息表箭。
通過后臺(tái)持續(xù)定位App免钻,可以實(shí)現(xiàn)App后臺(tái)保活凤覆。
以下Demo可以實(shí)現(xiàn)解決這個(gè)問題拆魏,但是發(fā)現(xiàn)在iOS15以上的系統(tǒng)還是存在問題。
https://github.com/QiShare/QiAppRunInBackground
https://gitee.com/msmasker/back-runing-demo
解決iOS15以上系統(tǒng)該問題:
http://www.reibang.com/p/4c02230677f3
以上是Swift寫的拥峦,改寫OC如下:
//
// XTBackRunningManager.m
// XTBackRunningDemo
//
// Created by mshi on 2022/1/20.
//
#import "C2AppBackRunningManager.h"
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface C2AppBackRunningManager ()
@property (nonatomic, strong) AVAudioPlayer *audioPlayer;
@property (nonatomic, strong) AVAudioEngine *audioEngine;
@property (nonatomic,assign) UIBackgroundTaskIdentifier backgroundTaskIdentifier;
@property (nonatomic, strong) NSTimer *applyTimer;
@property (nonatomic, strong) NSTimer *taskTimer;
@end
@implementation C2AppBackRunningManager
+ (instancetype)shareManager {
static C2AppBackRunningManager *manager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[C2AppBackRunningManager alloc] init];
});
return manager;
}
//- (instancetype)init {
// self = [super init];
// if (self) {
//// [self addNoti];
// // 獲取定位權(quán)限
// [self.locationManager requestAlwaysAuthorization];
// [self.locationManager requestWhenInUseAuthorization];
// }
// return self;
//}
- (void)startBackgroundTask: (UIApplication *)app {
self.backgroundTaskIdentifier = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:self.backgroundTaskIdentifier];
self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
[self applyForMoreTime];
}];
[self.applyTimer invalidate];
self.applyTimer = nil;
[self.taskTimer invalidate];
self.taskTimer = nil;
self.taskTimer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector: @selector(doSomething) userInfo:nil repeats:YES];
}
- (void)stopBackgroundTask {
[self.applyTimer invalidate];
self.applyTimer = nil;
[self.taskTimer invalidate];
self.taskTimer = nil;
}
- (void)doSomething {
DLog(@"doing some thing: %f", [UIApplication sharedApplication].backgroundTimeRemaining);
}
- (void)applyForMoreTime {
if ([UIApplication sharedApplication].backgroundTimeRemaining < 30) {
self.backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskIdentifier];
self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
[self applyForMoreTime];
}];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Silence" ofType:@"wav"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
if (!fileURL) {
NSLog(@"playEmptyAudio 找不到播放文件");
}
NSError *error = nil;
if (@available(iOS 11.0, *)) {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback mode:AVAudioSessionModeDefault routeSharingPolicy:AVAudioSessionRouteSharingPolicyDefault options:AVAudioSessionCategoryOptionMixWithOthers error:&error];
} else {
// Fallback on earlier versions
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback mode:AVAudioSessionModeDefault options:AVAudioSessionCategoryOptionMixWithOthers error:&error];
}
// 0.0~1.0,默認(rèn)為1.0
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
[self.audioEngine reset];
[self.audioPlayer play];
[self.audioPlayer stop];
}
}
@end