Google Cloud Messaging and Parse comparison
Few days ago my friend showed me his Android app with Google Cloud Messaging, which has also iOS support. We usually use Parse or direct access for Apple Push Notifications, so I thought that we should try this APN backend.
Preconfiguration
I assume that you already know how to setup push notification on developer.apple.com, generate certificates, etc. so I will not focus on that part of the setup.
Google Cloud Messaging
First of all, I prepared certificates for APN and enabled Google services for my test app. After that, I had a .plist configuration for my app.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <!--?xml version="1.0" encoding="UTF-8"?--> GCM_SENDER_ID *************** PLIST_VERSION 1 BUNDLE_ID com.example.testapp IS_ADS_ENABLED IS_ANALYTICS_ENABLED IS_APPINVITE_ENABLED IS_GCM_ENABLED IS_SIGNIN_ENABLED GOOGLE_APP_ID 1:**********:ios:************ |
At this step I think Parse is easier and faster to configure, also the Parse documentation is much better than the Google Cloud Messaging one. After that, I had to configure my app for receiving push notifications and registering in APN. I’ve started with adding required Pod – pod 'Google/CloudMessaging'
Parse
If you use Parse, you probably have configured some apps before. You just have to add certificates for APN. It also, supports CocoaPods pod 'Parse'
Implementation
Google Cloud Messaging
That’s the most unpleasant part of implementing Google Cloud Messaging for iOS. First of all, documentation is every hard to read/understand for me.
1 2 3 4 5 6 7 8 9 10 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // ... UIUserNotificationType allNotificationTypes = (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge); UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } |
But this is standard APN registration! What is really important is hidden behind // …
Fortunately, there is also link to GitHub example, with the full code – IMHO very poor quality, which I did not expect from Google.
Parse
1 2 3 4 5 6 7 8 9 | - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { PFInstallation *currentInstallation = [PFInstallation currentInstallation]; [currentInstallation setDeviceTokenFromData:deviceToken]; [currentInstallation saveInBackground]; } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { [PFPush handlePush:userInfo]; } |
Parse did this right. You have to add standard registration like in above example and add 4 lines of code to standard AppDelegate methods. It looks very clean and tidy, so again point for Parse.
Conclusion
I took the liberty of refactoring the Google’s implementation which can be found in the link above. Feel free to use it as your personal snippet.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | @interface AppDelegate() <GGLInstanceIDDelegate, GCMReceiverDelegate> @property(nonatomic, strong) NSString *gcmSenderID; @property(nonatomic, strong) NSDictionary *registrationOptions; @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [self gcmHelperSetup]; return YES; } - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { GGLInstanceIDConfig *instanceIDConfig = [GGLInstanceIDConfig defaultConfig]; instanceIDConfig.delegate = self; [[GGLInstanceID sharedInstance] startWithConfig:instanceIDConfig]; self.registrationOptions = @{kGGLInstanceIDRegisterAPNSOption:deviceToken, kGGLInstanceIDAPNSServerTypeSandboxOption:@NO}; [self gcmHelperToken]; } - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { NSLog(@"Registration for remote notification failed with error: %@", error.localizedDescription); } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { NSLog(@"Notification received: %@", userInfo); [[GCMService sharedInstance] appDidReceiveMessage:userInfo]; } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler { NSLog(@"Notification received: %@", userInfo); [[GCMService sharedInstance] appDidReceiveMessage:userInfo]; handler(UIBackgroundFetchResultNoData); } - (void)onTokenRefresh { NSLog(@"The GCM registration token needs to be changed."); [self gcmHelperToken]; } - (void)gcmHelperToken { [[GGLInstanceID sharedInstance] tokenWithAuthorizedEntity:self.gcmSenderID scope:kGGLInstanceIDScopeGCM options:self.registrationOptions handler:^(NSString *token, NSError *error) { if (error) { NSLog(@"Error %@", error.localizedDescription); } else { NSLog(@"GCM Token = %@", token); } }]; } - (void)gcmHelperSetup { NSError *configureError; [[GGLContext sharedInstance] configureWithError:&configureError]; if (configureError) { NSLog(@"GGLContext error %@", configureError); } self.gcmSenderID = [[[GGLContext sharedInstance] configuration] gcmSenderID]; UIUserNotificationType allNotificationTypes = (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge); UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } @end |
Testing
Google Cloud Messaging
Another problem with GCM is that you do not have any developer’s console for testing. Of course, you can use Postman, but I think it should have something for fast and easy testing without additional configuration.
Parse
Parse gives you the nice tool for sending push notifications with a preview on iPhone mock.
Conclusion
Furthermore, I made GCM Sender app, which allows you to send push notifications via GCM to iOS and Android recipients in fast and easy way.
You have to add your GCM API key and recipient token received in app. That’s all there is to it. Some simple request body is already provided, you can customise it as you want. To send push notification just push button on the right-hand side.
On right-hand tab you will see a response from Google Cloud Messaging (or error if you are offline).
GCM Sender is open source app on GitHub.
Google Cloud Messaging and Parse summary
As you have seen, Google Cloud Messaging is not as user-friendly as Parse. Implementation of GCM can be hard for the first time, however the best advantage of the GCM is that it is free.
If you have enough time and perseverance you should choose GCM. Otherwise, if you prefer clean and easy solutions and you want to pay for it in the future, Parse will be perfect for you – certain downfall payable when you have more than 1M unique recipients.
About the author
Ready to take your business to the next level with a digital product?
We'll be with you every step of the way, from idea to launch and beyond!