首页 文章

推送通知:没有声音

提问于
浏览
6

我已经在我的应用程序中使用Parse.com实现了推送通知系统,一切都很棒!

我唯一的问题是,当通知到达时:它不播放任何声音!

我进入设置(在我的平板电脑中),在通知下,我看到:

enter image description here

当你看到“声音”和“徽章”关闭时 . 如果我打开它们然后当推送通知到达时:它播放声音!

所以...我希望在安装我的应用程序时,这两个选项默认为TRUE .

这可能吗?我怎样才能做到这一点?

我在Swift工作,到目前为止这是我的代码:

method didFinishLaunchingWithOptions

var pushSettings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge , categories: nil)
    application.registerUserNotificationSettings(pushSettings)
    application.registerForRemoteNotifications()

非常感谢帮助我

4 回答

  • 4
    UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Badge | UIUserNotificationType.Sound | UIUserNotificationType.Alert, categories: nil))
    

    你必须设置soundName,因为默认是没有声音:

    对于此属性,请在应用程序的主程序包或UILocalNotificationDefaultSoundName中指定声音资源的文件名(包括扩展名)以请求默认系统声音 . 当系统显示本地通知的警报或标记应用程序图标时,它会播放此声音 . 默认值为nil(无声音) . 不支持持续时间超过30秒的声音 . 如果您指定的声音播放时间超过30秒,则会播放默认声音 .

    yourNotification.soundName = UILocalNotificationDefaultSoundName
    

    soundName

    对于远程通知,您需要使用The Notification Payload

  • 0

    与UIUserNotificationSettings类似,尝试使用RemoteNotification .

    例如,在Objective-C中:

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    
  • 0

    我想这只是你在这个精确设备上遇到的问题 . 在另一个上试一试,看看它是否有所不同 .

  • 0

    如果您希望为通知播放IOS标准声音,则需要将 content.sound 设置为 UNNotificationSound.default() 您可以在日程表功能中执行此操作,就像我在此处所做的那样:

    func schdule(date:Date,repeats:Bool)->Date?{
        let content = UNMutableNotificationContent()
        content.sound = UNNotificationSound.default()
        content.title = "Title"
        content.body = "blablablabla"
        content.setValue("YES", forKeyPath:"shouldAlwaysAlertWhileAppIsForeground")
    
        let trigger = UNCalendarNotificationTrigger(dateMatching: yourDateComponents,repeats: repeats)
        let request = UNNotificationRequest(identifier: "TestNotification", content: content, trigger: trigger)
    
        UNUserNotificationCenter.current().add(request) { (error:Error?) in
            if error == nil {
                print("Notification Schduled",trigger.nextTriggerDate()?.formattedDate ?? "Date nil")
            } else {
                print("Error schduling a notification",error?.localizedDescription ?? "")
            }
        }
        return trigger.nextTriggerDate()
    }
    

相关问题