1.
defaultConfig {
//#################### 极光推送 ###################
ndk {
//选择要添加的对应cpu类型的.so库。
abiFilters 'armeabi', 'armeabi-v7a', 'armeabi-v8a', 'x86'
// 还可以添加 'x86', 'x86_64', 'mips', 'mips64'
}
manifestPlaceholders = [
JPUSH_PKGNAME: applicationId,
JPUSH_APPKEY : "547383f7d5a5205a66482c2d", //JPush上注册的包名对应的appkey.
JPUSH_CHANNEL: "developer-default", //暂时填写默认值即可.
]
//#################### 极光推送 ###################
}
2.依赖
//#################### 极光推送 ###################
compile 'cn.jiguang.sdk:jpush:3.1.1'
compile 'cn.jiguang.sdk:jcore:1.1.9'
//#################### 极光推送 ###################
3.自定义接受类
public class MyJpushReceiver extends BroadcastReceiver {
private static final String TAG = "MyReceiver";
private NotificationManager nm;
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if (null == nm) {
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}
// 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())) {
// Log.d(TAG, "接受到推送下来的自定义消息");
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
// Log.d(TAG, "用户点击打开了通知");
//打开自定义的Activity
Intent i = new Intent(context, MainActivity.class);
i.putExtras(bundle);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
// openNotification(context,bundle);
} else {
Log.d(TAG, "Unhandled intent - " + intent.getAction());
}
}
private void receivingNotification(Context context, Bundle bundle) {
String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
// Log.d(TAG, " title : " + title);
String message = bundle.getString(JPushInterface.EXTRA_ALERT);
// Log.d(TAG, "message : " + message);
String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
// Log.d(TAG, "extras : " + extras);
}
private void openNotification(Context context, Bundle bundle) {
String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
String myValue = "";
try {
JSONObject extrasJson = new JSONObject(extras);
myValue = extrasJson.optString("myKey");
} catch (Exception e) {
// Log.w(TAG, "Unexpected: extras is not a valid json", e);
return;
}
// if (TYPE_THIS.equals(myValue)) {
// Intent mIntent = new Intent(context, ThisActivity.class);
// mIntent.putExtras(bundle);
// mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// context.startActivity(mIntent);
// } else if (TYPE_ANOTHER.equals(myValue)){
// Intent mIntent = new Intent(context, AnotherActivity.class);
// mIntent.putExtras(bundle);
// mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// context.startActivity(mIntent);
// }
}
// 打印所有的 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 (TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA))) {
Log.i(TAG, "This message has no Extra data");
continue;
}
try {
JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
Iterator 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();
}
}
4.清单
5.Application
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
//极光推送调试模式
JPushInterface.setDebugMode(false);
//极光推送初始化
JPushInterface.init(this);
}
}
6.针对用户人群进行推送,使用唯一标识RegistrationID
http://blog.jiguang.cn/registrationid/
7.权限
8.修改清单里边的包名,有一个叫package的不用管.
CV完毕,收功.已兼容8.0
9.考虑到没有做过分层次推送
https://blog.csdn.net/qq_34536167/article/details/78946437