在项目中用到了极光推送,所以把项目中做过的分享一下:
1.下载极光推送所需要的包,如:
2.在AndroidManifest中配置权限和类,如下:
3.在Application里面注册,也就是初始化
JPushInterface.setDebugMode(true); // 设置开启日志,发布时请关闭日志 JPushInterface.init(context.getApplicationContext()); // 初始化 JPush
4.自定义的广播接受者的代码如下,这里加的代码都是我自己加的,写法不一,这里可以接受到推送的消息,点击跳转自定义的界面:
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); } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) { Log.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE)); } 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 chooseMyActivity(context, bundle); } 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 { sb.append("\nkey:" + key + ", value:" + bundle.getString(key)); } } return sb.toString(); } // send msg to MyActivity private void chooseMyActivity(Context context, Bundle bundle) { // 打开自定义的Activity try { JSONObject jo = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA)); int code = Integer.parseInt(jo.getString("code")); bundle.clear(); switch (code) { case CommonData.JPUSH_URL://100:url, bundle.putString("Code", CommonData.JPUSH_URL + ""); bundle.putString("url", jo.getString("param")); startMyActivity(context, bundle, WebViewActivity.class); break; case CommonData.JPUSH_TXT://101:纯文本 bundle.putString("Code", CommonData.JPUSH_TXT + ""); bundle.putString("param", jo.getString("param")); startMyActivity(context, bundle, TxtActivity.class); break; default: break; } } } catch (JSONException e) { e.printStackTrace(); } } private void startMyActivity(Context context, Bundle bundle, Class> name) { Intent i = new Intent(context, name); i.putExtras(bundle); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); }
以上就是极光推送的内容了,但是要实现自定义消息的推送,这就是你和你们后台的事了,如别名、自定义通知栏样式等等。这里就不一一写出了。