Android极光推送(Android(安卓)studio 3.0+)
16lz
2021-01-25
使用步骤:
Step 1.创建应用:
进入极光控制台后,点击“创建应用”按钮,进入创建应用的界面。填上你的应用程序的名称以及应用包名这二项就可以了,最后点击最下方的 “创建我的应用”按钮,创建应用完毕。
Step 2. 根目录的主 gradle 中默认配置了jcenter支持(默认支持ndk),所以直接在 module 的 gradle 中添加依赖和AndroidManifest的替换变量即可
android { ...... defaultConfig { applicationId "com.xxx.xxx" //要和JPush上注册的包名一致 ...... ndk { //选择要添加的对应cpu类型的.so库。 abiFilters 'armeabi', 'armeabi-v7a', 'arm64-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.1.1' // 此处以JPush 3.1.1 版本为例。 compile 'cn.jiguang.sdk:jcore:1.1.9' // 此处以JCore 1.1.9 版本为例。 ......}
Step 3.测试:(在清单文件进行注册application)
public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); JPushInterface.setDebugMode(true); JPushInterface.init(this); }}
运行成功
Step 4.在本地的 AndroidManifest 中定义同名的组件并配置想要的属性,然后用 xmlns:tools 来控制本地组件覆盖 jcenter 上的组件
<receiver android:name=".jpush.MyReceiver" android:enabled="true" android:exported="false" tools:node="replace"> <intent-filter> <action android:name="cn.jpush.android.intent.REGISTRATION" /> <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /> <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /> <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <action android:name="cn.jpush.android.intent.CONNECTION" /> <category android:name="修改成自己app的包名" /> intent-filter>receiver>
public class MyReceiver extends BroadcastReceiver { private static final String TAG = "JIGUANG"; // RegistrationID 定义 // 集成了 JPush SDK 的应用程序在第一次成功注册到 JPush 服务器时, // JPush 服务器会给客户端返回一个唯一的该设备的标识 - RegistrationID。 // JPush SDK 会以广播的形式发送 RegistrationID 到应用程序。 // 应用程序可以把此 RegistrationID 保存以自己的应用服务器上, // 然后就可以根据 RegistrationID 来向设备推送消息或者通知。 public static String regId; @Override public void onReceive(Context context, Intent intent) { try { Bundle bundle = intent.getExtras(); Log.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle)); if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) { 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)); // 对应极光后台的 - 自定义消息 默认不会出现在notification上 所以一般都选用发送通知 } 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,ContentActivity.class); //i.putExtras(bundle); //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()); } }catch (Exception e){ } } // 打印所有的 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(); }}
Step 5.设置唯一标识(新别名alias),根据别名推送
//登录时进行绑定JPushInterface.setAlias(LoginActivity.this,int sequence,唯一标识-一般userid);//退出登录进行解除绑定(即不再接收根据别名推送的消息)JPushInterface.deleteAlias(ContentActivity.this,int sequence);
Step 6.网络权限
//这个一点要+<uses-permission android:name="android.permission.INTERNET"/>
更多相关文章
- 【android】数字签名理解
- android添加底层核心服务
- Android学习 之 应用程序基础及四大组件
- Looper,Handler,Message
- android实现权限管理和签名静默卸载
- Android(安卓)重要数据目录
- Android兼容性
- Android(安卓)Handler机制之循环消息队列的退出
- Android小项目之十 应用程序更新的签名问题