【发布时间】:2017-03-06 01:53:02
【问题描述】:
我正在触发本地通知。由于 UILocalNotification 类在 iOS 10 中已弃用,因此我使用了 UserNotifications.framework。
当我尝试为通知设置自定义声音时,默认声音一直播放。
这是我的代码:
- (IBAction)fireLocalNotification:(id)sender {
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:@"Hello!" arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:@"Hello_message_body"
arguments:nil];
content.sound = [UNNotificationSound soundNamed:@"sound.mp3"];
// Deliver the notification in five seconds.
UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:5 repeats:NO];
UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"
content:content trigger:trigger];
// Schedule the notification.
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError *error){
if(!error){
NSLog(@"Completion handler for notification");
}
}];
}
我的声音很好; sound.mp3 存在于项目包本身中。
更多信息: https://developer.apple.com/reference/usernotifications/unusernotificationcenter?language=objc
【问题讨论】:
-
你的项目-目标-复制捆绑资源中是否添加了.mp3文件?
-
是的,它存在于我的包中,并添加到目标 - 复制包资源
-
尝试从设备中删除应用程序,清理并再次运行设备中的应用程序。
-
奇怪,但它有效..!谢谢..@金刚狼
-
我已将我的评论更新为答案。如果它解决了您的问题,请接受它作为答案,以便对其他用户有所帮助。