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
package cn.flightfeather.thirdappmodule.util.push
 
import android.content.Context
import android.util.Log
import com.alibaba.sdk.android.push.CloudPushService
import com.alibaba.sdk.android.push.CommonCallback
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory
 
/**
 * 移动推送服务配置类
 * @author riku
 * Date: 2019/12/26
 */
class PushService {
    companion object {
        const val TAG = "PushService"
 
        private var mPushService: CloudPushService? = null
 
        /**
         * 初始化云推送通道
         * @param applicationContext
         */
        fun init(applicationContext: Context) {
            PushServiceFactory.init(applicationContext)
            mPushService = PushServiceFactory.getCloudPushService()
            mPushService?.register(applicationContext, object : CommonCallback {
                override fun onSuccess(response: String) {
                    Log.d(TAG, "init cloudChannel success")
                    Log.d(TAG, response)
                }
 
                override fun onFailed(errorCode: String, errorMessage: String) {
                    Log.d(TAG, "init cloudChannel failed -- errorCode:$errorCode -- errorMessage:$errorMessage")
                }
            })
        }
 
        fun bindAccount(account: String, s: () -> Unit) {
            mPushService?.bindAccount(account, object : CommonCallback {
                override fun onSuccess(p0: String?) {
                    Log.d(TAG, "bind account $account success")
                    s()
                }
 
                override fun onFailed(p0: String?, p1: String?) {
 
                }
            })
        }
 
        fun unBindAccount(s: () -> Unit) {
            mPushService?.unbindAccount(object : CommonCallback {
                override fun onSuccess(p0: String?) {
                    Log.d(TAG, "unbind account success")
                    s()
                }
 
                override fun onFailed(p0: String?, p1: String?) {
 
                }
            })
        }
    }
}