wpc
2018-11-27 680fbc9e73da3e11988557cf88fd935efd3e0b1e
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
package com.haicaojie.android.receiver;
 
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
 
import com.huawei.hms.support.api.push.PushReceiver;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * Created by weikou2015 on 2018/6/28.
 */
 
public class HuaweiPushReceiver extends PushReceiver {
    public static final String TAG = "HuaweiPushReceiver";
 
    public static final String ACTION_UPDATEUI = "action.updateUI";
    public static final String ACTION_TOKEN = "action.updateToken";
 
    public static List<IPushCallback> pushCallbacks = new ArrayList<IPushCallback>();
    private static final Object CALLBACK_LOCK = new Object();
 
    public interface IPushCallback {
        void onReceive(Intent intent);
    }
 
    public static void registerPushCallback(IPushCallback callback) {
        synchronized (CALLBACK_LOCK) {
//            SingleToast.showToast(ShoppingApplication.application, "registerPushCallback");
            pushCallbacks.add(callback);
        }
    }
 
    public static void unRegisterPushCallback(IPushCallback callback) {
        synchronized (CALLBACK_LOCK) {
            pushCallbacks.remove(callback);
        }
    }
 
    @Override
    public void onToken(Context context, String tokenIn, Bundle extras) {
        String belongId = extras.getString("belongId");
 
        Intent intent = new Intent();
        intent.setAction(ACTION_TOKEN);
        intent.putExtra(ACTION_TOKEN, tokenIn);
        callBack(intent);
 
        intent = new Intent();
        intent.setAction(ACTION_UPDATEUI);
        intent.putExtra("mResult", "belongId is:" + belongId + " Token is:" + tokenIn);
//        Toast.makeText(context, "belongId is:" + belongId + " Token is:", Toast.LENGTH_SHORT).show();
        callBack(intent);
    }
 
    @Override
    public boolean onPushMsg(Context context, byte[] msg, Bundle bundle) {
        try {
            //CP可以自己解析消息内容,然后做相应的处理 | CP can parse message content on its own, and then do the appropriate processing
            String content = new String(msg, "UTF-8");
            Intent intent = new Intent();
            intent.setAction(ACTION_UPDATEUI);
            intent.putExtra("log", "Receive a push pass message with the message:" + content);
            callBack(intent);
        } catch (Exception e) {
            Intent intent = new Intent();
            intent.setAction(ACTION_UPDATEUI);
            intent.putExtra("log", "Receive push pass message, exception:" + e.getMessage());
            callBack(intent);
        }
        return false;
    }
 
    public void onEvent(Context context, Event event, Bundle extras) {
 
        Intent intent = new Intent();
        intent.setAction(ACTION_UPDATEUI);
        int notifyId = 0;
        if (Event.NOTIFICATION_OPENED.equals(event) || Event.NOTIFICATION_CLICK_BTN.equals(event)) {
            notifyId = extras.getInt(BOUND_KEY.pushNotifyId, 0);
            if (0 != notifyId) {
                NotificationManager manager = (NotificationManager) context
                        .getSystemService(Context.NOTIFICATION_SERVICE);
                manager.cancel(notifyId);
            }
        }
        String message = extras.getString(BOUND_KEY.pushMsgKey);
        intent.putExtra("log", "Received event,notifyId:" + notifyId + " msg:" + message);
//        Toast.makeText(context, "Received event,notifyId:" + notifyId + " msg:" + message, Toast.LENGTH_SHORT).show();
        callBack(intent);
        super.onEvent(context, event, extras);
    }
 
    @Override
    public void onPushState(Context context, boolean pushState) {
        Intent intent = new Intent();
        intent.setAction(ACTION_UPDATEUI);
        intent.putExtra("log", "The Push connection status is:" + pushState);
        callBack(intent);
//        Toast.makeText(context, "The Push connection status is:" + pushState, Toast.LENGTH_SHORT).show();
 
    }
 
 
    private static void callBack(Intent intent) {
        synchronized (CALLBACK_LOCK) {
            for (IPushCallback callback : pushCallbacks) {
                if (callback != null) {
                    callback.onReceive(intent);
                }
            }
        }
    }
 
 
}