JPush is one of the popular and free push notification service provider available in China. Due chinese documentation, there might be chances you might find it difficult. This is not true however.
JPush provide SDK that is easy to use and implement. The only difficulty in implementing the JPush is that there resources are bit scattered or unorganized.[PS: don’t get me wrong]. There are two official domain https://www.jiguang.cn/ and other is jpush.cn.
However jpush.cn now redirect to the former one and according to our client it is used for there internal purposes.
So Let start Implementing JPush as per the docs available on http://jiguang.cn
There is a lot of information is provided related to common problems, 3-minute quick demo, integration guide and sdk on http://docs.jpush.cn/display/dev/Index
Let us know consume some ready backed code from JPush developers:
Please use sdk jpush-android-2.1.5.jar or above. Here is the link to download
Setting Manifest
Here are the required permission for JPush
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<permission android:name="com.example.jpush.high_example.permission.JPUSH_MESSAGE" android:protectionLevel="signature" /> <!-- Required --> <uses-permission android:name="com.example.jpush.high_example.permission.SEND" /> <uses-permission android:name="com.example.jpush.high_example.permission.JPUSH_MESSAGE" /> <uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> |
Here are the service, receiver and meta info related to JPUSH integration.
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
<!-- Start JPush Declarations --> <!-- Required SDK core functions--> <activity android:name="cn.jpush.android.ui.PushActivity" android:configChanges="orientation|keyboardHidden" android:exported="false" android:theme="@android:style/Theme.NoTitleBar"> <intent-filter> <action android:name="cn.jpush.android.ui.PushActivity" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="${applicationId}" /> </intent-filter> </activity> <!-- Required SDK core functions--> <service android:name="cn.jpush.android.service.DownloadService" android:enabled="true" android:exported="false" /> <!-- Required SDK core functions--> <!-- Option since 2.0.5 can be configured PushService, DaemonService, PushReceiver, AlarmReceiver the android: process parameters JPush related components to a separate process--> <!-- 如:android:process=":remote" --> <service android:name="cn.jpush.android.service.PushService" android:enabled="true" android:exported="false"> <intent-filter> <action android:name="cn.jpush.android.intent.REGISTER" /> <action android:name="cn.jpush.android.intent.REPORT" /> <action android:name="cn.jpush.android.intent.PushService" /> <action android:name="cn.jpush.android.intent.PUSH_TIME" /> </intent-filter> </service> <!-- Required SDK 核心功能 since 1.8.0 --> <service android:name="cn.jpush.android.service.DaemonService" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="cn.jpush.android.intent.DaemonService" /> <category android:name="${applicationId}" /> </intent-filter> </service> <!-- Required SDK核心功能--> <receiver android:name="cn.jpush.android.service.PushReceiver" android:enabled="true" android:exported="false"> <intent-filter android:priority="1000"> <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" /> <!--Required 显示通知栏 --> <category android:name="${applicationId}" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.USER_PRESENT" /> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> <!-- Optional --> <intent-filter> <action android:name="android.intent.action.PACKAGE_ADDED" /> <action android:name="android.intent.action.PACKAGE_REMOVED" /> <data android:scheme="package" /> </intent-filter> </receiver> <!-- Required SDK核心功能--> <receiver android:name="cn.jpush.android.service.AlarmReceiver" /> <!-- User defined. 用户自定义的广播接收器--> <receiver android:name=".JPushReceiver" android:enabled="true"> <intent-filter> <action android:name="cn.jpush.android.intent.REGISTRATION" /> <!--Required 用户注册SDK的intent--> <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /> <!--Required 用户接收SDK消息的intent--> <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /> <!--Required 用户接收SDK通知栏信息的intent--> <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <!--Required 用户打开自定义通知栏的intent--> <action android:name="cn.jpush.android.intent.ACTION_RICHPUSH_CALLBACK" /> <!--Optional 用户接受Rich Push Javascript 回调函数的intent--> <action android:name="cn.jpush.android.intent.CONNECTION" /><!-- 接收网络变化 连接/断开 since 1.6.3 --> <category android:name="${applicationId}" /> </intent-filter> </receiver> <!-- Required . Enable it you can get statistics data with channel --> <meta-data android:name="JPUSH_CHANNEL" android:value="developer-default" /> <meta-data android:name="JPUSH_APPKEY" android:value="XXXXXXXXXXXXXXXXXXXXXXXXXXX" /> <!-- </>值来自开发者平台取得的AppKey--> <!-- End JPush Declarations --> </application> |
Initialize JPushInterface in Application class
1 2 3 4 5 6 7 8 9 10 11 |
public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); JPushInterface.setDebugMode(true); JPushInterface.init(this); } } |
Creating JPushReciever
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 |
public class JPushReceiver extends BroadcastReceiver { private static String printBundle(Bundle bundle) { try { StringBuilder sb = new StringBuilder(); for (String key : bundle.keySet()) { sb.append("\nkey:" + key + ", value:" + bundle.getString(key)); } return sb.toString(); } catch (Exception e) { e.printStackTrace(); } return ""; } @Override public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); Log.d("TAG", "onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle)); if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) { } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) { Toast.makeText(context, "ACTION_MESSAGE_RECEIVED", Toast.LENGTH_SHORT).show(); } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) { Toast.makeText(context, "ACTION_NOTIFICATION_RECEIVED", Toast.LENGTH_SHORT).show(); } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) { Toast.makeText(context, "ACTION_NOTIFICATION_OPENED", Toast.LENGTH_SHORT).show(); } else { Log.d("TAG", "Unhandled intent - " + intent.getAction()); } } } |
Adding jniLibs
You are required to jniLibs in the project. You can find them in the downloaded sdk check sample image below for reference:
Location of these library inside your project is
Add following line in your module/app level gradle for building jniLibs in your project:
1 2 3 4 5 |
sourceSets { main { jniLibs.srcDirs=['libs'] } } |
Pushing notification from JPush panel
Login at https://www.jiguang.cn/ and create application with the proper application Id.
Here is your sample application detail after you successfully create the application
Application details will contatin a APP_KEY and SECRET_MASTER. use APP_KEY in the android manifest’s meta data for JPush.
SECRET_KEY is used for server side client for REST implementation.
SERVER SIDE IMPLEMENTATION
Here is ready baked code for pushing notification from your PHP script.
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 |
<?php /** * 该示例主要为JPush Push API的调用示例 * HTTP API文档:http://docs.jpush.io/server/rest_api_v3_push/ * PHP API文档:https://github.com/jpush/jpush-api-php-client/blob/master/doc/api.md#push-api--构建推送pushpayload */ ini_set("display_errors", "On"); error_reporting(E_ALL | E_STRICT); require_once("JPush.php"); echo "START"; $br = '<br/>'; echo $br; $app_key = 'XXXXXXXXXXXXXXXX'; $master_secret = 'XXXXXXXXXXXXXXXX'; // 初始化 $client = new JPush($app_key, $master_secret); // Push simple example echo "Push simple example"; $result = $client->push() ->setPlatform('all') ->addAllAudience() // ->setNotificationAlert('Hi, Shubham') // ->addAndroidNotification('Hi, android notification', 'notification title', 1, array("Notification key1"=>"Notification value1", "Notification key2"=>"Notification value2")) ->setMessage("msg content", 'msg title', 'type', array( "Message key1"=>"Message value1", "Message key2"=>"Message value2", "store_id"=>"1", "Message key2"=>"Message value2", )) ->setOptions(100000, 3600, null, false) // addAndroidNotification ( $ Alert = null , $ title = null , $ builderId = null , $ Extras = null ) // ->setOptions(100000, 3600, null, false) ->send(); echo 'Result=' . json_encode($result) . $br; |
You can refer to https://github.com/jpush/jpush-api-php-client/blob/master/examples/push_example.php for complete example and docs.
OTHER USEFUL LINKS AND FURTHER READING
http://community.jiguang.cn/
http://docs.jpush.cn/display/dev/Index
Common Issue:
http://community.jiguang.cn/t/jpushglobal-get-sdk-version-fail-sdk/4222
Stay updated !!! Keep coding and commenting