我在iOS8应用中实现了推送通知。收到通知后,我正在尝试播放音频文件。
当应用程序位于前台时,代码正在播放音频,但当应用程序在后台时,没有任何反应。
我尝试重新生成证书和配置文件。我确保应用程序在后台运行,即用户没有刷过去除它。在后台模式中,我启用了远程通知,后台提取和音频&点播。
我添加了AppDelegate.m
文件中的代码段:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// More code here ---------------------------------------------------
if (launchOptions) {
NSDictionary *userInfo = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
if (apsInfo) { //apsInfo is not nil
[self performSelector:@selector(playCarAlarmAudio)
withObject:nil
afterDelay:1];
}
}
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
[[UIApplication sharedApplication] registerForRemoteNotifications];
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge
|UIUserNotificationTypeSound
|UIUserNotificationTypeAlert) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
// More code here ---------------------------------------------------
}
处理推送通知的委托方法:
-(void) application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"Failed to register for push");
}
-(void) application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
}
-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[self respondToEventNotification:userInfo];
}
-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// [self respondToEventNotification:userInfo];
[self playAlarmAudio];
}
-(void) respondToEventNotification : (NSDictionary *) userInfo {
if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) {
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
[localNotification setSoundName:@"alarm.mp3"];
[localNotification setFireDate:[NSDate date]];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
else if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive) {
[self playAlarmAudio];
}
}
播放闹钟:
-(void) playAlarmAudio {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"alarm" ofType:@"mp3"];
NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:nil];
self.audioPlayer.numberOfLoops = 1;
[self.audioPlayer play];
}
答案 0 :(得分:0)
根据以下Apple文档,要播放的通知声音在通知有效内容字典(https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html)中指定:
通知有效负载
每个远程通知都包含一个有效负载。有效负载包含有关系统应如何提醒用户以及您提供的任何自定义数据的信息。在iOS 8及更高版本中,通知有效负载允许的最大大小为2千字节; Apple推送通知服务拒绝任何超出此限制的通知。 (在iOS 8和OS X之前,最大有效负载大小为256字节。)
对于每个通知,编写一个JSON字典对象(由RFC 4627定义)。该字典必须包含由密钥aps标识的另一个字典。 aps字典可以包含一个或多个指定以下用户通知类型的属性:
- 要向用户显示的提醒消息
- 使用
标记应用图标的数字- 播放声音