0
点赞
收藏
分享

微信扫一扫

iOS 推送总结

注册推送

-(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:固定的声音(提前准备好需要的声音文件)
1、声音文件:iOS支持的音频格式主要是aiff,wav,caf,具体文件需要放在mainBundle目录中。音频长度必须要在30秒以内,不然会被系统声音所取代;
2、后台设置推送sound的时候,设置成这个音频文件带后缀的全称就可以了;

####### 场景2:每次推送的声音几乎都需要不同
1、前提,不能像场景1一样准备好声音文件,所以这里需要用到一个能灵活播放声音的类:AVSpeechSynthesizer

  _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、实现静默推送

####### app在杀死状态如何播放 自定义声音

举报

相关推荐

0 条评论