前言:
本教程不討論極光推送的集成低零,請自行百度如何集成極光推送
本教程適用于需要支持ios10以下的后臺推送響鈴
對于IOS以上的系統(tǒng)可以了解通知擴展(Notification Extension)郑象,一個非常強大的東西,本教程不介紹隆豹。
本教程的代碼全部都放在AppDelegate.m文件里面
需求:
根據(jù)推送過來的內(nèi)容,需要進行響鈴提醒操作爹耗。即對于重要的推送要通過響鈴要提醒用戶奔滑。
思路介紹:
1.APP在前臺,我們在接受通知的函數(shù)里面直接調(diào)用播放聲音即可
2.APP在后臺顺少,我們需要先支持后臺播放朋其,之后讓APP進入后臺之后執(zhí)行一個長期任務(wù),讓后臺處于活躍狀態(tài)脆炎,否則不會播放聲音和語音播報
3.對于后臺接受通知梅猿,會調(diào)用通知函數(shù),當(dāng)用戶點擊通知進入APP 會再調(diào)用一次通知函數(shù)秒裕,我們需要避免2次播放
步驟
1.配置Capabilities
2.支持后臺播放
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
return YES;
}
3.后臺保存活躍
//為了讓后臺接受推送時響鈴 需要讓app處于活躍狀態(tài)
- (void)applicationDidEnterBackground:(UIApplication *)application {
UIApplication* app = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier bgTask;
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
if (bgTask != UIBackgroundTaskInvalid)
{
bgTask = UIBackgroundTaskInvalid;
}
});
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
if (bgTask != UIBackgroundTaskInvalid)
{
bgTask = UIBackgroundTaskInvalid;
}
});
});
}
4.播放自定義的聲音
//對于用戶的定義的警告 要通過響鈴
- (void)alarmClockEventWithHandler:(void (^)())completionHandler {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
if(!self.player) {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Thunder Song" ofType:@"m4r"];
self.player = [[AVAudioPlayer alloc] initWithData:[NSData dataWithContentsOfFile:filePath] error:nil];
self.player.numberOfLoops = -1;
self.player.volume = 1.0;
}
[self.player stop];
if([self.player prepareToPlay]) {
[self.player play];
completionHandler();
}
}
5.推送處理函數(shù)
//收到推送信息 該API適用于IOS7以下的系統(tǒng)
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[JPUSHService handleRemoteNotification:userInfo];
}
//收到推送信息 該API適用于IOS7以上的系統(tǒng)
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
[JPUSHService handleRemoteNotification:userInfo];
[self.player stop];
__weak typeof(self)weakSelf = self;
UIWindow *alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
alertWindow.rootViewController = [[UIViewController alloc] init];
alertWindow.windowLevel = UIWindowLevelAlert + 1;
[alertWindow makeKeyAndVisible];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"%@",userInfo[@"msg_content"]] preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"朕知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[weakSelf.player stop];
}];
[alertController addAction:okAction];
//程序當(dāng)前正處于前臺收到apns通知
if (application.applicationState == UIApplicationStateActive) {
//通過 userInfo[@"_j_msgid"]]來避免用戶點擊進入APP兩次響鈴問題
if([currentMsgId isEqualToString:userInfo[@"_j_msgid"]]) {
[self.player stop];
//用戶點擊了通知進入前臺
completionHandler(UIBackgroundFetchResultNewData);
} else {
//用戶在前臺沒點通知
currentMsgId = userInfo[@"_j_msgid"];
[alertWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
[self alarmClockEventWithHandler:^{
completionHandler(UIBackgroundFetchResultNewData);
}];
}
} else {
//這個userInfo就是jpush推送來的消息內(nèi)容
//后臺通知事件
currentMsgId = userInfo[@"_j_msgid"];
[self alarmClockEventWithHandler:^{
completionHandler(UIBackgroundFetchResultNewData);
}];
}
}