简体   繁体   English

推送Swift iOS 10的通知

[英]Push Notifications for Swift iOS 10

I'm struggling to get push notifications to work with Swift with iOS 10. Registering seems to be going through successfully, but creating a notificaiton does nothing on the device and returns a nil error. 我正在努力获得推送通知与Swift一起使用iOS 10.注册似乎成功通过,但创建通知在设备上什么都不做,并返回零错误。 Any ideas what I'm missing? 我缺少什么想法?

import Foundation
import UIKit
import UserNotifications
class SPKPushNotifications
{
    class func register(application:UIApplication){
        if #available(iOS 10.0, *) {
            let center = UNUserNotificationCenter.current()
            center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
                // Enable or disable features based on authorization.
                application.registerForRemoteNotifications()
            }
        } else {
            let notificationTypes: UIUserNotificationType = [UIUserNotificationType.alert, UIUserNotificationType.badge, UIUserNotificationType.sound]
            let pushNotificationSettings = UIUserNotificationSettings(types: notificationTypes, categories: nil)
            application.registerUserNotificationSettings(pushNotificationSettings)
            application.registerForRemoteNotifications()
        }
    }
    class func unregister(application:UIApplication){
        application.unregisterForRemoteNotifications()
    }
    class func create(title:String, body:String, delay:Double, repeats:Bool){
        if #available(iOS 10.0, *) {
            let content = UNMutableNotificationContent()
            content.title = title
            content.body = body
            content.sound = UNNotificationSound.default() //idk if we're gonna want something else
            content.badge = NSNumber(value:UIApplication.shared.applicationIconBadgeNumber+1)
            let trigger = UNTimeIntervalNotificationTrigger(timeInterval:delay, repeats:repeats)
            let request = UNNotificationRequest(identifier:title, content:content, trigger:trigger)
            let center = UNUserNotificationCenter.current()
            center.add(request){ (error) in
                print(error)
            }

        } else {
            // Fallback on earlier versions
        }
    }
    class func delete(){
        if #available(iOS 10.0, *) {
            let center = UNUserNotificationCenter.current()
            center.removeAllDeliveredNotifications()
        } else {
            // Fallback on earlier versions
        }
    }
}

You won't see the notification if the application is in the foreground. 如果应用程序位于前台,您将看不到通知。 Try adding the request to the notification center when the application is in the background. 当应用程序在后台时,尝试将请求添加到通知中心。

You can do (for this test only) that by adding a few second sleep and moving your application to the background. 您可以通过添加几秒钟睡眠并将应用程序移至后台来进行(仅适用于此测试)。 Or scheduling the notification to a later time when the application is not running in the foreground. 或者将通知安排到应用程序未在前台运行的稍后时间。

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

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