【问题标题】:Missing badge and sound on local notification swift本地通知swift上缺少徽章和声音
【发布时间】:2018-05-26 15:03:56
【问题描述】:

我正在尝试在我的应用程序中向本地通知添加徽章和声音,但我没有收到任何消息。 我看到通知应该出现但没有任何声音或徽章...... 谁能告诉我我做错了什么?

func requestUserPermissionForNotifications(){
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: {didAllow, error in
        if (error != nil) {
            print("Error authorzing notifications")
        } else {
            if (didAllow) {
                self.setDailyNotifications()
                print("User allowed to Push notifications")
            } else {
                print("User did not allow to Push notifications")
            }
        }
    })
}

func setDailyNotifications() {
    var sunday = DateComponents()
    sunday.hour = 11
    sunday.minute = 00
    sunday.weekday = 1
    timeNotification(id: "Sunday", notificationTitle: "Daily reward is waiting", notificationText: "Log in and get your daily reward", timeOfNotification: sunday)
}

func timeNotification(id: String, notificationTitle: String, notificationText: String, timeOfNotification: DateComponents) {
    let trigger = UNCalendarNotificationTrigger(dateMatching: timeOfNotification, repeats: true)
    let content = UNMutableNotificationContent()
    content.title = notificationTitle
    content.body = notificationText
    content.sound = UNNotificationSound.default()
    content.badge = 1

    let request = UNNotificationRequest(identifier: id, content: content, trigger: trigger)

    UNUserNotificationCenter.current().add(request) { (error) in
        if (error != nil) {
            print("Error adding notification \(id) --- \(String(describing: error))")
        }
    }
}

【问题讨论】:

  • 您是否检查了设备上的设置?
  • 我做了,声音和徽章是允许的。

标签: ios swift notifications


【解决方案1】:

问题是我从 requestAuthorization() 方法调用了我的 setDailyNotifications() 函数。如果用户批准推送通知,我所做的只是从 viewDidLoad 调用 setDailyNotifications() 。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    • 2021-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多