当应用程序在前台时,IOS 在收到推送通知时播放声音

标签 ios push-notification apple-push-notifications

当应用程序处于前台状态时,如何在收到推送通知时播放声音。

我的代码:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if (application.applicationState == UIApplicationStateActive)
    {
        //create an audio player and play the sound

        NSString *path = [[NSBundle mainBundle] pathForResource:@"whistle" ofType:@"caf"];
        NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path];
        AVAudioPlayer *theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:NULL];
        theAudio.volume = 1.0;
        [theAudio prepareToPlay];    
        [theAudio play];
    }

    NSLog(@"didReceiveRemoteNotification %@",userInfo);    
}

最佳答案

安装了AVPLayer,你应该试试下面的代码:

添加以下框架

#include <AudioToolbox/AudioToolbox.h>

并使用这段代码,

    SystemSoundID soundID;
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef ref = CFBundleCopyResourceURL(mainBundle, (CFStringRef)@"mySoundName.wav", NULL, NULL);
    AudioServicesCreateSystemSoundID(ref, &soundID);
    AudioServicesPlaySystemSound(soundID);

关于当应用程序在前台时,IOS 在收到推送通知时播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24884435/

相关文章:

ios - 使用什么变量名

ios - 为什么通知会自动从 iOS 通知中心删除?

ios - 将 apns token 与字符串进行比较?

ios - CLActivityType 值之间的差异 - iOS SDK

ios - FQL 查询以查看用户是否喜欢某个帖子

ios - AudioRingBuffer::GetTimeBounds 中的 EXC_BAD_ACCESS

iOS 通知动态操作类别

c# - WP7 推送通知服务器设置

swift - 使用堆栈和导航栏从 AppDelegate 推送到 ViewController

ios - 更改推送通知横幅中的警报语言