IOS: Objective -C How do I change the alert sound payload when an app is in the background?
In my application, I am letting the user change the sound for push notification
.
When the user selects Sound-A
or Sound-B
, it plays correctly when the app is in active mode.
But when the app is in background mode
, it always plays Sound-A
because it is push notification payload sound
set toSound-A.
{
"aps": {
"alert": "My News!",
"sound": "sound_a.mp3",
}
}
How can I overwrite it with user selected sound.
I recorded the selected audio in app preferences
and I tried to replace it but it doesn't work.
NSString *soundName = [self getSoundTrack];
[pushArray setObject:soundName forKey:@"sound"];
source to share
you can play sound alert in the background using AVplayer. Work great in my application.
@property (strong, nonatomic) AVAudioPlayer *audioPlayer;
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
_audioPlayer=nil;
if(application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground ){
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"soundName"
ofType:@"mp3"]];
NSError *error;
_audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:&error];
if (error)
{
NSLog(@"Error in audioPlayer: %@",
[error localizedDescription]);
} else {
[_audioPlayer prepareToPlay];
}
[_audioPlayer play];
}
source to share
If you want to programmatically change the sound using iOS client code, you can send a silent push notification server instead, and then send a local notification with a customized sound when your app wakes up in the background.
However, the simpler solution is what others say. Refresh the server when user changes sound settings.
source to share