极光推送(JPush)是一个端到端的推送服务,使得服务器端消息能够及时地推送到终端用户手机上,让开发者积极地保持与用户的连接,从而提高用户活跃度、提高应用的留存率。接下在咱们就介绍一下基于 JPush 来快捷地为 Android App 增加推送功能。首先说一下极光推送的主要功能。
1.保持与服务器的长连接,以便消息能够即时推送到达客户端
2.接收通知与自定义消息,并向开发者App 传递相关信息
咱们今天主要介绍的是使用jenter自动集成的步骤
1.在 module 的 gradle 中添加依赖和AndroidManifest的替换变量。
android {
......
defaultConfig {
applicationId "com.xxx.xxx" //JPush上注册的包名.
......
ndk {
//选择要添加的对应cpu类型的.so库。
abiFilters 'armeabi', 'armeabi-v7a', 'armeabi-v8a'
// 还可以添加 'x86', 'x86_64', 'mips', 'mips64'
}
manifestPlaceholders = [
JPUSH_PKGNAME : applicationId,
JPUSH_APPKEY : "你的appkey", //JPush上注册的包名对应的appkey.
JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可.
]
......
}
......
}
dependencies {
......
compile 'cn.jiguang.sdk:jpush:3.0.3' // 此处以JPush 3.0.3 版本为例。
compile 'cn.jiguang.sdk:jcore:1.1.1' // 此处以JCore 1.1.1 版本为例。
......
}
2.AndroidManifest.xml中的配置
在Application节点下,Activity的同级目录
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="false">
<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="com.example.admin.jgtest2" />
</intent-filter>
</receiver>
3.新建MyReceiver类
package com.example.admin.jgtest2;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Iterator;
import cn.jpush.android.api.JPushInterface;
/**
* 自定义接收器
*
* 如果不定义这个 Receiver,则:
* 1) 默认用户会打开主界面
* 2) 接收不到自定义消息
*/
public class MyReceiver extends BroadcastReceiver {
private static final String TAG = "JPush";
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Log.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));
if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);
//send the Registration Id to your server...
} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
Log.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
processCustomMessage(context, bundle);
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
Log.d(TAG, "[MyReceiver] 接收到推送下来的通知");
int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
Log.d(TAG, "[MyReceiver] 用户点击打开了通知");
//
// //打开自定义的Activity
// Intent i = new Intent(context, TestActivity.class);
// i.putExtras(bundle);
// //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
// context.startActivity(i);
} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
Log.d(TAG, "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
//在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码,比如打开新的Activity, 打开一个网页等..
} else if(JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
Log.w(TAG, "[MyReceiver]" + intent.getAction() +" connected state change to "+connected);
} else {
Log.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
}
}
// 打印所有的 intent extra 数据
private static String printBundle(Bundle bundle) {
StringBuilder sb = new StringBuilder();
for (String key : bundle.keySet()) {
if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
}else if(key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)){
sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
} else if (key.equals(JPushInterface.EXTRA_EXTRA)) {
if (bundle.getString(JPushInterface.EXTRA_EXTRA).isEmpty()) {
Log.i(TAG, "This message has no Extra data");
continue;
}
try {
JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
Iterator<String> it = json.keys();
while (it.hasNext()) {
String myKey = it.next().toString();
sb.append("\nkey:" + key + ", value: [" +
myKey + " - " +json.optString(myKey) + "]");
}
} catch (JSONException e) {
Log.e(TAG, "Get message extra JSON error!");
}
} else {
sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
}
}
return sb.toString();
}
//send msg to MainActivity
private void processCustomMessage(Context context, Bundle bundle) {
// if (MainActivity.isForeground) {
// String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
// String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
// Intent msgIntent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION);
// msgIntent.putExtra(MainActivity.KEY_MESSAGE, message);
// if (!ExampleUtil.isEmpty(extras)) {
// try {
// JSONObject extraJson = new JSONObject(extras);
// if (null != extraJson && extraJson.length() > 0) {
// msgIntent.putExtra(MainActivity.KEY_EXTRAS, extras);
// }
// } catch (JSONException e) {
//
// }
//
// }
// context.sendBroadcast(msgIntent);
// }
}
}
4.初始化SDK(放到初始化View之前)
//初始化sdk
JPushInterface.setDebugMode(true);//正式版的时候设置false,关闭调试
JPushInterface.init(this);
//建议添加tag标签,发送消息的之后就可以指定tag标签来发送了
Set<String> set = new HashSet<>();
set.add("andfixdemo");//名字任意,可多添加几个,能区别就好了
JPushInterface.setTags(this, set, null);//设置标签
5.模拟服务端进行消息或者通知的推送
本来这个活是应该服务器端干的,但是我们要测试完成这个功能,我们就要模拟服务端给我们发送推送。
进入开发者服务中心,创建应用,包名默认是咱们上面创建的应用的包名(一定不能写错了),创建应用之后点击该应用进入详细页,点击推送选项卡进入推送页面,然后就可以进行消息或者通知的推送了,有不明白的欢迎留言。。