推送通知:无声音

6
我使用Parse.com在我的应用程序中实现了推送通知系统,一切都很好!
但我的唯一问题是,当通知到达时,它不会播放任何声音!
我打开设置(在我的平板电脑上),在通知下面,我看到这个:
如您所见,“声音”和“徽章”都关闭了。如果我将它们打开,那么当推送通知到达时:它会播放声音!
所以...我希望在安装我的应用程序时,这两个选项默认为TRUE。
是否有可能?我该怎么做?
我正在使用Swift编写代码,以下是我目前的代码:
方法 didFinishLaunchingWithOptions
 var pushSettings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge , categories: nil)
    application.registerUserNotificationSettings(pushSettings)
    application.registerForRemoteNotifications()

非常感谢您的帮助!
4个回答

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

因为默认没有声音,所以您需要设置soundName:

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

yourNotification.soundName = UILocalNotificationDefaultSoundName

soundName(声音名称)是一个与本地通知相关的属性。

对于远程通知,您需要使用通知负载(Notification Payload)


http://stackoverflow.com/questions/27348209/user-set-time-for-notification-in-swift/27349421#27349421 - Leo Dabus
如果您编辑您的问题并发布您的代码,我可以尝试帮助您。 - Leo Dabus
你回答了他如何设置通知的声音,但那不是他问的问题。声音已经设置好了,但在注册用户设置时,“声音”选项没有被打开。 - JAB
抱歉,就像我说的那样,这里不会发生这种情况。它会激活所有的东西。 - Leo Dabus
我在模拟器上遇到了同样的问题,这也是我找到这个问题的原因。与上面相同的代码,在设备上可以工作,但只能在模拟器上批准警报(没有声音或标记)。目前还没有找到任何解决方案。 - JAB
显示剩余5条评论

1
如果您希望播放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()
}

如果您已经创建了NotificationManager Swift文件,则需要将此内容添加到其中。否则,我无法帮助您。 - Void2g

0

与UIUserNotificationSettings类似,尝试使用RemoteNotification。

例如,在Objective-C中:

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

在Swift中,使用application.registerForRemoteNotifications()可以实现你想要的功能,但并不能改变当前的情况 :) - ernestocattaneo

0

我猜这只是你在那个设备上遇到的问题。试试在另一个设备上看看是否有所不同。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接