jcenter 快速集成 极光推送 具体步骤
来源:互联网 发布:al软件平面设计教程 编辑:程序博客网 时间:2024/06/05 18:52
前言:项目要用到推送,自己集成一下,记录具体步骤。
官网:https://docs.jiguang.cn/jpush/client/Android/android_guide/#jcenter
1、按照官网说法进行配置:
2、配置完成后,点击 Sync Now,开始自动集成各种配置。
3、在 Application,进行初始化配置
JPushInterface.setDebugMode(true); JPushInterface.init(this);
5、但是这还不够,我们如果有接收自定义消息的需求,步骤如下:
A:在Demo中找到 MyReceiver ,和 MainActivity 参考里面的写法,记得在 清单文件注册 MyReceiver。这几个文件 参考极光Demo
B:代码如下
主要是接收广播及广播再转发
import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.support.v4.content.LocalBroadcastManager;import android.text.TextUtils;import android.util.Log;import org.json.JSONException;import org.json.JSONObject;import java.util.Iterator;import cn.jpush.android.api.JPushInterface;import vip.sinmore.tablerp.utils.Tool;/** * 自定义接收器 * * 如果不定义这个 Receiver,则: * 1) 默认用户会打开主界面 * 2) 接收不到自定义消息 */public class MyReceiver extends BroadcastReceiver {private static final String TAG = "JIGUANG-Example";@Overridepublic 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())) {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] 用户点击打开了通知");//打开自定义的ActivityIntent i = new Intent(context, MainActivity.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());}} 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 (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<String> it = json.keys();while (it.hasNext()) {String myKey = it.next();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 MainActivityprivate 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 (!Tool.isEmpty(extras)) {try {JSONObject extraJson = new JSONObject(extras);if (extraJson.length() > 0) {msgIntent.putExtra(MainActivity.KEY_EXTRAS, extras);}} catch (JSONException e) {}}LocalBroadcastManager.getInstance(context).sendBroadcast(msgIntent);}}}
清单文件
<!-- User defined. For test only 用户自定义的广播接收器--> <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.CONNECTION" /><!-- 接收网络变化 连接/断开 since 1.6.3 --> <category android:name="vip.sinmore.tablerp" /> </intent-filter> </receiver>
MainActivity
public static boolean isForeground = false; //是否是前台该参数判断当前Ac是否是在前台显示,如果不在,那就接收广播不进行操作
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); registerMessageReceiver(); }
@Override protected void onResume() { isForeground = true; super.onResume(); } @Override protected void onPause() { isForeground = false; super.onPause(); }
@Override protected void onDestroy() { LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver); super.onDestroy(); }
//for receive customer msg from jpush server private MessageReceiver mMessageReceiver; public static final String MESSAGE_RECEIVED_ACTION = "com.example.jpushdemo.MESSAGE_RECEIVED_ACTION"; public static final String KEY_TITLE = "title"; public static final String KEY_MESSAGE = "message"; public static final String KEY_EXTRAS = "extras"; public void registerMessageReceiver() { mMessageReceiver = new MessageReceiver(); IntentFilter filter = new IntentFilter(); filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); filter.addAction(MESSAGE_RECEIVED_ACTION); LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, filter); } public class MessageReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { try { if (MESSAGE_RECEIVED_ACTION.equals(intent.getAction())) { String messge = intent.getStringExtra(KEY_MESSAGE); String extras = intent.getStringExtra(KEY_EXTRAS); StringBuilder showMsg = new StringBuilder(); showMsg.append(KEY_MESSAGE + " : " + messge + "\n"); if (!Tool.isEmpty(extras)) { showMsg.append(KEY_EXTRAS + " : " + extras + "\n"); } //下一步就是拿到这个消息进行解析,再进行分类,再进行解析操作对应动作,这里先只是提示消息 Tool.showToast(MainActivity.this, showMsg.toString()); } } catch (Exception e) { } } }
阅读全文
0 0
- jcenter 快速集成 极光推送 具体步骤
- 极光推送jcenter 自动集成
- 快速集成极光推送
- 极光推送快速集成
- Android Studio jcenter 自动集成极光推送
- vmei-day04-Jcenter方式集成极光推送
- 极光推送jcenter集成的坑
- iOS极光推送-具体步骤
- android jpush(极光推送)快速集成
- iOS极光推送集成
- Android 极光推送集成
- iOS极光推送集成
- AndroidStudio集成极光推送
- 极光推送集成
- Ionic 集成极光推送
- iOS极光推送集成
- iOS集成极光推送
- android极光推送集成
- 17101101_Windows系统通过FillZilla上传文件到linux系统方法
- Mongoose OS 的mos命令
- VS 2013 VS 2015的卸载方法& VS 2015社区版的安装失败记录
- 利用ThreadLocal管理request和session以及用户信息,实现 Use anywhere
- 《Effective C++》条款06
- jcenter 快速集成 极光推送 具体步骤
- iOS 开发转战ASP.NET 的过程
- volatile用法
- Linux下基于V4L2 USB Camera操作
- Eclipse插件TFS的使用 类似SVN 但是功能更强大,适应就很好用,能防止冲突使用文档在资源中
- mongodb 启动报错的几种情况与办法
- web网站架构演变过程
- Kolla-Ansible Deploy Multimode(openstack)
- 最大利润问题(深搜+树形DP)