96 lines
3.8 KiB
Java
Raw Normal View History

2013-10-18 11:14:33 +08:00
package cn.jpush.phonegap;
2017-03-16 10:56:04 +08:00
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
2013-10-18 11:14:33 +08:00
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
2017-03-16 10:56:04 +08:00
2013-10-18 11:14:33 +08:00
import cn.jpush.android.api.JPushInterface;
public class MyReceiver extends BroadcastReceiver {
2017-03-16 10:56:04 +08:00
2016-03-10 13:42:09 +08:00
private static final List<String> IGNORED_EXTRAS_KEYS =
Arrays.asList(
"cn.jpush.android.TITLE",
"cn.jpush.android.MESSAGE",
"cn.jpush.android.APPKEY",
"cn.jpush.android.NOTIFICATION_CONTENT_TITLE"
);
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
2017-03-16 10:56:04 +08:00
if (action.equals(JPushInterface.ACTION_REGISTRATION_ID)) {
String rId = intent.getStringExtra(JPushInterface.EXTRA_REGISTRATION_ID);
JPushPlugin.transmitReceiveRegistrationId(rId);
} else if (action.equals(JPushInterface.ACTION_MESSAGE_RECEIVED)) {
2016-04-12 11:23:44 +08:00
handlingMessageReceive(intent);
2017-03-16 10:56:04 +08:00
} else if (action.equals(JPushInterface.ACTION_NOTIFICATION_RECEIVED)) {
2016-03-10 13:42:09 +08:00
handlingNotificationReceive(context, intent);
2017-03-16 10:56:04 +08:00
} else if (action.equals(JPushInterface.ACTION_NOTIFICATION_OPENED)) {
2016-03-10 13:42:09 +08:00
handlingNotificationOpen(context, intent);
2013-10-18 11:14:33 +08:00
}
2016-03-10 13:42:09 +08:00
}
2016-04-12 11:23:44 +08:00
private void handlingMessageReceive(Intent intent) {
2016-03-10 13:42:09 +08:00
String msg = intent.getStringExtra(JPushInterface.EXTRA_MESSAGE);
Map<String, Object> extras = getNotificationExtras(intent);
JPushPlugin.transmitMessageReceive(msg, extras);
2016-03-10 13:42:09 +08:00
}
private void handlingNotificationOpen(Context context, Intent intent) {
String title = intent.getStringExtra(JPushInterface.EXTRA_NOTIFICATION_TITLE);
JPushPlugin.openNotificationTitle = title;
2016-03-10 13:42:09 +08:00
String alert = intent.getStringExtra(JPushInterface.EXTRA_ALERT);
JPushPlugin.openNotificationAlert = alert;
Map<String, Object> extras = getNotificationExtras(intent);
JPushPlugin.openNotificationExtras = extras;
JPushPlugin.transmitNotificationOpen(title, alert, extras);
2016-03-10 13:42:09 +08:00
Intent launch = context.getPackageManager().getLaunchIntentForPackage(
2017-03-16 10:56:04 +08:00
context.getPackageName());
2016-03-10 13:42:09 +08:00
launch.addCategory(Intent.CATEGORY_LAUNCHER);
launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(launch);
}
private void handlingNotificationReceive(Context context, Intent intent) {
Intent launch = context.getPackageManager().getLaunchIntentForPackage(
context.getPackageName());
2016-03-10 13:42:09 +08:00
launch.addCategory(Intent.CATEGORY_LAUNCHER);
launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
String title = intent.getStringExtra(JPushInterface.EXTRA_NOTIFICATION_TITLE);
JPushPlugin.notificationTitle = title;
2016-03-10 13:42:09 +08:00
String alert = intent.getStringExtra(JPushInterface.EXTRA_ALERT);
JPushPlugin.notificationAlert = alert;
Map<String, Object> extras = getNotificationExtras(intent);
JPushPlugin.notificationExtras = extras;
JPushPlugin.transmitNotificationReceive(title, alert, extras);
2016-03-10 13:42:09 +08:00
}
private Map<String, Object> getNotificationExtras(Intent intent) {
Map<String, Object> extrasMap = new HashMap<String, Object>();
for (String key : intent.getExtras().keySet()) {
if (!IGNORED_EXTRAS_KEYS.contains(key)) {
if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
extrasMap.put(key, intent.getIntExtra(key, 0));
} else {
extrasMap.put(key, intent.getStringExtra(key));
}
}
}
return extrasMap;
}
2013-10-18 11:14:33 +08:00
}