GLNotifiactionManager.java
12.6 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package com.glsx.pushsdk.manager;
import android.annotation.SuppressLint;
import android.app.AppOpsManager;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.text.TextUtils;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import com.blankj.utilcode.util.LogUtils;
import com.glsx.pushsdk.R;
import com.glsx.pushsdk.receiver.GLMessageClickedReceiver;
import com.glsx.pushsdk.tools.Tools;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* 通知栏消息管理
*
* @author YFZX-yulg
*/
@SuppressWarnings({"deprecation","unchecked"})
public class GLNotifiactionManager {
private static String TAG = GLNotifiactionManager.class.getSimpleName();
public static final String CHECK_OP_NO_THROW = "checkOpNoThrow";
public static final String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";
private NotificationCompat.Builder mBuilder;
private NotificationManager mNotificationManager;
private Notification mNotification;
private int NOTIFICATION_FLAG = 1001;//通知ID,不重复,可以产生多个通知显示
private final String CHANNEL_ID = "1000";
private static GLNotifiactionManager mGLNotificationManager = new GLNotifiactionManager();
public static GLNotifiactionManager getInstance() {
if (mGLNotificationManager == null) {
synchronized (GLNotifiactionManager.class) {
if (mGLNotificationManager == null) {
mGLNotificationManager = new GLNotifiactionManager();
}
}
}
return mGLNotificationManager;
}
private GLNotifiactionManager() {
init();
}
/**
* 初始化我的通知栏
*/
private void init() {
mNotificationManager = (NotificationManager) GLPushManager.getInstance().getMyContext()
.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
mBuilder = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) ?
new NotificationCompat.Builder(GLPushManager.getInstance().getMyContext(), CHANNEL_ID) :
new NotificationCompat.Builder(GLPushManager.getInstance().getMyContext());
}
/**
* 设置通知栏通知
*
* @param title
*/
@SuppressWarnings("deprecation")
@SuppressLint("InlinedApi")
public void setUpNotification(String title, String content, String value, String msgId) {
LogUtils.i(TAG, "接收到推送通知, title = " + title);
if (mBuilder == null) {
init();
}
//应用在前台时,直接应用展示消息弹框
//应用在后台且未被清理时,以消息形式展示在消息通知栏内
if (Tools.isTopActivity(GLPushManager.getInstance().getMyContext())) {
LogUtils.i(TAG, "Tools.isTopActivity 11111 ");
Intent intent1 = new Intent();
intent1.putExtra(GLLoginManager.ACCEPT_RECEIVER_TITLE, title);
intent1.putExtra(GLLoginManager.ACCEPT_RECEIVER_CONTENT, content);
intent1.putExtra(GLLoginManager.ACCEPT_RECEIVER_VALUE, value);
intent1.putExtra(GLLoginManager.ACCEPT_RECEIVER_MSG_ID, msgId);
intent1.setAction(GLLoginManager.ACCEPT_RECEIVER_NOTIFICATION);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String pakName = GLPushManager.getInstance().getMyContext().getPackageName();
intent1.setComponent(new ComponentName(pakName, pakName + ".glpush.GLAcceptReceiver"));
GLPushManager.getInstance().getMyContext().sendBroadcast(intent1);
LogUtils.i(TAG, "发送广播, intent1,pakName = " + pakName);
} else {
GLPushManager.getInstance().getMyContext().sendBroadcast(intent1);
}
return;
}
LogUtils.i(TAG, "准备进行推送, mBuilder = " + mBuilder);
Drawable appIcon = Tools.getAppIcon(GLPushManager.getInstance().getMyContext());
String appName = Tools.getAppName(GLPushManager.getInstance().getMyContext());
String pushTitle = appName + "-" + title;
LogUtils.i(TAG, "准备进行推送, appName = " + appName);
LogUtils.i(TAG, "准备进行推送, pushTitle = " + pushTitle);
//通知栏消息点击执行意图
Intent clickIntent = new Intent(GLPushManager.getInstance().getMyContext(), GLMessageClickedReceiver.class); // 点击通知之后要发送的广播
//Intent clickIntent = new Intent(GLPushManager.getInstance().getMyContext(), com.glsx.aicar.ui.activity.MainActivity.class); // 点击通知之后要发送的广播
clickIntent.setAction(GLMessageClickedReceiver.RECEIVER);
clickIntent.putExtra(GLLoginManager.ACCEPT_RECEIVER_TITLE, title);
clickIntent.putExtra(GLLoginManager.ACCEPT_RECEIVER_CONTENT, content);
clickIntent.putExtra(GLLoginManager.ACCEPT_RECEIVER_VALUE, value);
clickIntent.putExtra(GLLoginManager.ACCEPT_RECEIVER_MSG_ID, msgId);
int id = (int) (System.currentTimeMillis() / 1000);
//创建PendingIntent,便于在应用非前台时通过客户点击通知栏消息时触发执行弹框和后续操作,不点击消息则不执行后续操作
PendingIntent contentIntent = PendingIntent.getBroadcast(
GLPushManager.getInstance().getMyContext(),
id, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//PendingIntent contentIntent = PendingIntent.getActivity(GLPushManager.getInstance().getMyContext(), 0,clickIntent, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (!openNotificationChannel(GLPushManager.getInstance().getMyContext(),
mNotificationManager, CHANNEL_ID)) {
return;
}
}
mBuilder.setContentTitle(pushTitle)// 设置title
.setContentText(content)// 设置内容
// .setLargeIcon(Tools.Drawable2Bitmap(appIcon)) // 设置大标题
.setSmallIcon(R.drawable.notify_logo)
// .setSmallIcon(Tools.getId(GLPushManager.getInstance().getMyContext(), "drawable","app_logo")) // 设置小图标
.setTicker(content) // 通知首次出现在通知栏,带上升动画效果的
.setAutoCancel(true)// 设置这个标志当用户单击面板就可以让通知将自动取消
.setWhen(System.currentTimeMillis())// 通知产生的时间,会在通知信息里显示,一般是系统获取到的时间
.setPriority(Notification.PRIORITY_DEFAULT) // 设置该通知优先级
.setDefaults(Notification.DEFAULT_ALL) // 设置震动,响声
.setContentIntent(contentIntent);// 设置点击跳转
LogUtils.i(TAG, "准备进行推送, pushTitle1 = " + pushTitle);
// mNotification = mBuilder.getNotification();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "消息类型",
NotificationManager.IMPORTANCE_HIGH
);
mNotificationManager.createNotificationChannel(channel);
}
mNotification = mBuilder.build();
mNotification.flags = Notification.FLAG_AUTO_CANCEL;
if (NOTIFICATION_FLAG < 2000) {
NOTIFICATION_FLAG++;
} else {
NOTIFICATION_FLAG = 1001;
}
LogUtils.i(TAG, "准备进行推送, title = " + title);
mNotificationManager.notify(NOTIFICATION_FLAG, mNotification);
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static Boolean openNotificationChannel(Context context, NotificationManager manager, String channelId) {
//判断通知是否有打开
if (!isNotificationEnabled(context)) {
toNotifySetting(context, null);
return false;
}
//判断渠道通知是否打开
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = manager.getNotificationChannel(channelId);
if (null != channel && channel.getImportance() == NotificationManager.IMPORTANCE_NONE) {
//没打开调往设置界面
toNotifySetting(context, channel.getId());
return false;
}
}
return true;
}
/**
* 手动打开应用通知
*/
public static void toNotifySetting(Context context, String channelId) {
Intent intent = new Intent();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //适配 8.0及8.0以上(8.0需要先打开应用通知,再打开渠道通知)
if (TextUtils.isEmpty(channelId)) {
intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
} else {
intent.setAction(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
intent.putExtra(Settings.EXTRA_CHANNEL_ID, channelId);
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//适配 5.0及5.0以上
intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
intent.putExtra("app_package", context.getPackageName());
intent.putExtra("app_uid", context.getApplicationInfo().uid);
} else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {// 适配 4.4及4.4以上
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setData(Uri.fromParts("package", context.getPackageName(), null));
} else {
intent.setAction(Settings.ACTION_SETTINGS);
}
context.startActivity(intent);
}
/**
* 判断应用通知是否打开
*
* @return
*/
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static boolean isNotificationEnabled(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
return notificationManagerCompat.areNotificationsEnabled();
}
AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
try {
Class appOpsClass = Class.forName(AppOpsManager.class.getName());
Method checkOpNoThrowMethod = appOpsClass.getMethod(
CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE, String.class);
Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
int value = (Integer) opPostNotificationValue.get(Integer.class);
return ( AppOpsManager.MODE_ALLOWED ==
(Integer)(checkOpNoThrowMethod.invoke(
mAppOps, value, context.getApplicationInfo().uid, context.getPackageName())));
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 离线数据通知
*
* @param title
* @param content
* @param value
*/
public void setOffMessageNotification(String title, String content, String value) {
LogUtils.i(TAG, "setOffMessageNotification in ");
Intent intent1 = new Intent();
intent1.putExtra(GLLoginManager.ACCEPT_RECEIVER_TITLE, title);
intent1.putExtra(GLLoginManager.ACCEPT_RECEIVER_CONTENT, content);
intent1.putExtra(GLLoginManager.ACCEPT_RECEIVER_VALUE, value);
intent1.putExtra(GLLoginManager.ACCEPT_RECEIVER_MSG_ID, "");
intent1.setAction(GLLoginManager.ACCEPT_RECEIVER_OFF_MESSAGE_NOTIFICATION);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String pakName = GLPushManager.getInstance().getMyContext().getPackageName();
intent1.setComponent(new ComponentName(pakName, pakName + ".glpush.GLGetOffMessageReceiver"));
GLPushManager.getInstance().getMyContext().sendBroadcast(intent1);
LogUtils.i(TAG, "发送广播, intent1,pakName = " + pakName);
} else {
GLPushManager.getInstance().getMyContext().sendBroadcast(intent1);
}
}
}