admin
2021-05-13 4a8f1bec26519a25f073739534e653a4f7c9e11d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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 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);
        SharedPreferences sp = context.getSharedPreferences("user", Context.MODE_PRIVATE);
        String uid = sp.getString("uid", "");
//        String off_no = sp.getString("push_off_no", "false_01");
//        if (off_no.equals("true_01")) {
        ShoppingApi.bindHMPush(context, s, uid, 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);
                }
            }
        }
    }
}