package com.tejia.lijin.app.receiver;
|
|
import android.content.Context;
|
import android.content.Intent;
|
import android.content.SharedPreferences;
|
import android.os.Bundle;
|
import android.util.Log;
|
|
import com.huawei.hms.support.api.push.PushReceiver;
|
import com.tejia.lijin.app.ShoppingApi;
|
import com.tejia.lijin.app.ShoppingApplication;
|
import com.tejia.lijin.app.util.user.UserUtil;
|
|
import org.json.JSONObject;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
public class HWPushMessageReceiver extends PushReceiver {
|
public static final String TAG = "HuaweiPushRevicer";
|
|
public static final String ACTION_UPDATEUI = "action.updateUI";
|
public static final String ACTION_TOKEN = "action.updateToken";
|
|
private static List<IPushCallback> pushCallbacks = new ArrayList<IPushCallback>();
|
private static final Object CALLBACK_LOCK = new Object();
|
public static String mToken = "";
|
// @SuppressLint("StaticFieldLeak")
|
// private static Context mcontext;
|
|
public HWPushMessageReceiver() {
|
super();
|
}
|
|
public interface IPushCallback {
|
void onReceive(Intent intent);
|
}
|
|
public static void registerPushCallback(IPushCallback callback) {
|
Log.e("eee", "registerPushCallback: ");
|
synchronized (CALLBACK_LOCK) {
|
pushCallbacks.add(callback);
|
// ShoppingApplication.application.startService(new Intent(ShoppingApplication.application, HMPushService.class));
|
Log.e("eee", "pushCallbacks: " + pushCallbacks.size());
|
}
|
}
|
|
public static void unRegisterPushCallback(IPushCallback callback) {
|
synchronized (CALLBACK_LOCK) {
|
pushCallbacks.remove(callback);
|
}
|
}
|
|
@Override
|
public void onToken(Context context, String s, Bundle bundle) {
|
super.onToken(context, s, bundle);
|
Log.e("eee", "onToken: " + s);
|
|
// String off_no = sp.getString("push_off_no", "false_01");
|
// if (off_no.equals("true_01")) {
|
ShoppingApi.bindHMPush(context, s, UserUtil.getUid(ShoppingApplication.application), null);
|
// }
|
mToken = s;
|
Intent intent = new Intent();
|
intent.setAction(ACTION_TOKEN);
|
intent.putExtra(ACTION_TOKEN, s);
|
callBack(intent);
|
}
|
|
|
@Override
|
public void onPushMsg(Context context, byte[] bytes, String s) {
|
try {
|
//开发者可以自己解析消息内容,然后做相应的处理
|
String msg = new String(bytes, "UTF-8");
|
Log.e("mResult", "收到PUSH透传消息,消息内容为:" + msg);
|
// Log.e("mResult", "收到PUSH透传消息,消息内容String为:" + s);
|
JSONObject jsonObject = new JSONObject(msg);
|
String title = jsonObject.optString("title");
|
String content = jsonObject.optString("content");
|
String type = jsonObject.optString("type");
|
|
// Intent intent = new Intent(context, SelfBuyAndReductionActivity.class);
|
// PendingIntent pendingIntent = PendingIntent.getActivity(context,
|
// 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
//
|
// String id = "my_channel_01";
|
// String name = "我是渠道名字";
|
// NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
// Notification notification;
|
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
// NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
|
// Log.i("mResult", mChannel.toString());
|
// notificationManager.createNotificationChannel(mChannel);
|
// notification = new Notification.Builder(context)
|
// .setChannelId(id)
|
// .setContentTitle(title)
|
// .setContentText(content)
|
// .setAutoCancel(true)
|
// .setContentIntent(pendingIntent)
|
// .setSmallIcon(R.mipmap.ic_launcher).build();
|
// } else {
|
// NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
|
// .setContentTitle(title)
|
// .setContentText(content)
|
// .setAutoCancel(true)
|
// .setContentIntent(pendingIntent)
|
// .setSmallIcon(R.mipmap.ic_launcher)
|
// .setOngoing(true);//无效
|
// notification = notificationBuilder.build();
|
// }
|
// notificationManager.notify(111123, notification);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
super.onPushMsg(context, bytes, s);
|
}
|
|
private static void callBack(Intent intent) {
|
synchronized (CALLBACK_LOCK) {
|
for (IPushCallback callback : pushCallbacks) {
|
if (callback != null) {
|
callback.onReceive(intent);
|
}
|
}
|
}
|
}
|
}
|