I want to implement an autoplay audio feature when silent push receive. I am receiving an audio URL when silent push come.I want to play an audio when app in background via receiving silent push. But my problem is that, I am successfully receiving audio URL via silent push, but I am not able to play audio when app in background mode. I am using "STKAudioPlayer" for playing an audio, which is downloaded from GitHub . I am implementing below code for playing an audio
NSString *finalUrl = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] stringForKey:@"forUrl"]];
[stkaudioPlayer play:finalUrl];
And below code is for receiving a silent push notification and here I am implementing to playing an Audio.
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
NSLog(@"BACK GROUND MODE PUSH");
if([userInfo[@"aps"][@"content-available"] intValue]== 1) //it's the silent notification
{
[self saveInUserDefault];
stkaudioPlayer = [[STKAudioPlayer alloc]init]; //player initialize
//Save audio url
NSString *str= [userInfo valueForKey:@"AudioURL"];
[[NSUserDefaults standardUserDefaults]setObject:str forKey:@"forUrl"];
[[NSUserDefaults standardUserDefaults] synchronize];
// for Playing an audio
NSString *finalUrl = [NSString stringWithFormat:@"%@",str];
[stkaudioPlayer play:finalUrl];
completionHandler(UIBackgroundFetchResultNewData);
// NSLog(@"I am Here");
return;
}
else
{
NSLog(@"User ... %@",userInfo);
return;
}}
If my implementation is wrong way, so please help me out with this topic.