package com.shunzhi.parent.manager; import com.netease.nimlib.sdk.NIMClient; import com.netease.nimlib.sdk.Observer; import com.netease.nimlib.sdk.auth.AuthService; 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 java.util.List; /** * Created by 10501 on 2018/3/17. */ public class MessageManager { private static class InstanceHolder { static final MessageManager instance = new MessageManager(); } public 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); } private void onMessageReceive(List messageList) { } private void onCustomNotificationReceive(CustomNotification customnotification) { } public void login(String account, String password) { NIMClient.getService(AuthService.class).login(new LoginInfo(account, password)); } public void logout() { NIMClient.getService(MsgServiceObserve.class).observeReceiveMessage(messageObserver, false); NIMClient.getService(MsgServiceObserve.class).observeCustomNotification(customNotificationObserver, false); NIMClient.getService(AuthService.class).logout(); } }