【问题标题】:Can't able to play an audio after receiving a silent push notification in ios?在 ios 中收到静默推送通知后无法播放音频?
【发布时间】:2017-10-04 11:19:02
【问题描述】:

我正在使用下面的代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.

pushcome=NO;

pushcome=[self registerForPushNotification:launchOptions];


[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
 return YES;
}

以下代码用于接收推送:

-(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 using 'Avplayer' ==>
NSString *finalUrl = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] stringForKey:@"forUrl"]];
    NSURL *url = [NSURL URLWithString:finalUrl];
    playerItem = [AVPlayerItem playerItemWithURL:url];
    player = [AVPlayer playerWithPlayerItem:playerItem];
    player = [AVPlayer playerWithURL:url];
    [player play];
    // <===
    completionHandler(UIBackgroundFetchResultNewData);

NSLog(@"==Hi, I am from silent Notification ========%@",[[NSUserDefaults standardUserDefaults] stringForKey:@"forUrl"]);
    return;
}
else
{

    NSLog(@"User ... %@",userInfo);
        return;
}
}

我收到以下错误:-

[0x37fd8000] >aurioc> 807: failed: '!int' (启用 2, outf inf

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    用于推送通知声音 您需要使用此代码

        SystemSoundID soundID;
        CFBundleRef mainBundle = CFBundleGetMainBundle();
        CFURLRef ref = CFBundleCopyResourceURL(mainBundle,      (CFStringRef)@"mysound.wav", NULL, NULL);
        AudioServicesCreateSystemSoundID(ref, &soundID);
        AudioServicesPlaySystemSound(soundID);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-08
      • 2018-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多