Ich habe anscheinend eine funktionierende Push-Benachrichtigung in einer iOS-App. Hier ist das Fleisch davon. Was ist Fahren mich Nüsse ist, dass alles scheint zu arbeiten, außer dass meine Haltepunkte nie in den Nachrichtenempfängerfunktionen getroffen werden; Die Registrierung von Geräten mit Google Cloud Notifications ist erfolgreich, der Initialisierungscode scheint zu funktionieren und ich erhalte sogar "Erfolgsmeldungen" vom GCM-Dienst.Keine Push-Benachrichtigungen erhalten (und keine Ideen zur Fehlerbehebung)
STILL, ich scheint nie wirklich die Nachricht in meiner App zu bekommen. Beachten Sie, dass ich es auf einem iPad verwende, das mit meinem Mac verbunden ist, und in den Benachrichtigungseinstellungen scheint alles koscher/aktiviert zu sein. Außerdem verwende ich das Dev-Zertifikat, um die Option kGGLInstanceIDAPNSServerTypeSandboxOption: true zu finden.
Was könnte ich vermissen?
import UIKit
import Google
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, GGLInstanceIDDelegate {
var window: UIWindow?
var gcmSenderID: String?
var registrationToken: String?
var registrationOptions = [String: AnyObject]()
let messageKey = "onMessageReceived"
let registrationKey = "onRegistrationCompleted"
func application(application: UIApplication,
didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
//********************************
//*** Never Hits Breakpoint ******
//********************************
print("Notification received: \(userInfo)")
// Handle the received message
NSNotificationCenter.defaultCenter().postNotificationName(messageKey, object: nil,
userInfo: userInfo)
}
func application(application: UIApplication,
didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void) {
//********************************
//*** Never Hits Breakpoint ******
//********************************
NSNotificationCenter.defaultCenter().postNotificationName(messageKey, object: nil,
userInfo: userInfo)
handler(UIBackgroundFetchResult.NoData);
}
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
var configureError:NSError?
GGLContext.sharedInstance().configureWithError(&configureError)
assert(configureError == nil, "Error configuring Google services: \(configureError)")
gcmSenderID = GGLContext.sharedInstance().configuration.gcmSenderID
// ...
// Register for remote notifications
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
// Override point for customization after application launch.
return true
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
// Create a config and set a delegate that implements the GGLInstaceIDDelegate protocol.
let instanceIDConfig = GGLInstanceIDConfig.defaultConfig()
instanceIDConfig.delegate = self
// 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)
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError
error: NSError) {
print("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
print("Registration Token: \(registrationToken)")
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 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)
}
}
Nachricht https://gcm-http.googleapis.com/gcm/send gesendet. Die Nutzlast ist willkürlich/kopiert von einer Probe.
{
"content-available":1,
"data": {
"score": "5x1",
"time": "15:10"
},
"to" : "<<the registration token resulting from the code above>>"
}
..., die in der Antwort ergibt:
{
"multicast_id": 12345...,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [
{
"message_id": "some ID"
}
]
}
Irgendwelche Ideen?
Haben aktivieren Sie Push-Benachrichtigung Fähigkeiten? http://i.stack.imgur.com/BffFp.png – fatihyildizhan
Ja, obwohl ich bemerkte, dass ich nicht aktiviert Hintergrund Modi-> Remote-Benachrichtigungen, so dass hilft die Hälfte meines Problems zu erklären. Egal, es scheint Wie der erste Handler sollte einen Haltepunkt treffen (da es Benachrichtigungen verarbeiten soll, wenn die App ausgeführt wird). Ist es auch wichtig, dass die tatsächliche Bundle-ID <> .com.company.appname wird? Würde sich das auf irgendetwas auswirken? –
Colin
ar Haben Sie in 'didRegisterForRemoteNotificationsWithDeviceToken' die richtige Antwort erhalten? – HardikDG