441 lines
15 KiB
Java
441 lines
15 KiB
Java
package com.gh.base;
|
||
|
||
import android.annotation.SuppressLint;
|
||
import android.app.Notification;
|
||
import android.app.NotificationManager;
|
||
import android.app.PendingIntent;
|
||
import android.content.Context;
|
||
import android.content.Intent;
|
||
import android.content.pm.ApplicationInfo;
|
||
import android.content.pm.PackageInfo;
|
||
import android.graphics.BitmapFactory;
|
||
import android.os.Build;
|
||
import android.os.Handler;
|
||
import android.os.Message;
|
||
import android.support.v4.app.NotificationCompat;
|
||
import android.support.v4.util.ArrayMap;
|
||
import android.text.TextUtils;
|
||
import android.util.Log;
|
||
import android.widget.RemoteViews;
|
||
|
||
import com.gh.common.util.FileUtils;
|
||
import com.gh.common.util.PackageUtils;
|
||
import com.gh.common.util.Utils;
|
||
import com.gh.gamecenter.R;
|
||
import com.xiaomi.mipush.sdk.ErrorCode;
|
||
import com.xiaomi.mipush.sdk.MiPushClient;
|
||
import com.xiaomi.mipush.sdk.MiPushCommandMessage;
|
||
import com.xiaomi.mipush.sdk.MiPushMessage;
|
||
import com.xiaomi.mipush.sdk.PushMessageReceiver;
|
||
|
||
import org.json.JSONArray;
|
||
import org.json.JSONException;
|
||
import org.json.JSONObject;
|
||
|
||
import java.io.File;
|
||
import java.io.IOException;
|
||
import java.text.SimpleDateFormat;
|
||
import java.util.ArrayList;
|
||
import java.util.Date;
|
||
import java.util.List;
|
||
import java.util.Locale;
|
||
|
||
/**
|
||
* 1、PushMessageReceiver是个抽象类,该类继承了BroadcastReceiver。
|
||
* 2、需要将自定义的DemoMessageReceiver注册在AndroidManifest.xml文件中 <receiver
|
||
* android:exported="true"
|
||
* android:name="com.xiaomi.mipushdemo.DemoMessageReceiver"> <intent-filter>
|
||
* <action android:name="com.xiaomi.mipush.RECEIVE_MESSAGE" /> </intent-filter>
|
||
* <intent-filter> <action android:name="com.xiaomi.mipush.ERROR" />
|
||
* </intent-filter> <intent-filter> <action
|
||
* android:name="com.xiaomi.mipush.MESSAGE_ARRIVED" /></intent-filter>
|
||
* </receiver>
|
||
* 3、DemoMessageReceiver的onReceivePassThroughMessage方法用来接收服务器向客户端发送的透传消息
|
||
* 4、DemoMessageReceiver的onNotificationMessageClicked方法用来接收服务器向客户端发送的通知消息,
|
||
* 这个回调方法会在用户手动点击通知后触发
|
||
* 5、DemoMessageReceiver的onNotificationMessageArrived方法用来接收服务器向客户端发送的通知消息,
|
||
* 这个回调方法是在通知消息到达客户端时触发。另外应用在前台时不弹出通知的通知消息到达客户端也会触发这个回调函数
|
||
* 6、DemoMessageReceiver的onCommandResult方法用来接收客户端向服务器发送命令后的响应结果
|
||
* 7、DemoMessageReceiver的onReceiveRegisterResult方法用来接收客户端向服务器发送注册命令后的响应结果
|
||
* 8、以上这些方法运行在非UI线程中
|
||
*
|
||
* @author mayixiang
|
||
*/
|
||
public class GHPushMessageReceiver extends PushMessageReceiver {
|
||
|
||
private String mRegId;
|
||
private long mResultCode = -1;
|
||
private String mReason;
|
||
private String mCommand;
|
||
private String mMessage;
|
||
private String mTopic;
|
||
private String mAlias;
|
||
private String mAccount;
|
||
private String mStartTime;
|
||
private String mEndTime;
|
||
|
||
@Override
|
||
public void onReceivePassThroughMessage(Context context,
|
||
MiPushMessage message) {
|
||
// 1判断notifyid是否为4
|
||
try {
|
||
if (message.getNotifyId() == 4) {
|
||
JSONObject jsonObject = new JSONObject(message.getContent());
|
||
Utils.log(jsonObject.toString());
|
||
String channel = jsonObject.getString("channel");
|
||
Utils.log("channel = " + channel);
|
||
// 1判断渠道号是否一致或是否为ALL
|
||
String TD_CHANNEL_ID = (String) PackageUtils.getMetaData(context, context.getPackageName(), "TD_CHANNEL_ID");
|
||
if ("ALL".equals(channel)
|
||
|| TD_CHANNEL_ID
|
||
.equalsIgnoreCase(channel)) {
|
||
String type = jsonObject.getString("type");
|
||
Utils.log("type = " + type);
|
||
if ("NEWS".equals(type)) {
|
||
// 新闻推送
|
||
JSONArray jsonArray = jsonObject
|
||
.getJSONArray("package");
|
||
ArrayMap<String, Boolean> map = getInstalledMapFromLocal(context);
|
||
for (int i = 0; i < jsonArray.length(); i++) {
|
||
Boolean b = map.get(jsonArray.getString(i));
|
||
if (b != null) {
|
||
// 显示推送的消息
|
||
showNotification(context, jsonObject, 0);
|
||
break;
|
||
}
|
||
}
|
||
} else if ("PLUGIN_UPDATE".equals(type)) {
|
||
// 插件更新推送
|
||
JSONArray jsonArray = jsonObject.getJSONArray("apk");
|
||
ArrayMap<String, Boolean> map = getInstalledMapFromLocal(context);
|
||
for (int i = 0; i < jsonArray.length(); i++) {
|
||
JSONObject apk = jsonArray.getJSONObject(i);
|
||
String packageName = apk.getString("package");
|
||
Boolean b = map.get(packageName);
|
||
if (b != null) {
|
||
// 判断是否gh_version是否相同
|
||
String gh_version = (String) PackageUtils
|
||
.getMetaData(context, packageName, "gh_version");
|
||
if (gh_version != null) {
|
||
gh_version = gh_version.substring(2);
|
||
// 判断gh_version是否相同
|
||
if (gh_version.equals(apk
|
||
.getString("gh_version"))) {
|
||
// 判断version是否相同
|
||
String version = PackageUtils
|
||
.getVersionByPackage(context,
|
||
packageName);
|
||
if (version.equals(apk
|
||
.getString("version"))) {
|
||
// 版本相同,无需显示插件更新,继续查看是否有可更新的游戏包
|
||
continue;
|
||
}
|
||
}
|
||
}
|
||
// 显示推送的消息
|
||
showNotification(context, jsonObject, 1);
|
||
break;
|
||
}
|
||
}
|
||
} else if ("NEW_GAME".equals(type)) {
|
||
// 新游推送
|
||
showNotification(context, jsonObject, 2);
|
||
}
|
||
}
|
||
}
|
||
} catch (JSONException e) {
|
||
e.printStackTrace();
|
||
}
|
||
|
||
Log.v(AppController.TAG, "onReceivePassThroughMessage is called. "
|
||
+ message.toString());
|
||
String log = context.getString(R.string.recv_passthrough_message,
|
||
message.getContent());
|
||
|
||
if (!TextUtils.isEmpty(message.getTopic())) {
|
||
mTopic = message.getTopic();
|
||
} else if (!TextUtils.isEmpty(message.getAlias())) {
|
||
mAlias = message.getAlias();
|
||
}
|
||
|
||
Message msg = Message.obtain();
|
||
msg.obj = log;
|
||
AppController.getHandler().sendMessage(msg);
|
||
}
|
||
|
||
private void showNotification(Context context, JSONObject jsonObject, int id)
|
||
throws JSONException {
|
||
Intent intent = new Intent();
|
||
intent.setAction("com.gh.gamecenter.NOTIFICATION");
|
||
intent.putExtra("notifyId", id);
|
||
intent.putExtra("notifyData", jsonObject.toString());
|
||
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id,
|
||
intent, PendingIntent.FLAG_ONE_SHOT);
|
||
|
||
NotificationManager nManager = (NotificationManager) context
|
||
.getSystemService(Context.NOTIFICATION_SERVICE);
|
||
|
||
Notification notification = new NotificationCompat.Builder(context)
|
||
.setSmallIcon(R.drawable.logo)
|
||
.setTicker(jsonObject.getString("pushTitle"))
|
||
.setContentTitle(jsonObject.getString("pushTitle"))
|
||
.setContentText(jsonObject.getString("pushDesc"))
|
||
.setContentIntent(pendingIntent).build();
|
||
|
||
RemoteViews remoteViews = null;
|
||
|
||
if (Build.MANUFACTURER.equals("Meizu")
|
||
&& (Build.MODEL.startsWith("m") || Build.MODEL.startsWith("MX"))) {
|
||
remoteViews = new RemoteViews(context.getPackageName(),
|
||
R.layout.notification_meizu);
|
||
SimpleDateFormat format = new SimpleDateFormat("HH:mm",
|
||
Locale.getDefault());
|
||
remoteViews.setTextViewText(R.id.time, format.format(new Date()));
|
||
} else if (Build.MANUFACTURER.equals("Xiaomi")
|
||
&& (Build.MODEL.startsWith("MI")
|
||
|| Build.MODEL.startsWith("HM") || Build.MODEL
|
||
.startsWith("Redmi"))) {
|
||
// 小米系统
|
||
remoteViews = new RemoteViews(context.getPackageName(),
|
||
R.layout.notification_xiaomi);
|
||
SimpleDateFormat format = new SimpleDateFormat("ah:mm",
|
||
Locale.getDefault());
|
||
remoteViews.setTextViewText(R.id.time, format.format(new Date()));
|
||
} else if (Build.MANUFACTURER.equals("HUAWEI")) {
|
||
// 华为系统
|
||
remoteViews = new RemoteViews(context.getPackageName(),
|
||
R.layout.notification_huawei);
|
||
}
|
||
|
||
String url = jsonObject.getString("icon");
|
||
String path = context.getCacheDir() + File.separator
|
||
+ url.substring(url.lastIndexOf("/") + 1);
|
||
try {
|
||
FileUtils.downloadFile(url, path);
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}
|
||
if (remoteViews != null) {
|
||
remoteViews.setImageViewBitmap(R.id.icon,
|
||
BitmapFactory.decodeFile(path));
|
||
// remoteViews.setImageViewResource(R.id.icon, R.drawable.me_icon);
|
||
remoteViews.setTextViewText(R.id.title,
|
||
jsonObject.getString("pushTitle"));
|
||
remoteViews.setTextViewText(R.id.intro,
|
||
jsonObject.getString("pushDesc"));
|
||
notification.contentView = remoteViews;
|
||
} else {
|
||
notification = new NotificationCompat.Builder(context)
|
||
.setSmallIcon(R.drawable.logo_black)
|
||
.setTicker(jsonObject.getString("pushTitle"))
|
||
.setContentTitle(jsonObject.getString("pushTitle"))
|
||
.setContentText(jsonObject.getString("pushDesc"))
|
||
.setContentIntent(pendingIntent)
|
||
.setLargeIcon(BitmapFactory.decodeFile(path)).build();
|
||
}
|
||
|
||
notification.defaults = Notification.DEFAULT_SOUND;// 添加系统默认声音
|
||
notification.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。
|
||
nManager.notify(((int) System.currentTimeMillis() / 1000), notification);// 通过通知管理器来发起通知。如果id不同,则每click,在status哪里增加一个提示
|
||
}
|
||
|
||
private ArrayMap<String, Boolean> getInstalledMapFromLocal(Context context) {
|
||
ArrayMap<String, Boolean> map = new ArrayMap<String, Boolean>();
|
||
ArrayList<String> list = getAllPackageName(context);
|
||
for (String str : list) {
|
||
map.put(str, true);
|
||
}
|
||
return map;
|
||
}
|
||
|
||
private ArrayList<String> getAllPackageName(Context context) {
|
||
ArrayList<String> list = new ArrayList<String>();
|
||
List<PackageInfo> packageInfos = context.getPackageManager()
|
||
.getInstalledPackages(0);
|
||
for (int i = 0, size = packageInfos.size(); i < size; i++) {
|
||
PackageInfo packageInfo = packageInfos.get(i);
|
||
if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
|
||
list.add(packageInfo.packageName);
|
||
}
|
||
}
|
||
return list;
|
||
}
|
||
|
||
@Override
|
||
public void onNotificationMessageClicked(Context context,
|
||
MiPushMessage message) {
|
||
Log.v(AppController.TAG, "onNotificationMessageClicked is called. "
|
||
+ message.toString());
|
||
String log = context.getString(R.string.click_notification_message,
|
||
message.getContent());
|
||
|
||
if (!TextUtils.isEmpty(message.getTopic())) {
|
||
mTopic = message.getTopic();
|
||
} else if (!TextUtils.isEmpty(message.getAlias())) {
|
||
mAlias = message.getAlias();
|
||
}
|
||
|
||
Message msg = Message.obtain();
|
||
if (message.isNotified()) {
|
||
msg.obj = log;
|
||
}
|
||
AppController.getHandler().sendMessage(msg);
|
||
|
||
}
|
||
|
||
@Override
|
||
public void onNotificationMessageArrived(Context context,
|
||
MiPushMessage message) {
|
||
Log.v(AppController.TAG, "onNotificationMessageArrived is called. "
|
||
+ message.toString());
|
||
String log = context.getString(R.string.arrive_notification_message,
|
||
message.getContent());
|
||
|
||
if (!TextUtils.isEmpty(message.getTopic())) {
|
||
mTopic = message.getTopic();
|
||
} else if (!TextUtils.isEmpty(message.getAlias())) {
|
||
mAlias = message.getAlias();
|
||
}
|
||
|
||
Message msg = Message.obtain();
|
||
msg.obj = log;
|
||
AppController.getHandler().sendMessage(msg);
|
||
}
|
||
|
||
@Override
|
||
public void onCommandResult(Context context, MiPushCommandMessage message) {
|
||
Log.v(AppController.TAG,
|
||
"onCommandResult is called. " + message.toString());
|
||
String command = message.getCommand();
|
||
List<String> arguments = message.getCommandArguments();
|
||
String cmdArg1 = ((arguments != null && arguments.size() > 0) ? arguments
|
||
.get(0) : null);
|
||
String cmdArg2 = ((arguments != null && arguments.size() > 1) ? arguments
|
||
.get(1) : null);
|
||
String log = "";
|
||
if (MiPushClient.COMMAND_REGISTER.equals(command)) {
|
||
if (message.getResultCode() == ErrorCode.SUCCESS) {
|
||
mRegId = cmdArg1;
|
||
log = context.getString(R.string.register_success);
|
||
} else {
|
||
log = context.getString(R.string.register_fail);
|
||
}
|
||
} else if (MiPushClient.COMMAND_SET_ALIAS.equals(command)) {
|
||
if (message.getResultCode() == ErrorCode.SUCCESS) {
|
||
mAlias = cmdArg1;
|
||
log = context.getString(R.string.set_alias_success, mAlias);
|
||
} else {
|
||
log = context.getString(R.string.set_alias_fail,
|
||
message.getReason());
|
||
}
|
||
} else if (MiPushClient.COMMAND_UNSET_ALIAS.equals(command)) {
|
||
if (message.getResultCode() == ErrorCode.SUCCESS) {
|
||
mAlias = cmdArg1;
|
||
log = context.getString(R.string.unset_alias_success, mAlias);
|
||
} else {
|
||
log = context.getString(R.string.unset_alias_fail,
|
||
message.getReason());
|
||
}
|
||
} else if (MiPushClient.COMMAND_SET_ACCOUNT.equals(command)) {
|
||
if (message.getResultCode() == ErrorCode.SUCCESS) {
|
||
mAccount = cmdArg1;
|
||
log = context.getString(R.string.set_account_success, mAccount);
|
||
} else {
|
||
log = context.getString(R.string.set_account_fail,
|
||
message.getReason());
|
||
}
|
||
} else if (MiPushClient.COMMAND_UNSET_ACCOUNT.equals(command)) {
|
||
if (message.getResultCode() == ErrorCode.SUCCESS) {
|
||
mAccount = cmdArg1;
|
||
log = context.getString(R.string.unset_account_success,
|
||
mAccount);
|
||
} else {
|
||
log = context.getString(R.string.unset_account_fail,
|
||
message.getReason());
|
||
}
|
||
} else if (MiPushClient.COMMAND_SUBSCRIBE_TOPIC.equals(command)) {
|
||
if (message.getResultCode() == ErrorCode.SUCCESS) {
|
||
mTopic = cmdArg1;
|
||
log = context.getString(R.string.subscribe_topic_success,
|
||
mTopic);
|
||
} else {
|
||
log = context.getString(R.string.subscribe_topic_fail,
|
||
message.getReason());
|
||
}
|
||
} else if (MiPushClient.COMMAND_UNSUBSCRIBE_TOPIC.equals(command)) {
|
||
if (message.getResultCode() == ErrorCode.SUCCESS) {
|
||
log = context.getString(R.string.unsubscribe_topic_success,
|
||
mTopic);
|
||
} else {
|
||
log = context.getString(R.string.unsubscribe_topic_fail,
|
||
message.getReason());
|
||
}
|
||
} else if (MiPushClient.COMMAND_SET_ACCEPT_TIME.equals(command)) {
|
||
if (message.getResultCode() == ErrorCode.SUCCESS) {
|
||
mStartTime = cmdArg1;
|
||
mEndTime = cmdArg2;
|
||
log = context.getString(R.string.set_accept_time_success,
|
||
mStartTime, mEndTime);
|
||
} else {
|
||
log = context.getString(R.string.set_accept_time_fail,
|
||
message.getReason());
|
||
}
|
||
} else {
|
||
log = message.getReason();
|
||
}
|
||
|
||
Message msg = Message.obtain();
|
||
msg.obj = log;
|
||
AppController.getHandler().sendMessage(msg);
|
||
}
|
||
|
||
@Override
|
||
public void onReceiveRegisterResult(Context context,
|
||
MiPushCommandMessage message) {
|
||
Log.v(AppController.TAG, "onReceiveRegisterResult is called. "
|
||
+ message.toString());
|
||
String command = message.getCommand();
|
||
List<String> arguments = message.getCommandArguments();
|
||
String cmdArg1 = ((arguments != null && arguments.size() > 0) ? arguments
|
||
.get(0) : null);
|
||
String log;
|
||
if (MiPushClient.COMMAND_REGISTER.equals(command)) {
|
||
if (message.getResultCode() == ErrorCode.SUCCESS) {
|
||
mRegId = cmdArg1;
|
||
log = context.getString(R.string.register_success);
|
||
} else {
|
||
log = context.getString(R.string.register_fail);
|
||
}
|
||
} else {
|
||
log = message.getReason();
|
||
}
|
||
|
||
Message msg = Message.obtain();
|
||
msg.obj = log;
|
||
AppController.getHandler().sendMessage(msg);
|
||
}
|
||
|
||
@SuppressLint("SimpleDateFormat")
|
||
public static String getSimpleDate() {
|
||
return new SimpleDateFormat("MM-dd hh:mm:ss").format(new Date());
|
||
}
|
||
|
||
public static class PushHandler extends Handler {
|
||
|
||
private Context context;
|
||
|
||
public PushHandler(Context context) {
|
||
this.context = context;
|
||
}
|
||
|
||
@Override
|
||
public void handleMessage(Message msg) {
|
||
String s = (String) msg.obj;
|
||
|
||
if (!TextUtils.isEmpty(s)) {
|
||
// Toast.makeText(context, s, Toast.LENGTH_LONG).show();
|
||
}
|
||
}
|
||
}
|
||
}
|