MessageManager.java
12.9 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
package com.shunzhi.parent.manager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.Observer;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.StatusCode;
import com.netease.nimlib.sdk.auth.AuthService;
import com.netease.nimlib.sdk.auth.AuthServiceObserver;
import com.netease.nimlib.sdk.auth.LoginInfo;
import com.netease.nimlib.sdk.msg.MsgServiceObserve;
import com.netease.nimlib.sdk.msg.model.CustomNotification;
import com.netease.nimlib.sdk.msg.model.IMMessage;
import com.share.mvpsdk.utils.DateUtils;
import com.share.mvpsdk.utils.ToastUtils;
import com.shunzhi.parent.AppConfig;
import com.shunzhi.parent.AppContext;
import com.shunzhi.parent.R;
import com.shunzhi.parent.bean.NIMLoginResultBean;
import com.shunzhi.parent.bean.message.PHMessage;
import com.shunzhi.parent.bean.message.PHMessageDao;
import com.shunzhi.parent.bean.message.PHMessageSession;
import com.shunzhi.parent.db.DBMessageController;
import com.shunzhi.parent.db.Messages;
import com.shunzhi.parent.ui.MainActivity;
import com.shunzhi.parent.ui.activity.LoginAndRegistActivity;
import com.shunzhi.parent.ui.service.BadgeIntentService;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Date;
import java.util.List;
import io.reactivex.Observable;
import io.reactivex.subjects.PublishSubject;
import me.leolin.shortcutbadger.ShortcutBadger;
import timber.log.Timber;
/**
* Created by 10501 on 2018/3/17.
*/
public class MessageManager {
public boolean isNotiNotification = true;
private static class InstanceHolder {
static final MessageManager instance = new MessageManager();
}
public static MessageManager getInstance() {
return InstanceHolder.instance;
}
private Observer<List<IMMessage>> messageObserver = new Observer<List<IMMessage>>() {
@Override
public void onEvent(List<IMMessage> imMessages) {
// Log.d("77777", "IMMessage=" + imMessages.toString());
onMessageReceive(imMessages);
}
};
private Observer<CustomNotification> customNotificationObserver = new Observer<CustomNotification>() {
@Override
public void onEvent(CustomNotification customNotification) {
// Log.d("77777", "customNotification=" + customNotification.getContent());
onCustomNotificationReceive(customNotification);
}
};
private MessageManager() {
NIMClient.getService(MsgServiceObserve.class).observeReceiveMessage(messageObserver, true);
NIMClient.getService(MsgServiceObserve.class).observeCustomNotification(customNotificationObserver, true);
NIMClient.getService(AuthServiceObserver.class).observeOnlineStatus(new Observer<StatusCode>() {
@Override
public void onEvent(StatusCode statusCode) {
if (statusCode == StatusCode.KICKOUT) {
MessageManager.getInstance().logout();
Context context = AppContext.getInstance();
Intent i = new Intent(context, LoginAndRegistActivity.class);
i.putExtra("type", "登录");
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
ToastUtils.showToast("请重新登录");
context.startActivity(i);
}
}
}, true);
}
private void onMessageReceive(List<IMMessage> messageList) {
}
private void onCustomNotificationReceive(CustomNotification customNotification) {
Timber.d("sss-==" + customNotification.getContent());
try {
JSONObject json = new JSONObject(customNotification.getContent());
Timber.d("sss-==" + json.toString());
String type = json.optString("type");
String uuid = json.optString("msgId");
Timber.d("onCustomNotificationReceive%s", "json=" + json.toString());
if ("homework".equals(type)) {
Messages messages = new Messages();
messages.msgId = uuid;
messages.title = json.optString("title");
messages.type = 0;
messages.userid = json.optString("userid");
messages.isRead="false";
messages.date= DateUtils.dateFormat(new Date(),"yyyy年MM月dd日");
notifyNotification(uuid, DBMessageController.getInstance(AppContext.getContext()).addMessages(messages), "收到一条作业通知");
} else if ("attend".equals(type)) {
Messages messages = new Messages();
messages.msgId = uuid;
messages.title = json.optString("title");
messages.type = 1;
messages.userid = json.optString("userid");
messages.isRead="false";
messages.date= DateUtils.dateFormat(new Date(),"yyyy年MM月dd日");
notifyNotification(uuid, DBMessageController.getInstance(AppContext.getContext()).addMessages(messages), "收到一条考勤通知");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
/*private void onCustomNotificationReceive(CustomNotification customNotification) {
Timber.d("sss-==" + customNotification.getContent());
try {
JSONObject json = new JSONObject(customNotification.getContent());
Timber.d("sss-==" + json.toString());
String type = json.optString("type");
String uuid = json.optString("msgId");
Timber.d("onCustomNotificationReceive%s", "json=" + json.toString());
if ("homework".equals(type)) {
PHMessageSession session = PHMessageSession.findAndCreateSession(PHMessageSession.sessionType_homework, true);
session.setSessionName("作业通知");
session.setDate(new Date(customNotification.getTime()));
session.setSessionText(json.optString("title"));
// ToastUtils.showToast(json.optString("title")+"uuid="+uuid);
PHMessage message = new PHMessage();
PHMessageDao messageDao = AppContext.getInstance().getDaoSession().getPHMessageDao();
message.setDate(new Date(customNotification.getTime()));
message.setMessageId(uuid);
message.setSessionId(session.getSessionId());
message.setExValue(customNotification.getContent());
message.setSessionType(session.getSessionType());
Long id = messageDao.insert(message);
notifyNotification(uuid, id, "收到一条作业通知");
} else if ("attend".equals(type)) {
//考勤
PHMessageSession session = PHMessageSession.findAndCreateSession(PHMessageSession.sessionType_kaoqin, true);
session.setSessionName("考勤通知");
session.setDate(new Date(customNotification.getTime()));
session.setSessionText(json.optString("title"));
PHMessage message = new PHMessage();
PHMessageDao messageDao = AppContext.getInstance().getDaoSession().getPHMessageDao();
message.setDate(new Date(customNotification.getTime()));
message.setMessageId(uuid);
message.setSessionId(session.getSessionId());
message.setMessageText("不能为空的字段。。。");
message.setExValue(customNotification.getContent());
message.setSessionType(session.getSessionType());
Long id = messageDao.insert(message);
Log.e("aaaaa","------"+id);
notifyNotification(uuid, id, "收到一条考勤通知");
}
} catch (JSONException e) {
e.printStackTrace();
}
}*/
public void notifyNotification(String uuid, long msgLongId, String text) {
if (!isNotiNotification) {
// return;
}
// int notificationCount = (int) PHMessage.findUnreadNotificationCount();//XSTMessage.findUnreadNotificationCount();
int notificationCount=DBMessageController.getInstance(AppContext.getContext()).getMessagesCountsAllNoRead();
boolean setBadgerSuccessful = ShortcutBadger.applyCount(AppContext.getInstance(), notificationCount);
Timber.i("----== notificationCount : %s ,setBadgerSuccessful : %s", notificationCount, setBadgerSuccessful);
if (!setBadgerSuccessful) {
BadgeIntentService.start(AppContext.getInstance(), notificationCount, "通知", text, uuid, msgLongId);
return;
}
Intent resultIntent = new Intent(AppContext.getInstance(), MainActivity.class);
resultIntent.putExtra("sid", msgLongId);
resultIntent.putExtra("uuid", uuid);
//resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
AppContext.getInstance(),
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(AppContext.getInstance())
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("通知")
.setContentText(text)
//.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE)
.setAutoCancel(true)
.setContentIntent(resultPendingIntent);
AppConfig config = AppConfig.getAppConfig(AppContext.getInstance());
boolean vibrate = Boolean.parseBoolean(config.get(AppConfig.NIM_CONFIG_VIBRATE));//XSTApp.instance.getVibrateState();
boolean sound = Boolean.parseBoolean(config.get(AppConfig.NIM_CONFIG_SOUND));//XSTApp.instance.getRingState();
if (vibrate && sound) {
mBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
} else if (vibrate) {
mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
} else if (sound) {
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
}
NotificationManager notifyMgr =
(NotificationManager) AppContext.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);
notifyMgr.notify((int) msgLongId, mBuilder.build());
}
public Observable<NIMLoginResultBean> login(String account, String password) {
final PublishSubject<NIMLoginResultBean> subject = PublishSubject.create();
SharedPreferences sp = AppContext.getInstance().getApplicationContext().getSharedPreferences("info", Context.MODE_PRIVATE);
sp.edit().putString("account", account).apply();
sp.edit().putString("token", password).apply();
NIMClient.getService(AuthService.class)
.login(new LoginInfo(account, password))
.setCallback(new RequestCallback<Object>() {
@Override
public void onSuccess(Object o) {
NIMLoginResultBean bean = new NIMLoginResultBean(true, 200, null);
subject.onNext(bean);
// Timber.i("----===onSuccess : %s", o);
}
@Override
public void onFailed(int i) {
// Timber.i("----===nim login failed : %s", i);
NIMLoginResultBean bean = new NIMLoginResultBean(false, i, null);
subject.onNext(bean);
}
@Override
public void onException(Throwable throwable) {
// Timber.i("----===onException");
if (throwable != null) throwable.printStackTrace();
NIMLoginResultBean bean = new NIMLoginResultBean(false, 0, throwable);
subject.onNext(bean);
}
});
return subject;
}
public void logout() {
SharedPreferences sp = AppContext.getInstance().getApplicationContext().getSharedPreferences("info", Context.MODE_PRIVATE);
sp.edit().putString("account", "").apply();
sp.edit().putString("token", "").apply();
NIMClient.getService(MsgServiceObserve.class).observeReceiveMessage(messageObserver, false);
NIMClient.getService(MsgServiceObserve.class).observeCustomNotification(customNotificationObserver, false);
NIMClient.getService(AuthService.class).logout();
}
public interface NIMLoginCallback {
void onResult(boolean isSuccess, int resultCode, Throwable throwable);
}
}