iOS无法播放远程通知的自定义声音

问题描述:

我一直在尝试在iOS上实现通知的自定义声音。iOS无法播放远程通知的自定义声音

根据苹果的例子3中的文档here。所有我们需要做的是确保推送通知有效载荷应该是这样的:

{ 
"aps" : { 
     "alert" : "You got your emails.", 
     "badge" : 9, 
     "sound" : "bingbong.aiff" 
    } 
} 

而且bingbong.aiff已被放置在应用程序包。

此外,如果应用程序处于活动状态,有人建议我添加以下代码:

- (void)application:(UIApplication *)application 
didReceiveRemoteNotification:(NSDictionary *)userInfo 
{ 
    if (application.applicationState == UIApplicationStateActive) 
    { 
     NSString *path = [[NSBundle mainBundle] pathForResource:@"bingbong" ofType:@"aiff"]; 
     theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]error:NULL]; 

     theAudio.delegate = self; 
     [theAudio play]; 
    } 
} 

"bingbong.aiff"被放置在"SupportingFiles"

我仍然DONOT获取远程通知的自定义音效。

我已经经历了类似于这个的其他SO问题,但这些与我正在做的没有什么不同。

  1. How to play custom sound file when user get push notification?

  2. Custom sounds in remote notifications iOS 10, swift 3

  3. Change push notification sound

+0

显示你的脚本如何从服务器发射的通知。 – vaibhav

+0

当应用程序打开并且您收到Push时,函数'didReceiveRemoteNotification'被调用,您是否在应用程序打开时听到声音? – iphonic

+0

@iphonic不,当应用程序打开时,我听不到任何声音。默认声音在后台播放。 – Ichthyocentaurs

默认声音:

根据苹果文档&准则发挥默认的声音,你必须送“默认“在声音文件中的价值。其他方面它也不会播放默认声音。

对于自定义音效

你需要到位的default通过自定义的声音文件的名称您php script里面**。

  1. 对于自定义声音文件必须在Xcode项目 资源存在。
  2. 确保声音文件持续时间不超过30秒。否则 它会播放默认声音。
  3. 而且您必须在.plist文件中添加以下密钥UILocalNotificationDefaultSoundName 。见下图:

enter image description here

+0

.mp3文件也可以在这种情况下工作,我尝试了上面的但不起作用。 – Ichthyocentaurs

+0

其工作正常,我在这里支持'.waw',或者你可以尝试一些由苹果支持的扩展,可能'.mp3'不支持,请仔细检查你的PHP脚本,可能会导致问题。 – vaibhav

+0

我将文件放置在项目中的方式有​​问题吗?我刚刚添加了条目“UILocalNotificationDefaultSoundName” - 字符串 - “customsound.aiff”。我通过菜单选项“文件”>“添加文件到项目”将Xcode中的声音文件添加到文件夹Supporting Files – Ichthyocentaurs

didReceiveRemoteNotification取出AVPlayer东西。

确保:

  • 的声音长度为<25秒
  • 的文件确实是在支持的格式(AIFF,WAV,CAF)
  • 文件确实存在whitin你的包一个(复制包资源)
  • 您注册推送通知:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
        let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil) 
        UIApplication.sharedApplication().registerUserNotificationSettings(settings) 
        UIApplication.sharedApplication().registerForRemoteNotifications() 
    
        return true 
    } 
    
  • 名称完全一致的文件名:

    { 
        "aps" : { 
         "alert" : "message", 
         "badge" : 1, 
         "sound" : "name.aiff" 
        } 
    } 
    
+0

我删除了AVPlayer代码,并且已经在applicationDidFinishLaunching中有权限,但仍然没有解析 – Ichthyocentaurs