iOS推送通知已启用但不起作用

时间:2018-08-22 15:46:52

标签: ios swift xcode push-notification apple-push-notifications

我正在尝试在我的应用中设置推送通知。

我已启用我的APN证书。 enter image description here

我已经在Xcode中打开了推送通知。 enter image description here

我已经在以前的xcode项目中启用了推送通知,并且工作正常。这是我的代码。

 //add push notifications every 5 hours
func pushNotifications() {
    let pushNotification = UNMutableNotificationContent()
    pushNotification.title = "Time to track your water useage!"
    pushNotification.badge = 1

    let minute:TimeInterval = 60.0
    let hour:TimeInterval = 60.0 * minute
    let eighthDay:TimeInterval = 5 * hour

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: eighthDay, repeats: true)
    let request = UNNotificationRequest(identifier: "timerDone", content: pushNotification, trigger: trigger)
    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}

我在我的viewdidload函数中称呼它

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

您可以尝试使用此代码,

func sendLocalPush() {
    let objNotificationContent = UNMutableNotificationContent()
    objNotificationContent.title = "Manish Kumar"
    objNotificationContent.body = "iOS Developer"
    objNotificationContent.userInfo = getUserInfoDict()
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1.0, repeats: false)
    let request = UNNotificationRequest(identifier: "LocalNotification", content: objNotificationContent, trigger: trigger)
    /// 3. schedule localNotification
    let center = UNUserNotificationCenter.current()
    center.add(request, withCompletionHandler: {(_ error: Error?) -> Void in
    })
}

希望这会有所帮助。