Migrating from GCM
Migrating your Android app from Google Cloud Messaging is extremely simple.
There are only a few trivial changes to perform. The rest of your implementation stays the same, including token persistence, push notification logic, and push payload.
Remove GCM Dependency
Open your app-specific build.gradle
file.
compile "com.google.android.gms:play-services-gcm:9.0.0"
Delete the line entirely.
Note: Your referenced play-services-gcm
version may differ slightly.
Remove Manifest Declarations
Delete the following declarations in your AndroidManifest.xml:
- The
<permission>
containing the.permission.C2D_MESSAGE
declaration. - The
<service>
containing thecom.google.android.gms.iid.InstanceID
declaration. - The
<receiver>
containing thecom.google.android.gms.gcm.GcmReceiver
declaration. - The
<uses-permission>
containing thecom.google.android.c2dm.permission.RECEIVE
declaration. - The push notification
<receiver>
containing thecom.google.android.c2dm.intent.RECEIVE
declaration.
Get the SDK
Visit Get the SDK and follow along. Return to this guide when you're done.
Update Registration Call
Please replace GCM's device registration call with Pushy.register(Context)
.
Depending on how you implemented GCM in your app, you may have either one of the following SDK calls that you need to update.
Find:GoogleCloudMessaging.getInstance(context).register(SENDER_ID);
Otherwise, find:
instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
Replace with:
Pushy.register(MainActivity.this);
Your code may slightly differ.
Note: Pushy.register(Context)
is a synchronous, blocking call, so make sure to only execute it from within a background thread. We provide a sample asynchronous implementation if you'd like.
Additional Note: Pushy device tokens are static, there is no need to monitor for changes like with GCM.
Modify Launcher Activity
Visit Modify Launcher Activity and follow along. Return to this guide when you're done.
Modify AndroidManifest
Visit Modify AndroidManifest and follow along. Return to this guide when you're done.
Migrate Notification Logic
Modify the .PushReceiver
reference within your AndroidManifest
so that it links to your existing push notification BroadcastReceiver
.
Also, verify that your BroadcastReceiver
isn't checking for the com.google.android.c2dm.intent.RECEIVE
intent action within its onReceive()
.
Unlike Google Cloud Messaging, we do not stringify your payload data, except if you supply JSON objects or arrays. This means that if you send {"id": 3}
, you'd retrieve that value in your BroadcastReceiver
using intent.getIntExtra("id");
Also, unlike Google Cloud Messaging, we will always invoke your PushReceiver
when notifications are received, even when your app is in the foreground.
Furthermore, on Android, we currently do not support the notification
parameter which builds and displays a notification if your app is in the background. Instead, you can send the relevant parameters within the data
payload of push notifications and then implement logic to build and display a system notification.
Modify Backend Logic
All that's left to do is modify the URL of the HTTP request used to send push notifications to your users in your backend code.
Find the part of your backend code that connects to GCM to send push notifications. All you need to do is replace the URL to the GCM endpoint with Pushy's endpoint URL. The GCM parameters data
and registration_ids
are supported in our Send Notifications API.
https://android.googleapis.com/gcm/send
Replace with:
https://api.pushy.me/push?api_key=SECRET_API_KEY
Note: Make sure to replace SECRET_API_KEY
with your app's Secret API Key, available in the Pushy Dashboard (Click your app -> API Authentication tab). This is a backend API endpoint. Never expose your application's Secret API Key in your client code.
Note: Refer to the Send Notifications API for more information about this API.
All done!
Now that you've migrated both the Android code and backend logic, try to send a push notification from your backend to verify that everything works.
You may use this page to send a test notification containing {message: "Hello World!"}
to your device.
We thank you for choosing Pushy. Let us know if you have any ideas on how to improve our service.