1

在iOS中使用GCM進行推送通知。一切都設置得很好。我得到registrationToken值&也subscribeToTopic成功。連接到GCM。 didRegisterForRemoteNotificationsWithDeviceToken方法也被調用。但沒有得到推送通知。 didReceiveRemoteNotification方法從不調用。在我的Android應用程序,我得到推送通知沒有任何問題。但在iOS通知從未收到。這裏是源代碼:GCM iOS沒有收到推送通知

class AppDelegate: UIResponder, UIApplicationDelegate, GGLInstanceIDDelegate, GCMReceiverDelegate{ 
var gcmSenderID: String? 
var registrationToken: String? 
let registrationKey = "onRegistrationCompleted" 
let messageKey = "onMessageReceived" 
var registrationOptions = [String: AnyObject]() 
var connectedToGCM = false 
let subscriptionTopic = "/topics/global" 
var subscribedToTopic = false 

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

    var configureError:NSError? 

    GGLContext.sharedInstance().configureWithError(&configureError) 

    gcmSenderID = GGLContext.sharedInstance().configuration.gcmSenderID 

    if application.respondsToSelector("registerUserNotificationSettings:") { 

     let types:UIUserNotificationType = (.Alert | .Badge | .Sound) 
     let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil) 
     application.registerUserNotificationSettings(settings) 
     application.registerForRemoteNotifications() 

    } else { 
     // Register for Push Notifications before iOS 8 
     application.registerForRemoteNotificationTypes(.Alert | .Badge | .Sound) 
    } 
    let gcmConfig = GCMConfig.defaultConfig() 
    gcmConfig.receiverDelegate = self 
    GCMService.sharedInstance().startWithConfig(gcmConfig) 

    return true 

} 

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { 
    print("remote notification") 
    GCMService.sharedInstance().appDidReceiveMessage(userInfo); 

} 
func application(application: UIApplication, 
    didReceiveRemoteNotification userInfo: [NSObject : AnyObject], 
    fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void) { 
     GCMService.sharedInstance().appDidReceiveMessage(userInfo); 

    NSNotificationCenter.defaultCenter().postNotificationName(messageKey, object: nil, 
      userInfo: userInfo) 
     handler(UIBackgroundFetchResult.NoData); 


} 
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken 
    deviceToken: NSData) { 

     let instanceIDConfig = GGLInstanceIDConfig.defaultConfig() 
     instanceIDConfig.delegate = self 

    GGLInstanceID.sharedInstance().startWithConfig(instanceIDConfig) 
     registrationOptions = [kGGLInstanceIDRegisterAPNSOption:deviceToken, 
      kGGLInstanceIDAPNSServerTypeSandboxOption:true] 

     GGLInstanceID.sharedInstance().tokenWithAuthorizedEntity(gcmSenderID, 
      scope: kGGLInstanceIDScopeGCM, options: registrationOptions, handler: registrationHandler) 
     // [END get_gcm_reg_token] 
} 
func onTokenRefresh() { 
    // A rotation of the registration tokens is happening, so the app needs to request a new token. 
    print("The GCM registration token needs to be changed.") 
    GGLInstanceID.sharedInstance().tokenWithAuthorizedEntity(gcmSenderID, 
     scope: kGGLInstanceIDScopeGCM, options: registrationOptions, handler: registrationHandler) 
} 
func registrationHandler(registrationToken: String!, error: NSError!) { 
    if (registrationToken != nil) { 

     self.registrationToken = registrationToken 
     print("Registration Token: \(registrationToken)") 
     self.subscribeToTopic() 
     let userInfo = ["registrationToken": registrationToken] 
     NSNotificationCenter.defaultCenter().postNotificationName(
      self.registrationKey, object: nil, userInfo: userInfo) 
    } else { 
     print("Registration to GCM failed with error: \(error.localizedDescription)") 
     let userInfo = ["error": error.localizedDescription] 
     NSNotificationCenter.defaultCenter().postNotificationName(
      self.registrationKey, object: nil, userInfo: userInfo) 
    } 
} 
func subscribeToTopic() { 

    // topic 
    if(registrationToken != nil && connectedToGCM) { 
     GCMPubSub.sharedInstance().subscribeWithToken(self.registrationToken, topic: subscriptionTopic, 
      options: nil, handler: {(NSError error) -> Void in 
       if (error != nil) { 
        // Treat the "already subscribed" error more gently 
        if error.code == 3001 { 
         print("Already subscribed to \(self.subscriptionTopic)") 
        } else { 
         print("Subscription failed: \(error.localizedDescription)"); 
        } 
       } else { 
        self.subscribedToTopic = true; 
        NSLog("Subscribed to \(self.subscriptionTopic)"); 
       } 
     }) 
    } 
} 


func applicationDidBecomeActive(application: UIApplication) { 
    print("applicationDidBecomeActive") 
    self.globalPrice = CartLocalData.getTotalPrice() 
    GCMService.sharedInstance().connectWithHandler({ 
     (NSError error) -> Void in 
     if error != nil { 
      print("Could not connect to GCM: \(error.localizedDescription)") 

     } else { 
      self.connectedToGCM = true 
      self.subscribeToTopic() 
      print("Connected to GCM") 
      // ... 
     } 
    }) 

} 

}

我在做什麼錯。有誰可以建議?

+0

你有沒有在應用目標樹立正確的能力說話的鏈接到蘋果開發者網站射線wenderlich教程?並提出了推動條款? – bolnad

+0

是的,我有推送條款。我需要額外的應用程序目標? –

+0

你需要在能力部分打開它,當你建立應用程序時,你需要在你的機器上有推送通知密鑰/證書對,它可以讓你建立它 – bolnad

回答

0

您應該可以選擇打開功能部分的推送功能。您還需要在Apple會員中心構建單獨的密鑰/證書對,並將其包含在您的計算機中。這裏是我製作的一個新項目的截圖。

capabilities section

這聽起來就像在過程中的步驟也許有可能無法完成。

重新開始並嘗試找出你的遺漏。這裏要說的是引導您完成步驟here

而且關於它的深入here

+0

我正在使用xcode 6.從這個[link](http://stackoverflow.com/questions/30979218/no-push-notification-capability-in-xcode)我可以看到,push不再是一個選項xcode 6.相反,它應該通過代碼 –

+0

確定。現在我要刪除所有證書和配置文件。然後重新創建那些。我在這裏有一個問題。我已經將應用程序提交給appstore。重新創建證書是否會影響appstore應用程序? –

+0

是的,它很小心,有一個有用的工具,你應該使用稱爲推杆,它會告訴你什麼是錯誤的之前,你刪除一切和統計 – bolnad