【问题标题】:iOS push notifications enabled but not workingiOS 推送通知已启用但无法正常工作
【发布时间】:2018-08-22 15:46:51
【问题描述】:

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

我已启用我的 APN 证书..

我已经在 Xcode 中打开了推送通知..

我在以前的 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 函数中调用它

我做错了什么?

【问题讨论】:

  • UNMutableNotificationContent 创建一个本地通知。这不是推送通知。
  • @vadian 哦。那为什么我的本地通知不起作用?
  • 你需要实现 willPresent 并返回所需的类型,因为当应用程序在前台时通知不会显示默认值,还要确保你请求了权限,看这里thomashanning.com/…
  • 谢谢@Sh_Khan 我会检查一下

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


【解决方案1】:

你可以试试这段代码,

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

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2021-08-09
    • 2016-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-30
    • 2011-06-16
    相关资源
    最近更新 更多