前言:
本教程不讨论极光推送的集成,请自行百度如何集成极光推送
本教程适用于需要支持ios10以下的后台推送响铃
对于IOS以上的系统可以了解通知扩展(Notification Extension),一个非常强大的东西,本教程不介绍。
本教程的代码全部都放在AppDelegate.m文件里面
需求:
根据推送过来的内容,需要进行响铃提醒操作。即对于重要的推送要通过响铃要提醒用户。
思路介绍:
1.APP在前台,我们在接受通知的函数里面直接调用播放声音即可
2.APP在后台,我们需要先支持后台播放,之后让APP进入后台之后执行一个长期任务,让后台处于活跃状态,否则不会播放声音和语音播报
3.对于后台接受通知,会调用通知函数,当用户点击通知进入APP 会再调用一次通知函数,我们需要避免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处于活跃状态
- (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.推送处理函数
//收到推送信息 该API适用于IOS7以下的系统
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[JPUSHService handleRemoteNotification:userInfo];
}
//收到推送信息 该API适用于IOS7以上的系统
- (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];
//程序当前正处于前台收到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推送来的消息内容
//后台通知事件
currentMsgId = userInfo[@"_j_msgid"];
[self alarmClockEventWithHandler:^{
completionHandler(UIBackgroundFetchResultNewData);
}];
}
}