Updated 10 December 2018
GCM(Google Cloud messaging) don’t work in china. But WHY ?
GCM is a derived from Google it should work in android devices irrespective of country.
The reason is from this source
GCM requires devices running Android 2.2 or higher that also have the Google Play Store application installed
Google Cloud Messaging (GCM) works with the Google Play Services SDK and as you probably know Google Play is not available in China (just like most of other Google services). ~Source2
Here is a list of some alternative for the GCM:
http://stackoverflow.com/a/17826007/5028508
This post will deal with implentation of https://pushy.me/ (Military-grade reliability and speed for push notifications)
Yes as the description say pushy service is reliable, speedy no doubt and also easy to implement
Check pricing (its not expensive however there are other free push service available like JPush) and documentation .
Here is the procedure from document to implement Pushy notification
Download the link of the latest sdk.
https://pushy.me/downloads/sdk/pushy-1.0.9.zip
Extract the archive and copy the pushy-x.x.x.jar and pushy-jackson.jar files to your libs/ folder.
Add this compilation statement in module level build.gradle file
1 2 3 |
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) } |
1 |
String registrationID = Pushy.register(getApplicationContext()); |
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 |
new RegisterForPushNotificationsAsync().execute(); private class RegisterForPushNotificationsAsync extends AsyncTask<Void, Void, Exception> { protected Exception doInBackground(Void... params) { try { // Ask Pushy to assign a unique registration ID to this device String registrationID = Pushy.register(getApplicationContext()); // Send the registration ID to your backend server via an HTTP GET request new URL("https://{YOUR_API_HOSTNAME}/register/device?registration_id=" + registrationID).openConnection(); } catch (Exception exc) { // Return exc to onPostExecute return exc; } // Success return null; } @Override protected void onPostExecute(Exception exc) { // Failed? if (exc != null) { // Show error as toast message Toast.makeText(getApplicationContext(), exc.toString(), Toast.LENGTH_LONG).show(); return; } // Succeeded, do something to alert the user } } |
Add this initialization statement in the laucher activity
1 |
Pushy.listen(this); |
1 2 3 4 5 6 7 8 9 |
<!-- Pushy Permissions --> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <!-- End Pushy Permissions -- |
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 |
<!-- Pushy Declarations --> <!-- Pushy Notification Receiver --> <!-- Incoming push notifications will invoke the following BroadcastReceiver --> <receiver android:name=".PushReceiver" android:exported="false"> <intent-filter> <!-- Do not modify this --> <action android:name="pushy.me" /> </intent-filter> </receiver> <!-- Pushy Update Receiver --> <!-- Do not modify - internal BroadcastReceiver that restarts the listener service --> <receiver android:name="me.pushy.sdk.receivers.PushyUpdateReceiver" android:exported="false"> <intent-filter> <action android:name="android.intent.action.PACKAGE_REPLACED" /> <data android:scheme="package" /> </intent-filter> </receiver> <!-- Pushy Boot Receiver --> <!-- Do not modify - internal BroadcastReceiver that restarts the listener service --> <receiver android:name="me.pushy.sdk.receivers.PushyBootReceiver" android:exported="false"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver> <!-- Pushy Socket Service --> <!-- Do not modify - internal socket service --> <service android:name="me.pushy.sdk.services.PushySocketService"/> <!-- End Pushy Declarations --> |
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 |
package {YOUR_PACKAGE_NAME}; import android.content.Intent; import android.graphics.Color; import android.content.Context; import android.app.Notification; import android.media.RingtoneManager; import android.app.NotificationManager; import android.content.BroadcastReceiver; public class PushReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String notificationTitle = "Pushy"; String notificationText = "Test notification"; // Attempt to grab the "message" property from the payload: {"message":"Hello World!"} if (intent.getStringExtra("message") != null) { notificationText = intent.getStringExtra("message"); } // Prepare a notification with vibration, sound and lights Notification.Builder mBuilder = new Notification.Builder(context) .setSmallIcon(android.R.drawable.ic_dialog_info) .setContentTitle(notificationTitle) .setContentText(notificationText) .setLights(Color.RED, 1000, 1000) .setVibrate(new long[]{0, 400, 250, 400}) .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); // Get an instance of the NotificationManager service NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); // Build the notification and send it mNotifyMgr.notify(1, mBuilder.build()); } } |
For backend configuration visit https://dashboard.pushy.me/apps
Signup a account and create a application. You will be provided a secret key for each app.
Here is a test script that send a notification on a particular RegisterId that was obtain in Register a device step
PushyNoficationTest.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); // Require Pushy API Class require('pushy.php'); // Payload data you want to send to devices $data = array('message' => 'PUSH MESSAGE !!!!!'); // The recipient device registration IDs $ids = array('XXXXXXXXXXXXXXXXXXXXXXXXX'); // Send it with Pushy PushyAPI::sendPushNotification($data, $ids); |
pushy.php
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 |
<?php class PushyAPI { static public function sendPushNotification( $data, $ids ) { // Insert your Secret API Key here $apiKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // Set post variables $post = array( 'data' => $data, 'registration_ids' => $ids, ); // Set Content-Type header since we're sending JSON $headers = array( 'Content-Type: application/json' ); // Initialize curl handle $ch = curl_init(); // Set URL to Pushy endpoint curl_setopt($ch, CURLOPT_URL, 'https://api.pushy.me/push?api_key=' . $apiKey); // Set request method to POST curl_setopt($ch, CURLOPT_POST, true); // Set our custom headers curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // Get the response back as string instead of printing it curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Set post data as JSON curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post)); // Actually send the push $result = curl_exec($ch); // Display errors if (curl_errno($ch)) { echo curl_error($ch); } // Close curl handle curl_close($ch); // Debug API response //echo $result; } } |
For more information check out the original documentation or leave a comment here.
For JPush Stay tuned!
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
2 comments