我正在为iOS9重新编译我的iPhone应用程序,并在尝试注册GCM时遇到错误,如下所示:
向GCM注册失败并显示错误:无法完成操作。 (com.google.iid错误1003。)
我已经找了一段时间,在Google上找不到任何东西。 任何人都可以帮我这个吗?
提前致谢
来自appdelegate.swift的代码如下:
var connectedToGCM = false
var subscribedToTopic = false
var gcmSenderID: String?
var registrationToken: String?
var registrationOptions = [String: AnyObject]()
let registrationKey = "onRegistrationCompleted"
let messageKey = "onMessageReceived"
let subscriptionTopic = "/topics/global"
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
GCMService.sharedInstance().startWithConfig(GCMConfig.defaultConfig())
return true
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData)
{
//Process the deviceToken and send it to your server
let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
var tokenString = ""
for var i = 0; i < deviceToken.length; i++
{
tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
}
GGLInstanceID.sharedInstance().startWithConfig(GGLInstanceIDConfig.defaultConfig())
registrationOptions = [kGGLInstanceIDRegisterAPNSOption:deviceToken,
kGGLInstanceIDAPNSServerTypeSandboxOption:true]
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)")
APIManager.sharedInstance.setDeviceToken(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)
}
}
答案 0 :(得分:2)
1003 error talks "Token request has invalid authorizedEntity."
就我而言,gcmSenderID
是零。
也许您没有使用gcmSenderID
中的GoogleService-Info.plist
。要从GoogleService-Info.plist
导入设置,请插入此代码
var configureError:NSError?
GGLContext.sharedInstance().configureWithError(&configureError)
assert(configureError == nil, "Error configuring Google services: \(configureError)")
gcmSenderID = GGLContext.sharedInstance().configuration.gcmSenderID
进入
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { ... }
答案 1 :(得分:1)
Google错误1003
我在根文件夹中有GoogleService-info.plist。 在我的情况下,gcmSenderID甚至不是null,但我仍然收到此错误。
但我在didFinishLaunchingOptions下的AppDelegate.m文件中遗漏了这段代码。
NSError * configureError; [[GGLContext sharedInstance] configureWithError:&amp; configureError]; NSAssert(!configureError,@&#34;配置Google服务时出错:%@&#34;,configureError); _gcmSenderID = [[[GGLContext sharedInstance] configuration] gcmSenderID];
答案 2 :(得分:0)
这是我身上的一个愚蠢的错误。 'GoogleService-Info.plist'的名称不正确并且位于错误的位置,并且不在我的构建阶段 - &gt;复制捆绑资源。