NotificationUtils.java
8.64 KB
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
195
196
197
198
199
200
package com.lotus.town.notify;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.widget.RemoteViews;
import com.lotus.town.R;
import com.lotus.town.ali.MonitorService;
import static android.content.Context.NOTIFICATION_SERVICE;
public class NotificationUtils {
private static NotificationUtils notificationUtils;
public static NotificationUtils init() {
if (notificationUtils == null) {
notificationUtils = new NotificationUtils();
}
return notificationUtils;
}
/**
* 适配8.0通知栏
* 在启动页或首页中创建通知渠道
*/
public void createNotificationChannel(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//通知渠道
String channelId = "systemmsg";
String channelName = "系统消息";
int importance = NotificationManager.IMPORTANCE_LOW;
setNotificationChannel(context, channelId, channelName, importance);
}
}
@TargetApi(Build.VERSION_CODES.O)
private void setNotificationChannel(Context context, String channelId, String channelName, int importance) {
NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
//设置不显示角标
channel.setShowBadge(false);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
}
public Notification sendYmNotify(Context context, String ticket,String title) {
NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
RemoteViews view = new RemoteViews(context.getPackageName(), R.layout.notification_view);
RemoteViews smallview = new RemoteViews(context.getPackageName(), R.layout.notification_view);
view.setTextViewText(R.id.notification_title, title);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//8.0及以上,需传入渠道的id
Notification notification = new NotificationCompat.Builder(context, "systemmsg")
.setAutoCancel(true)
.setSmallIcon(R.mipmap.ic_launcher)
.setCustomBigContentView(view)
.setCustomContentView(smallview)
.setTicker(ticket)
.setWhen(System.currentTimeMillis())
.build();
notification = setAlarmParams(context, notification, "", "");
notification.flags = Notification.DEFAULT_ALL;
notification.contentIntent = PendingIntent.getActivity(context, 0,
getIntent(context,"com.lotus.town.SplashActivity"), PendingIntent.FLAG_UPDATE_CURRENT);
manager.notify(111, notification);
return notification;
} else {
Notification notification = new Notification.Builder(context)
.setAutoCancel(true)
.setSmallIcon(R.mipmap.ic_launcher)
.setContent(view)
.setTicker(ticket)
.setWhen(System.currentTimeMillis())
.build();
notification.flags = Notification.DEFAULT_ALL;
notification = setAlarmParams(context, notification, "", "");
notification.contentIntent = PendingIntent.getActivity(context, 0,
getIntent(context,"com.lotus.town.SplashActivity"), PendingIntent.FLAG_UPDATE_CURRENT);
manager.notify(111, notification);
return notification;
}
}
/**
* 根据消息type自定义通知动作
*
* @return
*/
public static Intent getIntent(Context context,Class clz) {
Intent intent = new Intent();
intent.setClass(context, clz);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
return intent;
}
public static Intent getIntent(Context context,String name) {
Intent intent = new Intent();
intent.setClassName(context, name);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
return intent;
}
/**
* 新消息通知模式
*
* @param context
* @param notification
* @return
*/
private Notification setAlarmParams(Context context, Notification notification, String sound, String vibrate) {
AudioManager volMgr = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
switch (volMgr.getRingerMode()) {// 获取系统设置的铃声模式
case AudioManager.RINGER_MODE_SILENT:// 静音模式,值为0,这时候不震动,不响铃
notification.sound = null;
notification.vibrate = null;
break;
case AudioManager.RINGER_MODE_VIBRATE:// 震动模式,值为1,这时候震动,不响铃
if (vibrate.equals("1")) {
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.sound = null;
} else {
notification.sound = null;
notification.vibrate = null;
}
break;
case AudioManager.RINGER_MODE_NORMAL:// 常规模式,值为2,分两种情况:1_响铃但不震动,2_响铃+震动
if (sound.equals("1") && vibrate.equals("1")) {// 声音+震动
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_SOUND;
} else if (sound.equals("0") && vibrate.equals("1")) {// 震动 ,不响铃
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.sound = null;
} else if (sound.equals("1") && vibrate.equals("0")) {// 响铃,不震动
notification.defaults |= Notification.DEFAULT_SOUND;
notification.vibrate = null;
} else {// 静音
notification.sound = null;
notification.vibrate = null;
}
break;
default:
break;
}
return notification;
}
public Notification sendNotify(Context context, PushModel model, String sound, String vibrate) {
NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//8.0及以上,需传入渠道的id
Notification notification = new NotificationCompat.Builder(context, "systemmsg")
.setAutoCancel(true)
.setSmallIcon(R.drawable.empty)
.setContentTitle("荷花小镇")
.setContentText("小青蛙为您持续赚荷花中")
.setSubText("小青蛙为您持续赚荷花中")
.setWhen(System.currentTimeMillis())
.build();
notification = setAlarmParams(context, notification, sound, vibrate);
notification.flags = model.getFlag();
notification.contentIntent = PendingIntent.getActivity(context, 0,
getIntent(context,"com.lotus.town.SplashActivity"), PendingIntent.FLAG_UPDATE_CURRENT);
manager.notify(111, notification);
return notification;
} else {
Notification notification = new Notification.Builder(context)
.setAutoCancel(true)
.setContentTitle("荷花小镇")
.setContentText("小青蛙为您持续赚荷花中")
.setSubText("小青蛙为您持续赚荷花中")
.setSmallIcon(R.drawable.empty)
.setWhen(System.currentTimeMillis())
.build();
notification.flags = model.getFlag();
notification.contentIntent = PendingIntent.getActivity(context, 0,
getIntent(context,"com.lotus.town.SplashActivity"), PendingIntent.FLAG_UPDATE_CURRENT);
notification = setAlarmParams(context, notification, sound, vibrate);
manager.notify(111, notification);
return notification;
}
}
}