MessageManager.java 2.02 KB
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<List<IMMessage>> messageObserver = new Observer<List<IMMessage>>() {
        @Override
        public void onEvent(List<IMMessage> imMessages) {
            onMessageReceive(imMessages);
        }
    };
    private Observer<CustomNotification> customNotificationObserver = new Observer<CustomNotification>() {
        @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<IMMessage> 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();
    }
}