注册推送
-(void)setRemoteNotification{
UIApplication* application = [UIApplication sharedApplication];
//iOS8之后
//#define kVersion [[[UIDevice currentDevice] systemVersion] floatValue]
if (kVersion >= 10) {
[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
//设置行为
UNTextInputNotificationAction *inputAction = [UNTextInputNotificationAction actionWithIdentifier:@"action.input" title:@"回复" options:UNNotificationActionOptionForeground textInputButtonTitle:@"Send" textInputPlaceholder:@"What do you want to say..."];
// // 2
// UNNotificationAction *goodbyAction = [UNNotificationAction actionWithIdentifier:@"action.goodbye" title:@"Goodbye" options:UNNotificationActionOptionForeground];
UNNotificationAction *cancelAction = [UNNotificationAction actionWithIdentifier:@"action.cancle" title:@"取消" options:UNNotificationActionOptionDestructive];
UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"saySomethingCategory" actions:@[inputAction,cancelAction] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
[center setNotificationCategories:[NSSet setWithObject:category]];
[center requestAuthorizationWithOptions:
(UNAuthorizationOptionBadge|
UNAuthorizationOptionSound|
UNAuthorizationOptionAlert)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
MYLog(@"推送成功开启");
}else{
MYLog(@"推送开启失败,应弹出提示");
}
}];
[application registerForRemoteNotifications];
}else if(kVersion >= 8){
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:
(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)
categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
}
}
收到推送
iOS10以前
//前台 收到推送
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler NS_AVAILABLE_IOS(7_0);
iOS10之后
//前台收到推送
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler;
//后台收到推送
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
NSDictionary * userInfo = response.notification.request.content.userInfo;
UNNotificationRequest *request = response.notification.request; // 收到推送的请求
UNNotificationContent *content = request.content; // 收到推送的消息内容
NSNumber *badge = content.badge; // 推送消息的角标
NSString *body = content.body; // 推送消息体
UNNotificationSound *sound = content.sound; // 推送消息的声音
NSString *subtitle = content.subtitle; // 推送消息的副标题
NSString *title = content.title; // 推送消息的标题
}
关于推送的声音播放
苹果默认的推送声音就不说了,需要播放自定义声音的场景很多
1、常用的支付类app
2、个性化推送的需要
####### 场景1:固定的声音(提前准备好需要的声音文件)
1、声音文件:iOS支持的音频格式主要是aiff,wav,caf,具体文件需要放在mainBundle目录中。音频长度必须要在30秒以内,不然会被系统声音所取代;
2、后台设置推送sound的时候,设置成这个音频文件带后缀的全称就可以了;
####### 场景2:每次推送的声音几乎都需要不同
1、前提,不能像场景1一样准备好声音文件,所以这里需要用到一个能灵活播放声音的类:AVSpeechSynthesizer
提供一种将文本合成语音并提供播放控制的类
1、AVSpeechUtterance初始化资源:可以自定义播放文本,语言AVSpeechSynthesisVoice,音调,音速等;
2、AVSpeechSynthesizer,主要用于控制播放,常用其代理方法判断播放状态AVSpeechSynthesizerDelegate
_speech = [[AVSpeechSynthesizer alloc] init];
_speech.delegate = self;
_utterance = [[AVSpeechUtterance alloc] initWithString:strText]; // **** @"您好,您消费金额是:¥10.01"];
AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-TW"];// **** @"zh-CN"
_utterance.voice = voice; // **** 音色-大陆/台湾
_utterance.pitchMultiplier = 1.0;// **** 音调/音高
_utterance.rate = rate; // **** 语速
_utterance.volume = 1.0; // **** 音量/响度
[_speech speakUtterance:_utterance];
####### 场景3:场景1和场景2的结合
在播放语音之前,会有一个类似系统推送 叮 一声的固定声音
问题
#######类似收款类app,程序如何持续在后台收到推送并播放声音
1、打开能够让app在后台保持活跃的方式
[图片上传失败...(image-8b049c-1514514246095)]
2、在Appdelegate 里添加后台任务代码
//程序即将被杀死的代理方法,开启后台任务
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
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;
}
});
});
}
//开启音频的循环播放
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSError *setCategoryErr = nil;
![Uploading WechatIMG395_980830.jpeg . . .] NSError *activationErr = nil;
[[AVAudioSession sharedInstance]
setCategory: AVAudioSessionCategoryPlayback
error: &setCategoryErr];
[[AVAudioSession sharedInstance]
setActive: YES
error: &activationErr];
return YES;
}
3、实现静默推送
静默推送:后台远程推送,允许应用在收到通知后在后台运行一段代码,且能够马上执行- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler 代理方法的一种推送方式
实现: 后台发推送的时候,加一个字段 content-available = 1,如此,设备在收到此类推送时,也能执行收到推送的代码了,就可以播放推送内容里面传入的声音内容了。[图片上传失败...(image-943ebd-1514514246095)]
####### app在杀死状态如何播放 自定义声音