3

I can't seem to get GCM push notifications working. My problem is I don't know how to get a registration ID from GCM. I can get a token from APN just fine. But I'm not quite sure what to do next. I tried following the tutorial but its not really working for me. I'm a beginner so please be explicit.

What I'm asking is, after obtaining a token from APN, then what do I do?

Thanks in advance. https://developers.google.com/cloud-messaging/ios/client

TheSabby
  • 267
  • 1
  • 3
  • 12

1 Answers1

4

The Registration Token is given to the registration handler from didRegisterForRemoteNotificationsWithDeviceToken

All code below is taken from the GCM Sample from Google.

First, declare a handler in your application:didFinishLaunchingWithOptions:

_registrationHandler = ^(NSString *registrationToken, NSError *error){
    if (registrationToken != nil) {
        weakSelf.registrationToken = registrationToken;
        NSLog(@"Registration Token: %@", registrationToken);
        NSDictionary *userInfo = @{@"registrationToken":registrationToken};
        [[NSNotificationCenter defaultCenter] postNotificationName:weakSelf.registrationKey
                                                            object:nil
                                                          userInfo:userInfo];
    } else {
        NSLog(@"Registration to GCM failed with error: %@", error.localizedDescription);
        NSDictionary *userInfo = @{@"error":error.localizedDescription};
        [[NSNotificationCenter defaultCenter] postNotificationName:weakSelf.registrationKey
                                                            object:nil
                                                          userInfo:userInfo];
    }
};

Call your handler in the application registration callback:

- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    // 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:@YES};
[[GGLInstanceID sharedInstance] tokenWithAuthorizedEntity:_gcmSenderID
                                                    scope:kGGLInstanceIDScopeGCM
                                                  options:_registrationOptions
                                                  handler:_registrationHandler];
}

The GCM token to use is simply the "NSString *registrationToken" in the registration handler.

Jeremy
  • 3,438
  • 3
  • 34
  • 57
  • Is registrationToken the one, which should be used in the server side to identify recipient for the notification? Or is it just an auth token used with GCM API on the device side? This is not clearly stated in GCM iOS docs. – Tapani Aug 03 '15 at 10:55
  • @Tapani, yes, the registrationToken received there is the one used to identify the recipient when sending a push from the server. – Jeremy Aug 04 '15 at 16:17
  • not sure if you can help but when i run my app from archive the _registrationHandler = code doesnt run and i dont get a token. if i run straight to my device from xcode it works fine. any idea here? – Rob85 Sep 05 '15 at 19:11
  • After _registrationHandler block callback in didFinishLaunching, I am getting registrationToken as nil. Any Idea? I have verified that deviceToken which I am passing to _registrationOptions dictionary is not nil. – JiteshW Nov 06 '15 at 07:36
  • @Jitesh, if the token is null, the error object should contain some information about why it failed – Jeremy Nov 06 '15 at 17:36
  • @JeremyLee, Error object is returning code as 0. As per google GCM docs its "Invalid Request - Some parameters of the request were invalid". Can't figure out why. I have sender Id, Dictionary is not nil (deviceToken is string after removing spaces and <>), Scope is kGGLInstanceIDScopeGCM. Added GoogleService-Info.plist file. – JiteshW Nov 09 '15 at 05:28
  • how about getting token from the other classes not only on appdelegate. this means, token is only generated on load of the app. but what if when you open the app, internet connections fails and unable to produce token, do you have code for generating token not on appdelegate? – lhencq Apr 21 '16 at 01:31