How do I get my sound to play when a remote notification is received?

我的未来我决定 提交于 2019-11-30 23:52:47

If you call AVAudioSession:setActive:error: and AVAudioSession:setCatagory:withOptions:error: and create your AVPlayer before going into the background, you can then play your sound. For example:

    // Make sure this is run BEFORE entering background.
    [[AVAudioSession sharedInstance] setActive:YES error:&error];
    if(error != nil){
        NSLog(@"ERROR: %@", error.localizedDescription);
    }
    else {
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&error];
        if(error != nil){
            NSLog(@"ERROR: %@", error.localizedDescription);
        }
        else {
            self._player = [[AVAudioPlayer alloc] initWithContentsOfURL:self.urlOfFileToPlay error:&error];  
            if(error != nil){
                [[AVAudioSession sharedInstance] setActive:NO error:&error];
                NSLog(@"ERROR: %@", error.localizedDescription);
            }
        }
    }
    //You can then do this in application:didReceiveRemoteNotification:fetchCompletionHandler:
    [backgroundPlayer.player play]; 

Of course I am assuming that you already have the Capabilities for Background Modes set for Background fetch and Remote notifications.

I will also warn that if you call AVAudioSession:setActive:error: too early, other applications might change things on you.

Also, if you set your application NOT to run in the background (Info.plist: <key>UIApplicationExitsOnSuspend</key> <true/>), when it receives a "Notification" (local or remote) and you have activated an AVAudioSession, like the above code, it will play the sound in the notification payload regardless of if the application is set to silent or vibrate mode, which is actually how alarms do it. Of course this will not work if you need to poll the server in response to a remote notification. See here.

You cannot activate your audio session in the background, and you cannot initiate sound playback in the background. Background audio simply means that you can keep playing at the time your app goes into the background; but once your background sound has been interrupted, or if your app wasn't playing when you went into the background, you have no ability to make a sound.

And this makes sense, because it would be terrible if apps the user isn't using, and may not even be aware of, could start making noise from out of nowhere.

The usual thing is either to come to the foreground or to hand the system an immediate local notification that has a sound.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!