aabe4b7ebc2b00f4a9802ef7385d87e7ee5886cb.svn-base 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package eVVM.apk.jpush;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.text.TextUtils;
  7. import org.json.JSONException;
  8. import org.json.JSONObject;
  9. import java.util.Iterator;
  10. import cn.jpush.android.api.JPushInterface;
  11. import cn.jpush.android.helper.Logger;
  12. import eVVM.apk.helper.SPUtils;
  13. /**
  14. * 自定义接收器
  15. *
  16. * 如果不定义这个 Receiver,则:
  17. * 1) 默认用户会打开主界面
  18. * 2) 接收不到自定义消息
  19. */
  20. public class MyReceiver extends BroadcastReceiver {
  21. private static final String TAG = "JIGUANG-Example";
  22. @Override
  23. public void onReceive(Context context, Intent intent) {
  24. try {
  25. Bundle bundle = intent.getExtras();
  26. Logger.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));
  27. if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
  28. String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
  29. Logger.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);
  30. //send the Registration Id to your server...
  31. SPUtils.put("pushToken",regId);
  32. } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
  33. Logger.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
  34. processCustomMessage(context, bundle);
  35. } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
  36. Logger.d(TAG, "[MyReceiver] 接收到推送下来的通知");
  37. int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
  38. Logger.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);
  39. } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
  40. Logger.d(TAG, "[MyReceiver] 用户点击打开了通知");
  41. //打开自定义的Activity
  42. // Intent i = new Intent(context, TestActivity.class);
  43. // i.putExtras(bundle);
  44. // //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  45. // i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
  46. // context.startActivity(i);
  47. } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
  48. Logger.d(TAG, "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
  49. //在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码,比如打开新的Activity, 打开一个网页等..
  50. } else if(JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
  51. boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
  52. Logger.w(TAG, "[MyReceiver]" + intent.getAction() +" connected state change to "+connected);
  53. } else {
  54. Logger.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
  55. }
  56. } catch (Exception e){
  57. }
  58. }
  59. // 打印所有的 intent extra 数据
  60. private static String printBundle(Bundle bundle) {
  61. StringBuilder sb = new StringBuilder();
  62. for (String key : bundle.keySet()) {
  63. if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
  64. sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
  65. }else if(key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)){
  66. sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
  67. } else if (key.equals(JPushInterface.EXTRA_EXTRA)) {
  68. if (TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA))) {
  69. Logger.i(TAG, "This message has no Extra data");
  70. continue;
  71. }
  72. try {
  73. JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
  74. Iterator<String> it = json.keys();
  75. while (it.hasNext()) {
  76. String myKey = it.next();
  77. sb.append("\nkey:" + key + ", value: [" +
  78. myKey + " - " +json.optString(myKey) + "]");
  79. }
  80. } catch (JSONException e) {
  81. Logger.e(TAG, "Get message extra JSON error!");
  82. }
  83. } else {
  84. sb.append("\nkey:" + key + ", value:" + bundle.get(key));
  85. }
  86. }
  87. return sb.toString();
  88. }
  89. //send msg to MainActivity
  90. private void processCustomMessage(Context context, Bundle bundle) {
  91. // if (MainActivity.isForeground) {
  92. // String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
  93. // String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
  94. // Intent msgIntent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION);
  95. // msgIntent.putExtra(MainActivity.KEY_MESSAGE, message);
  96. // if (!ExampleUtil.isEmpty(extras)) {
  97. // try {
  98. // JSONObject extraJson = new JSONObject(extras);
  99. // if (extraJson.length() > 0) {
  100. // msgIntent.putExtra(MainActivity.KEY_EXTRAS, extras);
  101. // }
  102. // } catch (JSONException e) {
  103. //
  104. // }
  105. //
  106. // }
  107. // LocalBroadcastManager.getInstance(context).sendBroadcast(msgIntent);
  108. // }
  109. }
  110. }