gpt4 book ai didi

iOS:Objective -C 如何在应用程序处于后台模式时更改推送通知声音有效负载?

转载 作者:搜寻专家 更新时间:2023-10-30 20:24:03 24 4
gpt4 key购买 nike

在我的应用程序中,我允许用户更改推送通知的声音。

当用户选择 Sound-ASound-B 时,它会在应用处于事件模式时正常播放。

但当应用处于后台模式时,它总是播放Sound-A,因为推送通知负载声音设置为Sound- A.

{
"aps": {
"alert": "My News!",
"sound": "sound_a.mp3",
}
}

如何用用户选择的声音覆盖它。

我已经在 app preferences 中存储了用户选择的声音,我尝试替换它,但它不起作用。

NSString *soundName = [self getSoundTrack];
[pushArray setObject:soundName forKey:@"sound"];

最佳答案

您可以使用 AVplayer 在后台播放通知声音..在我的应用程序中工作正常

@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];
}

关于iOS:Objective -C 如何在应用程序处于后台模式时更改推送通知声音有效负载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44491927/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com