我有一个声音文件在应用程序包中,我想在用户收到推送通知时播放该声音文件。
在iOS中是否可能?如果是,请建议实现此目标的方法。
谢谢。
在iOS中是否可能?如果是,请建议实现此目标的方法。
谢谢。
若要播放此声音,您必须在通知载荷中指定声音文件的文件名。例如,假设您已经将一个名为example.caf的声音文件添加到应用程序中,则可以使用以下通知载荷来播放此声音:
{
aps =
{
alert = "test example notification message";
sound = "example.caf";
};
}
当您收到通知时,自定义声音将播放。
NSURL *url = create path
即可。但是,如果您无法将有效负载与设置同步,则表示当您从应用程序设置更改声音文件时,您的有效负载sound =“example.caf”
不会更改。您必须在本地应用逻辑以满足您的需求。 - Bhavin_msound =“example.caf”
,那么即使应用程序设置了不同的.caf
文件作为通知声音,应用程序也会播放“example.caf”文件? - Matrosov Oleksandr在你的应用代理类中使用此方法。
-(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];
}
}