记录一个遇到的坑。
需求:收到推送后播放本地音频;
问题:NotificationServiceExtension不工作;
坑:音频文件没有添加到NotificationServiceExtension target.
在NotificationServiceExtension target的didReceiveNotificationRequest:withContentHandler:方法中添加一下代码,以播放本地音频文件:
SystemSoundID soundID;
NSString *path = [[NSBundle mainBundle] pathForResource:@"my_sound_file.wav" ofType:nil];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound(soundID);
/*AudioServicesPlaySystemSoundWithCompletion*/
AudioServicesPlayAlertSoundWithCompletion(soundID, ^{
self.contentHandler(self.bestAttemptContent);
});
结果发现执行下面一行代码,
NSString *path = [[NSBundle mainBundle] pathForResource:@"my_sound_file.wav" ofType:nil];
从bundle里获取音频文件路径时导致NotificationServiceExtension"退出"(exit with 0),结果NotificationService类对收到通知的处理无效,系统显示默认的通知内容。
处理方法:发现音频文件只添加到了主APP的target,但是没有添加到NotificationServiceExtension target。两个target全部勾选就可以了。
1524104181929.jpg
网友评论