iOS GCM通知正在发送,但未显示
我正在尝试在我的iOS应用程序中实现GCM。一切似乎都很好,该应用程序正在连接到GCM并获得注册ID。如果我用邮递员向那个regid发送通知,它就会工作,我会从谷歌那里得到一个成功的回复。然而,不管我怎么做,通知实际上并没有显示在设备上 我正在使用的GCM服务器的post消息是iOS GCM通知正在发送,但未显示,ios,swift,google-cloud-messaging,Ios,Swift,Google Cloud Messaging,我正在尝试在我的iOS应用程序中实现GCM。一切似乎都很好,该应用程序正在连接到GCM并获得注册ID。如果我用邮递员向那个regid发送通知,它就会工作,我会从谷歌那里得到一个成功的回复。然而,不管我怎么做,通知实际上并没有显示在设备上 我正在使用的GCM服务器的post消息是 { "to" : "RegID", "content_available" : true, "notification" : { "body" : "Test Body",
{
"to" : "RegID",
"content_available" : true,
"notification" : {
"body" : "Test Body",
"title" : "Test Title"
}
}
我的回答是:
{
"multicast_id": 6594175386712804014,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [
{
"message_id": "0:1443445858075083%c4cfa24dc4cfa24d"
}
]
}
这让我相信我的应用程序中的代码有问题。下面我粘贴了所有相关的代码。我已经编写了3次代码,但都没有成功,第一次是遵循教程并将其正确地编辑到我的应用程序中,第二次是从教程中复制代码,第三次是从Github repo获得示例应用程序,并复制了与GCM相关的所有内容
AppDelegate:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var connectedToGCM = false
var gcmSenderID: String?
var registrationToken: String?
var registrationOptions = [String: AnyObject]()
let defaults = NSUserDefaults.standardUserDefaults()
let registrationKey = "onRegistrationCompleted"
let messageKey = "onMessageReceived"
let notification = "isNotificationEnabled"
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Google Cloud Messaging
var configureError:NSError?
GGLContext.sharedInstance().configureWithError(&configureError)
assert(configureError == nil, "Error configuring Google services: \(configureError)")
gcmSenderID = GGLContext.sharedInstance().configuration.gcmSenderID
var types: UIUserNotificationType = UIUserNotificationType.Badge |
UIUserNotificationType.Alert |
UIUserNotificationType.Sound
var settings: UIUserNotificationSettings =
UIUserNotificationSettings( forTypes: types, categories: nil )
application.registerUserNotificationSettings( settings )
application.registerForRemoteNotifications()
var gcmConfig = GCMConfig.defaultConfig()
GCMService.sharedInstance().startWithConfig(gcmConfig)
return true
}
func applicationDidBecomeActive(application: UIApplication) {
GCMService.sharedInstance().connectWithHandler({
(NSError error) -> Void in
if error != nil {
println("Could not connect to GCM: \(error.localizedDescription)")
} else {
self.connectedToGCM = true
println("Connected to GCM")
// ...
}
})
}
func application( application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken
deviceToken: NSData ) {
println(deviceToken)
// [END receice_apns_token]
// [START get_gcm_reg_token]
// Create a config and set a delegate that implements the GGLInstaceIDDelegate protocol.
var instanceIDConfig = GGLInstanceIDConfig.defaultConfig()
// Start the GGLInstanceID shared instance with that config and request a registration
// token to enable reception of notifications
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 registrationHandler(registrationToken: String!, error: NSError!) {
if (registrationToken != nil) {
self.registrationToken = registrationToken
println("Registration Token: \(registrationToken)")
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 application( application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
println("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)
}
如果之前有人问过类似的问题,请告诉我,我找不到一个能正确描述我处境的问题
提前谢谢 您尚未实现适当的通知回调。你需要实施
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
// Do something with userInfo
// call fetchCompletionHandler with the appropriate UIBackgroundFetchResult
}
静默推送通知(即设置了内容可用标志的通知)调用上述uiapplicationelegate
方法,而不是application:didReceiveMotonification:
另外,请确保已将远程通知
值添加到Info.plist
中的ui背景模式
您的HTTP请求看起来不错。但是在您的实现和应用程序之间存在一些差异。您应该尝试复制并粘贴该示例项目中的所有行,或者下载该示例项目。您的链接中有哪些内容负责显示通知?看起来主要是UI代码。从谷歌教程中我了解到,他们Appdelegate中的代码应该足以显示一个简单的通知?我从没想过苹果会允许你在IOS上使用任何非APNS的推送通知。@Dan在示例项目中。你是上帝,这解决了我的问题。是否有任何方法可以使用fetchCompletionHandler更新UITableView?在调用fetchCompletionHandler
之前,您可以执行任何更新。处理程序只是用来通知iOS您已经完成了您想要执行的任何更新。