当用户获得推送通知时如何播放自定义声音文件?
我在应用程序包中有一个声音文件,当用户将得到推送通知时,我想播放该声音文件。
如果是的话,是否有可能在iOS上,那么请build议的方式来实现这一点。
谢谢,
要播放此声音,您必须在通知有效内容中指定声音的文件名。 例如,假设您已经将一个名为example.caf的声音文件添加到您的应用程序中,则可以使用通知载荷播放此声音,如下所示:
{ aps = { alert = "test example notification message"; sound = "example.caf"; }; }
然后,当您的通知到达时,自定义声音将播放。
在您的应用程序代理类中使用此方法。
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { UIApplicationState state = [application applicationState]; if (state == UIApplicationStateActive) { NSLog(@"User Info : %@", [userInfo description]); NSLog(@"User Info Alert Message : %@", [[userInfo objectForKey:@"aps"] objectForKey:@"alert"]); NSString *messageString = [NSString stringWithFormat:@"%@", [[userInfo objectForKey:@"aps"] objectForKey:@"alert"]]; NSString *playSoundOnAlert = [NSString stringWithFormat:@"%@", [[userInfo objectForKey:@"aps"] objectForKey:@"sound"]]; NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle] resourcePath],playSoundOnAlert]]; NSError *error; audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; audioPlayer.numberOfLoops = 0; [audioPlayer play]; } }