MyReceiver.java 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package cn.jiguang.cordova.push;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import java.util.Arrays;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import cn.jpush.android.api.JPushInterface;
  10. public class MyReceiver extends BroadcastReceiver {
  11. private static final List<String> IGNORED_EXTRAS_KEYS =
  12. Arrays.asList(
  13. "cn.jpush.android.TITLE",
  14. "cn.jpush.android.MESSAGE",
  15. "cn.jpush.android.APPKEY",
  16. "cn.jpush.android.NOTIFICATION_CONTENT_TITLE"
  17. );
  18. @Override
  19. public void onReceive(Context context, Intent intent) {
  20. String action = intent.getAction();
  21. if (action.equals(JPushInterface.ACTION_REGISTRATION_ID)) {
  22. String rId = intent.getStringExtra(JPushInterface.EXTRA_REGISTRATION_ID);
  23. JPushPlugin.transmitReceiveRegistrationId(rId);
  24. } else if (action.equals(JPushInterface.ACTION_MESSAGE_RECEIVED)) {
  25. handlingMessageReceive(intent);
  26. } else if (action.equals(JPushInterface.ACTION_NOTIFICATION_RECEIVED)) {
  27. handlingNotificationReceive(context, intent);
  28. } else if (action.equals(JPushInterface.ACTION_NOTIFICATION_OPENED)) {
  29. handlingNotificationOpen(context, intent);
  30. }
  31. }
  32. private void handlingMessageReceive(Intent intent) {
  33. String msg = intent.getStringExtra(JPushInterface.EXTRA_MESSAGE);
  34. Map<String, Object> extras = getNotificationExtras(intent);
  35. JPushPlugin.transmitMessageReceive(msg, extras);
  36. }
  37. private void handlingNotificationOpen(Context context, Intent intent) {
  38. String title = intent.getStringExtra(JPushInterface.EXTRA_NOTIFICATION_TITLE);
  39. JPushPlugin.openNotificationTitle = title;
  40. String alert = intent.getStringExtra(JPushInterface.EXTRA_ALERT);
  41. JPushPlugin.openNotificationAlert = alert;
  42. Map<String, Object> extras = getNotificationExtras(intent);
  43. JPushPlugin.openNotificationExtras = extras;
  44. JPushPlugin.transmitNotificationOpen(title, alert, extras);
  45. Intent launch = context.getPackageManager().getLaunchIntentForPackage(
  46. context.getPackageName());
  47. launch.addCategory(Intent.CATEGORY_LAUNCHER);
  48. launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
  49. context.startActivity(launch);
  50. }
  51. private void handlingNotificationReceive(Context context, Intent intent) {
  52. Intent launch = context.getPackageManager().getLaunchIntentForPackage(
  53. context.getPackageName());
  54. launch.addCategory(Intent.CATEGORY_LAUNCHER);
  55. launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
  56. String title = intent.getStringExtra(JPushInterface.EXTRA_NOTIFICATION_TITLE);
  57. JPushPlugin.notificationTitle = title;
  58. String alert = intent.getStringExtra(JPushInterface.EXTRA_ALERT);
  59. JPushPlugin.notificationAlert = alert;
  60. Map<String, Object> extras = getNotificationExtras(intent);
  61. JPushPlugin.notificationExtras = extras;
  62. JPushPlugin.transmitNotificationReceive(title, alert, extras);
  63. }
  64. private Map<String, Object> getNotificationExtras(Intent intent) {
  65. Map<String, Object> extrasMap = new HashMap<String, Object>();
  66. for (String key : intent.getExtras().keySet()) {
  67. if (!IGNORED_EXTRAS_KEYS.contains(key)) {
  68. if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
  69. extrasMap.put(key, intent.getIntExtra(key, 0));
  70. } else {
  71. extrasMap.put(key, intent.getStringExtra(key));
  72. }
  73. }
  74. }
  75. return extrasMap;
  76. }
  77. }