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
package cn.flightfeather.thirdapp.util.notification
 
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.Context.NOTIFICATION_SERVICE
import android.graphics.Color
import android.os.Build
 
 
/**
 * Android 8.0 后需要注册NotificationChannel才可以接收通知
 * @author riku
 * Date: 2019/12/26
 */
class MyNotificationChannel {
    companion object {
        fun init(applicationContext: Context) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val mNotificationManager = applicationContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager?
                // 通知渠道的id
                val id = "1"
                // 用户可以看到的通知渠道的名字.
                val name = "aliCloudPush channel"
                // 用户可以看到的通知渠道的描述
                val description = "aliCloudPush"
                val importance = NotificationManager.IMPORTANCE_HIGH
                val mChannel = NotificationChannel(id, name, importance)
                // 配置通知渠道的属性
                mChannel.description = description
                // 设置通知出现时的闪灯(如果 android 设备支持的话)
                mChannel.enableLights(true)
                mChannel.lightColor = Color.RED
                // 设置通知出现时的震动(如果 android 设备支持的话)
                mChannel.enableVibration(true)
                mChannel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
                //最后在notificationmanager中创建该通知渠道
                mNotificationManager?.createNotificationChannel(mChannel)
            }
        }
    }
}