admin
2021-07-08 1764c1784a4cf1a6afd25fcf1a0eef6187a84218
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.yeshi.push;
 
import android.app.Application;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
 
import com.heytap.msp.push.HeytapPushManager;
import com.heytap.msp.push.callback.ICallBackResultService;
import com.huawei.hms.aaid.HmsInstanceId;
import com.huawei.hms.common.ApiException;
import com.meizu.cloud.pushsdk.PushManager;
import com.vivo.push.IPushActionListener;
import com.vivo.push.PushClient;
import com.xiaomi.mipush.sdk.MiPushClient;
import com.yeshi.push.entity.PushApps;
import com.yeshi.push.entity.PushRomType;
 
public class PushUtil {
 
    static String TAG = "PushUtil";
 
    /**
     * 推送 注册
     */
    public static void inintPush(Application context, PushApps apps, ITokenListener tokenListener) {
        if (context != null) {
            String xmRegId = null;
            switch (Build.BRAND.toLowerCase()) {
                case "huawei":
                case "honor":
                    //华为是自动初始化
                    //获取token
                    new Thread() {
                        @Override
                        public void run() {
                            try {
                                // 从agconnect-service.json文件中读取appId
                                String appId = apps.getHw().getAppId();
                                // 输入token标识"HCM"
                                String tokenScope = "HCM";
                                String token = HmsInstanceId.getInstance(context).getToken(appId, tokenScope);
                                Log.i(TAG, "get token: " + token);
 
                                // 判断token是否为空
                                if(!TextUtils.isEmpty(token)) {
                                    tokenListener.onToken(PushRomType.huawei,token);
                                }
                            } catch (ApiException e) {
                                Log.e(TAG, "hw: get token failed, " + e);
                            }
                        }
                    }.start();
 
                    return;
                case "oppo"://上面官方判断
                    /***OPPO官方判断方法*/
                    if (apps.getOppo() == null)
                        break;
                    HeytapPushManager.init(context, true);
                    if (HeytapPushManager.isSupportPush()) {
                        String appKey = apps.getOppo().getAppKey();
                        String appSecret = apps.getOppo().getAppSecret();
                        HeytapPushManager.register(context, appKey, appSecret, new ICallBackResultService() {
 
                            @Override
                            public void onRegister(int i, String s) {
                                //注册成功
                                Log.i(TAG, "OPPO注册结果:" + i);
                                Log.i(TAG, "OPPO注册成功:" + s);
                                if (i == 0) {
                                    tokenListener.onToken(PushRomType.oppo, s);
                                }
                            }
 
                            @Override
                            public void onUnRegister(int i) {
 
                            }
 
                            @Override
                            public void onSetPushTime(int i, String s) {
 
                            }
 
                            @Override
                            public void onGetPushStatus(int i, int i1) {
 
                            }
 
                            @Override
                            public void onGetNotificationStatus(int i, int i1) {
 
                            }
                        });
                        HeytapPushManager.requestNotificationPermission();
                        return;
                    }
                    break;
                case "vivo"://上面官方判断
                    if (apps.getVivo() == null)
                        break;
                    /***VIVO官方判断方法**/
                    // 在当前工程入口函数,建议在 Application 的 onCreate 函数中,添加以下代码:
                    PushClient.getInstance(context).initialize();
                    if (PushClient.getInstance(context).isSupport()) {
                        //并且打开推送服务
                        PushClient.getInstance(context).turnOnPush(new IPushActionListener() {
                            @Override
                            public void onStateChanged(int i) {
                                if (i == 0) {
                                    String regId = PushClient.getInstance(context).getRegId();
                                    Log.i(TAG, "VIVO注册成功:" + regId);
                                    tokenListener.onToken(PushRomType.vivo, regId);
                                }
                            }
                        });
                        return;
                    }
                    break;
                case "meizu"://魅族
 
                {
                    if (apps.getMeizu() == null) {
                        break;
                    }
                    String appId = apps.getMeizu().getAppId();
                    String appKey = apps.getMeizu().getAppKey();
                    PushManager.register(context, appId, appKey);
                    return;
                }
            }
 
            String appId = apps.getXm().getAppId();
            String appKey = apps.getXm().getAppKey();
            MiPushClient.registerPush(context, appId, appKey);//注册小米推送
            xmRegId = MiPushClient.getRegId(context);
            Log.i(TAG, "小米注册成功:" + xmRegId);
            if (xmRegId != null && xmRegId.trim().length() > 0) {
                tokenListener.onToken(PushRomType.xm, xmRegId);
            }
 
        }
    }
 
 
    public interface ITokenListener {
        public void onToken(PushRomType romType, String regId);
    }
 
}