gpt4 book ai didi

ios Swift GCM 注册 token nil

转载 作者:搜寻专家 更新时间:2023-10-31 19:34:34 24 4
gpt4 key购买 nike

我做错了什么?它打印出我的 registrationToken 是 NIL,尽管它也成功地打印出 Registration to GCM。

appDelegate.swift

import UIKit
import SlideMenuControllerSwift
import AFNetworking

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
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 {
var configureError:NSError?
GGLContext.sharedInstance().configureWithError(&configureError)
if configureError != nil {
println("Error configuring the Google context: \(configureError)")
}
gcmSenderID = GGLContext.sharedInstance().configuration.gcmSenderID
// [END_EXCLUDE]

// Register for remote notifications
var types: UIUserNotificationType = UIUserNotificationType.Badge | UIUserNotificationType.Alert | UIUserNotificationType.Sound
var settings: UIUserNotificationSettings =
UIUserNotificationSettings( forTypes: types, categories: nil )
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()

// [START start_gcm_service]
GCMService.sharedInstance().startWithConfig(GCMConfig.defaultConfig())

AFNetworkReachabilityManager.sharedManager().startMonitoring()
// Override point for customization after application launch.
Utilities.customizeNavigationBar()
Utilities.customizeNavigationBarButtons()
let storyboard = UIStoryboard(name: "MenuStoryboard", bundle: nil)
let containerController: RestaurantTableViewController = storyboard.instantiateViewControllerWithIdentifier("hi") as! RestaurantTableViewController
let rightMenu: RightMenuViewController = storyboard.instantiateViewControllerWithIdentifier("rightMenu") as! RightMenuViewController
let leftMenu: LeftMenuViewController = storyboard.instantiateViewControllerWithIdentifier("leftMenu") as! LeftMenuViewController


let nvc: UINavigationController = UINavigationController(rootViewController: containerController)
// leftMenu.mainViewController = nvc

let slideMenuController = SlideMenuController(mainViewController:nvc, leftMenuViewController: leftMenu, rightMenuViewController: rightMenu)
self.window?.rootViewController = slideMenuController
self.window?.makeKeyAndVisible()

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

// Start the GGLInstanceID shared instance with the default config and request a registration token to enable reception of notifications
GGLInstanceID.sharedInstance().startWithConfig(GGLInstanceIDConfig.defaultConfig())
registrationOptions = [kGGLInstanceIDRegisterAPNSOption:deviceToken,
kGGLInstanceIDAPNSServerTypeSandboxOption:true]
GGLInstanceID.sharedInstance().tokenWithAuthorizedEntity(gcmSenderID, scope: kGGLInstanceIDScopeGCM, options: registrationOptions, handler: registrationHandler)


}

// [START receive_apns_token_error]
func application( application: UIApplication, didFailToRegisterForRemoteNotificationsWithError
error: NSError ) {
println("Registration for remote notification failed with error: \(error.localizedDescription)")
// [END receive_apns_token_error]
let userInfo = ["error": error.localizedDescription]
NSNotificationCenter.defaultCenter().postNotificationName(
registrationKey, object: nil, userInfo: userInfo)
}


func registrationHandler(registrationToken: String!, error: NSError!) {
if (registrationToken != nil) {
self.registrationToken = registrationToken
println("Registration Token: \(registrationToken)")
NSUserDefaults.standardUserDefaults().setObject(registrationToken, forKey: "registrationToken")
self.subscribeToTopic()
let userInfo = ["registrationToken": registrationToken]
NSNotificationCenter.defaultCenter().postNotificationName(
self.registrationKey, object: nil, userInfo: userInfo)
} else {
println("Registration to GCM failed with error: \(error.localizedDescription)")
let userInfo = ["error": error.localizedDescription]
NSNotificationCenter.defaultCenter().postNotificationName(
self.registrationKey, object: nil, userInfo: userInfo)
} }
func onTokenRefresh() {
// A rotation of the registration tokens is happening, so the app needs to request a new token.
println("The GCM registration token needs to be changed.")
GGLInstanceID.sharedInstance().tokenWithAuthorizedEntity(gcmSenderID,
scope: kGGLInstanceIDScopeGCM, options: registrationOptions, handler: registrationHandler)
}

func subscribeToTopic() {
// If the app has a registration token and is connected to GCM, proceed to subscribe to the
// 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)");
}
})
}
}

// [START connect_gcm_service]
func applicationDidBecomeActive( application: UIApplication) {
// Connect to the GCM server to receive non-APNS notifications
GCMService.sharedInstance().connectWithHandler({
(NSError error) -> Void in
if error != nil {
print("Could not connect to GCM: \(error.localizedDescription)")
} else {
self.connectedToGCM = true
print("Connected to GCM")
// [START_EXCLUDE]
self.subscribeToTopic()
// [END_EXCLUDE]
}
})
}


// [START ack_message_reception]
func application( application: UIApplication,
didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
print("Notification received: \(userInfo)")
// This works only if the app started the GCM service
GCMService.sharedInstance().appDidReceiveMessage(userInfo);
// Handle the received message
// [START_EXCLUDE]
NSNotificationCenter.defaultCenter().postNotificationName(messageKey, object: nil,
userInfo: userInfo)
// [END_EXCLUDE]
}

func application( application: UIApplication,
didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void) {
print("Notification received: \(userInfo)")
// This works only if the app started the GCM service
GCMService.sharedInstance().appDidReceiveMessage(userInfo);
// Handle the received message
// Invoke the completion handler passing the appropriate UIBackgroundFetchResult value
// [START_EXCLUDE]
NSNotificationCenter.defaultCenter().postNotificationName(messageKey, object: nil,
userInfo: userInfo)
handler(UIBackgroundFetchResult.NoData);
// [END_EXCLUDE]
}
func applicationDidEnterBackground(application: UIApplication) {
GCMService.sharedInstance().disconnect()
// [START_EXCLUDE]
self.connectedToGCM = false
// [END_EXCLUDE]
println("Stop executing app and whent into background!")
}

func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


// [START upstream_callbacks]
func willSendDataMessageWithID(messageID: String!, error: NSError!) {
if (error != nil) {
// Failed to send the message.
} else {
// Will send message, you can save the messageID to track the message
}
}

func didSendDataMessageWithID(messageID: String!) {
// Did successfully send message identified by messageID
}
// [END upstream_callbacks]

func didDeleteMessagesOnServer() {
// Some messages sent to this device were deleted on the GCM server before reception, likely
// because the TTL expired. The client should notify the app server of this, so that the app
// server can resend those messages.
}
}

错误日志

Tracking ID must not be nil or empty.
Client ID must not be nil or empty.
Attempted to configure [Identity, Analytics, AdMob, SignIn, AppInvite, CloudMessaging].
Successfully configured [CloudMessaging].
Failed to configure [Analytics, SignIn].
Subspecs not present, so not configured [Identity, AdMob, AppInvite].

Error configuring the Google context: Optional(Error Domain=com.google.greenhouse Code=-200 "Unable to configure GGL."
UserInfo=0x7fdab9d3a390 {NSLocalizedRecoverySuggestion=Check formatting and location of GoogleService-Info.plist., NSLocalizedDescription=Unable to configure GGL., NSLocalizedFailureReason=Unable to parse supplied GoogleService-Info.plist. See log for details.})

我正在尝试连接到 GCM,我已经设置了 SENDER-ID 和 SENDER ID,没问题,现在出了什么问题?

Setting up a GCM Client App on iOS 它说:

If registration was successful, InstanceID invokes the registrationToken handler with a valid registration token and a nil error object. If registration instead fails, the registration token is nil and a valid error object is returned. For details on the error codes, see GGLInstanceID.h. When registration fails, client apps should implement exponential backoff to try again.

我在输出中得到了一个无错误对象,但也没有注册 token 。

最佳答案

我认为您的代码没有任何问题,因为这是在 iOS 客户端 上实现云消息传递 的正确方法。

您可能错过的一些棘手部分是 Provisioning APNs SSL Certificates 的配置

  1. 创建您的 App ID 并开启推送通知
  2. 创建您的 SSL 证书
  3. 下载并运行它以添加到配置文件
  4. 要确保您的配置文件具有此证书,请转至 Xcode -> 首选项 -> 帐户 -> 您的 Apple ID -> 您的团队 -> 查看详细信息 -> 全部下载

这将刷新您的证书,然后转到您的目标,选择正确的团队并运行它。希望它能奏效:)

关于ios Swift GCM 注册 token nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33145303/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com