lmw
2023-06-06 7a563b559c48b9b339784c25fc5f0adc2ab5154e
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package com.sinata.download.utils.notification;
 
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Vibrator;
import androidx.core.app.NotificationCompat;
import android.widget.RemoteViews;
 
import com.sinata.download.DownloadLibrary;
 
import static android.content.Context.NOTIFICATION_SERVICE;
 
 
/**
 * author:Created by zhaohaoting on 2018/1/10 11:51
 * email:526309416@qq.com
 * desc:
 */
 
public class MessageNoticeManager {
    private long[] pattern = {0, 500, 500, 500};
    private MediaPlayer mp;
    private NotificationManager manager;
 
    private MessageNoticeManager() {
    }
 
    private static MessageNoticeManager messageNoticeManager;
 
    public static MessageNoticeManager getInstance() {
        if (messageNoticeManager == null) {
            synchronized (MessageNoticeManager.class) {
                if (messageNoticeManager == null) {
                    messageNoticeManager = new MessageNoticeManager();
                }
            }
        }
        return messageNoticeManager;
    }
 
    public void makeNotification(Context context, String title, String content, String tickerText, int id) {
        Notification notification = createNotice(context, title, content, tickerText);
        /**
         * 手机设置的默认提示音
         */
//        Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
 
        /**
         * sound属性是一个 Uri 对象。 可以在通知发出的时候播放一段音频,这样就能够更好地告知用户有通知到来.
         * 如:手机的/system/media/audio/ringtones 目录下有一个 Basic_tone.ogg音频文件,
         * 可以写成: Uri soundUri = Uri.fromFile(new
         * File("/system/media/audio/ringtones/Basic_tone.ogg"));
         */
 
 
        /**
         * vibrate属性是一个长整型的数组,用于设置手机静止和振动的时长,以毫秒为单位。
         * 参数中下标为0的值表示手机静止的时长,下标为1的值表示手机振动的时长, 下标为2的值又表示手机静止的时长,以此类推。
         */
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            notification.vibrate = null;
            notification.sound = null;
        }
 
//        /**
//         * 手机处于锁屏状态时, LED灯就会不停地闪烁, 提醒用户去查看手机,下面是绿色的灯光一 闪一闪的效果
//         */
//        notification.ledARGB = Color.GREEN;// 控制 LED 灯的颜色,一般有红绿蓝三种颜色可选
//        notification.ledOnMS = 1000;// 指定 LED 灯亮起的时长,以毫秒为单位
//        notification.ledOffMS = 1000;// 指定 LED 灯暗去的时长,也是以毫秒为单位
//        notification.flags = Notification.FLAG_SHOW_LIGHTS;// 指定通知的一些行为,其中就包括显示
//        // LED 灯这一选项
 
        /**
         * 如果不想进行那么多繁杂的设置,也可以直接使用通知的默认效果,它会根据当前手机的环境来决定播放什么铃声,以及如何振动
         */
        // notification.defaults = Notification.DEFAULT_ALL;
        notify(id, notification);
    }
 
    public void notify(int id, Notification notification) {
        manager.notify(id, notification);
    }
 
    public void cancel(int id) {
        manager.cancel(id);
    }
 
    private void play(Context context) {
        // 设置通知出现时的震动(如果 android 设备支持的话)
        Vibrator vibrator = (Vibrator) context.getSystemService(context.VIBRATOR_SERVICE);
        vibrator.vibrate(pattern, -1);
 
        mp = MediaPlayer.create(context, getSystemDefultRingtoneUri(context));
        try {
//            mpMediaPlayer.prepare();
            if (mp.isPlaying()) {
                mp.stop();
                mp.release();
                mp = MediaPlayer.create(context, getSystemDefultRingtoneUri(context));
            }
            mp.setVolume(1f, 1f);
            mp.setLooping(false);
            mp.start();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }
 
    public Notification createNotice(Context context, RemoteViews remoteViews) {
        manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        AVChatNotificationChannelCompat26.createNIMMessageNotificationChannel(context, manager);
        /**
         * 实例化Intent,构建意图:从当前页面条状到NotificationActivity页面
         */
 
        /**
         * 用于启动活动、启动服务以及发送广播等。 根据需求来选择是使用
         * getActivity()方法、getBroadcast()方法、还是 getService() 方法。 一共四个参数:
         * 1)、第一个参数是 上下文Context ; 2)、 第二个参数一般用不到,通常都是传入 0 即可 3)、第三个参数是一个
         * Intent对象,我们可以通过这个对象构建出 PendingIntent 的“意图”; 4)、第四个参数用于确定
         * PendingIntent 的行为
         */
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, AVChatNotificationChannelCompat26.getNIMChannelId(context));
        builder.setCategory(NotificationCompat.CATEGORY_MESSAGE)
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setSmallIcon(DownloadLibrary.getIc_launcher())
                .setCustomContentView(remoteViews)
                .setPriority(Notification.PRIORITY_HIGH)
                .setVisibility(Notification.VISIBILITY_PUBLIC);
        return builder.build();
    }
 
    public Notification createNotice(Context context, String title, String content, String tickerText) {
        Class<? extends Activity> entranceActivity = DownloadLibrary.getEntranceActivity();
        Intent intent = new Intent();
        if (entranceActivity !=null){
            intent.setClass(context,entranceActivity);
        }
        return createNotice(context, title, content, tickerText, intent);
    }
 
    public Notification createNotice(Context context, String title, String content, String tickerText, Intent intent) {
        manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        AVChatNotificationChannelCompat26.createNIMMessageNotificationChannel(context, manager);
 
        /**
         * 用于启动活动、启动服务以及发送广播等。 根据需求来选择是使用
         * getActivity()方法、getBroadcast()方法、还是 getService() 方法。 一共四个参数:
         * 1)、第一个参数是 上下文Context ; 2)、 第二个参数一般用不到,通常都是传入 0 即可 3)、第三个参数是一个
         * Intent对象,我们可以通过这个对象构建出 PendingIntent 的“意图”; 4)、第四个参数用于确定
         * PendingIntent 的行为
         */
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 1,
                intent, PendingIntent.FLAG_CANCEL_CURRENT);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, AVChatNotificationChannelCompat26.getNIMChannelId(context));
        builder.setCategory(NotificationCompat.CATEGORY_MESSAGE)
                .setTicker(tickerText)
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setSmallIcon(DownloadLibrary.getIc_launcher())
                .setContentTitle(title)
                .setContentText(content)
                .setContentIntent(pendingIntent)
                .setPriority(Notification.PRIORITY_HIGH)
                .setVisibility(Notification.VISIBILITY_PUBLIC);
        return builder.build();
    }
 
    //停止播放
    public void stopRing() {
        if (mp != null) {
            if (mp.isPlaying()) {
                mp.stop();
                mp.release();
            }
        }
    }
 
    //获取系统默认铃声的Uri
    private Uri getSystemDefultRingtoneUri(Context context) {
        return RingtoneManager.getActualDefaultRingtoneUri(context,
                RingtoneManager.TYPE_NOTIFICATION);
    }
}