简体   繁体   English

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

[英]iOS push notifications enabled but not working

I am trying to set push notifications in my app. 我正在尝试在我的应用中设置推送通知。

I have enabled my APN certificates.. 我已启用我的APN证书。 在此处输入图片说明

And I have turned push notifications on in Xcode.. 而且我已经在Xcode中打开了推送通知。 在此处输入图片说明

I have turned push notifications on in previous xcode projects and it worked fine. 我已经在以前的xcode项目中启用了推送通知,并且工作正常。 This is the code I have.. 这是我的代码。

 //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)
}

I am calling this in my viewdidload func 我在我的viewdidload函数中称呼它

What am I doing wrong? 我究竟做错了什么?

You can try this code, 您可以尝试此代码,

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
    })
}

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM