admin
2021-02-27 db66b085308b8bc7a1ff9440c85895d41c98b5ed
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
package com.yeshi.push;
 
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.Process;
import android.util.Log;
 
import com.heytap.msp.push.HeytapPushManager;
import com.heytap.msp.push.callback.ICallBackResultService;
import com.lcjian.library.util.ManifestDataUtil;
import com.lcjian.library.util.common.StringUtils;
import com.meizu.cloud.pushsdk.PushManager;
import com.vivo.push.IPushActionListener;
import com.vivo.push.PushClient;
import com.xiaomi.mipush.sdk.MiPushClient;
 
import java.util.List;
 
public class PushUtil {
 
    static String TAG = "PushUtil";
 
    /**
     * 推送 注册
     */
    public static void inintPush(Application context, ITokenListener tokenListener) {
        if (context != null) {
            String xmRegId = null;
            switch (Build.BRAND.toLowerCase()) {
                case "huawei":
                case "honor":
                    //华为是自动初始化
                    return;
                case "oppo"://上面官方判断
                    /***OPPO官方判断方法*/
                    HeytapPushManager.init(context, true);
                    if (HeytapPushManager.isSupportPush()) {
                        String appKey = ManifestDataUtil.getAppMetaData(context, "OPPO_APPKEY").replace("OPPO-", "");
                        String appSecret = ManifestDataUtil.getAppMetaData(context, "OPPO_APPSECRET").replace("OPPO-", "");
                        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("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"://上面官方判断
                    /***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("vivo", regId);
                                }
                            }
                        });
                        return;
                    }
                    break;
                case "meizu"://魅族
                {
                    String appId = ManifestDataUtil.getAppMetaData(context, "MEIZU_APPID").replace("MEIZU-", "");
                    String appKey = ManifestDataUtil.getAppMetaData(context, "MEIZU_APPKEY").replace("MEIZU-", "");
                    PushManager.register(context, appId, appKey);
                    return;
                }
            }
 
            String appId = ManifestDataUtil.getAppMetaData(context, "XIAOMI_APPID").replace("XIAOMI-", "");
            String appKey = ManifestDataUtil.getAppMetaData(context, "XIAOMI_APPKEY").replace("XIAOMI-", "");
            MiPushClient.registerPush(context, appId, appKey);//注册小米推送
            xmRegId = MiPushClient.getRegId(context);
            Log.i(TAG, "小米注册成功:" + xmRegId);
            if (!StringUtils.isEmpty(xmRegId)) {
                tokenListener.onToken("xm", xmRegId);
            }
 
 
        }
    }
 
 
    public interface ITokenListener {
        public void onToken(String romType, String regId);
 
    }
 
}