GCM iOS 没有收到推送通知
Posted
技术标签:
【中文标题】GCM iOS 没有收到推送通知【英文标题】:GCM iOS not receiving push notification 【发布时间】:2016-03-05 12:39:17 【问题描述】:在 ios 中使用 GCM 进行推送通知。一切都设置得很好。我获得了registrationToken 值并且也成功地subscribeToTopic。连接到 GCM。 didRegisterForRemoteNotificationsWithDeviceToken 方法也被调用。但没有收到推送通知。 didReceiveRemoteNotification 方法从未调用过。在我的 android 应用程序上,我收到推送通知没有任何问题。但在 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")
// ...
)
我做错了什么。有人可以推荐吗?
【问题讨论】:
您是否在应用目标中设置了正确的功能?并制定了推送规定? 是的,我有推送条款。我需要在应用目标中添加任何其他内容吗?? 您需要在功能部分打开它,并且在构建应用程序时,您需要在您的机器上拥有推送通知密钥/证书对,以便您构建它 可能重复的 SO 票请参考此link 对于您的链接无法解决我的问题的信息 【参考方案1】:您应该可以选择从功能部分打开推送。您还需要在苹果会员中心建立单独的密钥/证书对,并将它们包含在您的机器上。这是我做的一个新项目的截图。
听起来可能流程中的某个步骤可能没有完成。
重新开始,试着找出你错过了什么。这是一个 ray wenderlich 教程,将引导您完成步骤here
以及深入讨论它的 Apple 开发者网站的链接here
【讨论】:
我正在使用 xcode 6。从 link 我可以看到 push 不再是 xcode 6 中的选项。而是应该通过代码激活 好的。现在我要删除我所有的证书和配置文件。然后重新创建它们。我在这里有个问题。我已经将应用程序提交到应用商店。重新创建证书会影响 appstore 应用吗?? 是的,非常小心,你应该使用一个有用的工具,叫做 Pusher,它会在你删除所有内容并统计数据之前告诉你出了什么问题以上是关于GCM iOS 没有收到推送通知的主要内容,如果未能解决你的问题,请参考以下文章