2016-08-02 145 views
1

推送通知没有被发送(或接收)到我正在使用CloudKit订阅的应用程序,因为有很多方法可以向南,我将在下面列出我的'已完成,这可能会帮助其他人有相同的问题,并(希望)会指出我做错了:(Cloudkit没有发送推送通知

具体而言,我试图得到一个安静的通知记录插入(在后台

项目设置我在功能下启用CloudKit,在仪表板中设置记录类型,在功能下启用应用程序的推送通知,并且启用背景模式“remote-notific通货膨胀”。

注册在启动远程通知:

// Register for push notification 
    let settings = UIUserNotificationSettings(forTypes: [.Alert,.Badge,.Sound], categories: nil) 
    application.registerUserNotificationSettings(settings) 

    // Register for remote notification 
    application.registerForRemoteNotifications() 

倾听推送通知注册成功(成功),并创建订阅:

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { 
    NSLog("Remote notifications registered - %@",deviceToken) 
    createSubscriptions() 
} 

创建我的订阅对象,设置它的通知信息,并将其保存到服务器:

private func createSubscriptions(user : CKRecord, completion: (NSError?) ->()) { 
     let messageSubscription = CKSubscription(recordType: "Messages", predicate: NSPredicate(format:"TRUEPREDICATE"), subscriptionID: "Messages", options: .FiresOnRecordCreation) 
     let notificationInfo = CKNotificationInfo() 
     notificationInfo.shouldSendContentAvailable = true 
     notificationInfo.soundName = "" 
     messageSubscription.notificationInfo = notificationInfo 
     CKContainer.defaultContainer().publicCloudDatabase.saveSubscription(subscription) { (_, error) in 
      if let error = error { 
       if error.domain == "CKErrorDomain" && error.code == 15 { 
        NSLog("Already set, ignoring dupe") 
       } 
       else 
       { 
        completion(error) 
        return 
       } 
      } 
      completion(nil) 
     } 
} 

我可以请参阅CloudKit Dashboard中的订阅确实已成功创建。

我然后创建一个从第二设备或仪表板的一些记录(“信息”),看到他们无论是在查询或仪表板调用添加,但该方法我已经得到通知没有被调用:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 
    // Crickets. Never called 
} 

我知道通知不能在模拟器上工作。我已经在使用设备的开发中尝试了这一点,并且在使用testflight和一些设备的生产环境中尝试了这一点。我是否错过了osmething?

回答

2

你似乎在做一切正确。我能想到的两件事:

  1. 您确定您正在注册远程通知吗?我无法在任何地方看到致电application.registerForRemoteNotifications()的电话。我假设你忘了粘贴在这里。

  2. 您是否尝试删除面板中的订阅并重新创建它?我有问题注册相同的订阅多次与不同的notificationInfo 打破它,它会停止射击。

+0

我正在注册,删除并重新创建修复它的订阅,但只是在我更改了我使用的订阅标识符后。哎呀.. – BadPirate

1

这有效......并且与您的版本有点不同......我不尝试注册特定通知,我注册了所有...然后在看到它们之后已经收到他们。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

    application.registerForRemoteNotifications() 
    return true 
} 

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { 
    let notification = CKQueryNotification(fromRemoteNotificationDictionary: userInfo as! [String : NSObject]) 


    if notification.notificationType == .Query { 
     let queryNotification = notification 
     if queryNotification.queryNotificationReason == .RecordUpdated { 
      print("queryNotification.recordID \(queryNotification.recordID)") 
     } 
    } 
    }