【问题标题】:UNNotification: Custom Sound for LocalNotification is not playing in iOS10UNNotification:LocalNotification 的自定义声音在 iOS10 中没有播放
【发布时间】: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文件?
  • 是的,它存在于我的包中,并添加到目标 - 复制包资源
  • 尝试从设备中删除应用程序,清理并再次运行设备中的应用程序。
  • 奇怪,但它有效..!谢谢..@金刚狼
  • 我已将我的评论更新为答案。如果它解决了您的问题,请接受它作为答案,以便对其他用户有所帮助。

标签: ios unnotificationrequest


【解决方案1】:

尝试从设备中删除应用,清理并在设备上再次运行应用。

有时,资源没有正确更新;我认为这是你的问题。

【讨论】:

    【解决方案2】:

    不支持长音频文件。

    您的文件是否超过 30 秒?如果是,它将不起作用。只有短于 30 秒的文件可用。如果文件超过 30 秒,则将替换为默认声音。

    UNNotificationSound documentation

    【讨论】:

    猜你喜欢
    • 2017-01-16
    • 2013-10-20
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    • 2017-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多