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 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.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.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> messageObserver = new Observer>() { @Override public void onEvent(List imMessages) { onMessageReceive(imMessages); } }; private Observer customNotificationObserver = new Observer() { @Override public void onEvent(CustomNotification customNotification) { 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() { @Override public void onEvent(StatusCode statusCode) { if (statusCode == StatusCode.KICKOUT) { 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 messageList) { } private void onCustomNotificationReceive(CustomNotification customNotification) { try { JSONObject json = new JSONObject(customNotification.getContent()); String type = json.optString("type"); String uuid = json.optString("msgId"); 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")); 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, "收到一条作业通知"); } } catch (JSONException e) { e.printStackTrace(); } } public void notifyNotification(String uuid, long msgLongId, String text) { if (!isNotiNotification) return; int notificationCount = (int) PHMessage.findUnreadNotificationCount();//XSTMessage.findUnreadNotificationCount(); 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 login(String account, String password) { final PublishSubject 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() { @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); ToastUtils.showToast("云信服务器登录失败:" + 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() { 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); } }