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