Files
yuliao_and/app/src/main/java/com/hbl/lewan/NotificationMsgService.java
2025-08-08 15:08:52 +08:00

152 lines
6.2 KiB
Java

package com.hbl.lewan;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import androidx.core.app.ServiceCompat;
import com.hbl.lewan.utils.LogUtils;
import com.netease.nimlib.sdk.msg.constant.MsgTypeEnum;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.netease.nimlib.sdk.msg.model.IMMessage;
import com.netease.yunxin.kit.chatkit.model.IMMessageInfo;
import com.netease.yunxin.kit.chatkit.repo.ChatObserverRepo;
import com.netease.yunxin.kit.chatkit.ui.common.ChatDataUtils;
import com.netease.yunxin.kit.corekit.im.IMKitClient;
import com.netease.yunxin.kit.corekit.im.model.EventObserver;
import com.netease.yunxin.kit.corekit.im.provider.FriendProvider;
import com.netease.yunxin.kit.corekit.im.repo.SettingRepo;
import java.util.List;
public class NotificationMsgService extends Service {
//处理在线通知而已
private final EventObserver<List<IMMessageInfo>> receiveMessageObserver =
new EventObserver<>() {
@Override
public void onEvent(@Nullable List<IMMessageInfo> event) {
if (IMApplication.getForegroundActCount() == 0) { //在前台不推送
LogUtils.d("messagechat" + "收到通知收到通知");
IMMessage messageInfo = event.get(0).getMessage();
if (messageInfo.getMsgType() == MsgTypeEnum.tip && messageInfo.getPushPayload().containsKey("sound")) {
return;
}
if (SettingRepo.isPushNotify() && messageInfo.getMsgType() != MsgTypeEnum.nrtc_netcall) { //音视频单话就结束后会发送一条信息
//收到私聊消息通知
if (messageInfo.getSessionType() == SessionTypeEnum.P2P && FriendProvider.INSTANCE.isNotify(messageInfo.getFromAccount())) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
setNotifications(messageInfo);
}
}
}
} else {
LogUtils.d("messagechat" + "没有收到");
LogUtils.d("不在前台");
}
}
};
@RequiresApi(api = Build.VERSION_CODES.O)
private void setNotifications(IMMessage messageInfo) {
Intent notifyIntent = new Intent();
ComponentName launchComponent = getApplication()
.getPackageManager()
.getLaunchIntentForPackage(getApplication().getPackageName())
.getComponent();
notifyIntent.setComponent(launchComponent);
notifyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
notifyIntent.setAction(Intent.ACTION_VIEW);
notifyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 必须
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notifyIntent, PendingIntent.FLAG_IMMUTABLE);
//获取本地设置消息铃声
NotificationManager notificationManager = (NotificationManager) IMApplication.getAppContext().getSystemService(Context.NOTIFICATION_SERVICE);
String channelID = ChatDataUtils.getMessageBell(IMKitClient.getUserInfo());
LogUtils.d("channelId==" + channelID);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
// IMMessage messageInfo = event.get(0).getMessage();
String content = "";
if (!isShowDetails) {
content = messageInfo.getContent();
} else {
if (messageInfo.getSessionType() == SessionTypeEnum.Team) {
content = getString(R.string.message_notifycontent_team);
} else if (messageInfo.getSessionType() == SessionTypeEnum.P2P) {
content = getString(R.string.message_notifycontent_friend);
} else if (messageInfo.getMsgType() == MsgTypeEnum.avchat) {
content = getString(R.string.message_notifycontent_friend);
}
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelID)
.setSmallIcon(R.mipmap.ic_launche)
.setContentTitle(messageInfo.getFromNick())
.setContentText(content)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
notificationManager.notify(0, builder.build());
}
}
private boolean isShowDetails = true;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
isShowDetails = intent.getBooleanExtra("isShowDetails", true);
//注册通知
ChatObserverRepo.registerReceiveMessageObserve(receiveMessageObserver);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
NotificationChannel channel = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
channel = new NotificationChannel("1","前台服务",
NotificationManager.IMPORTANCE_HIGH);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
Notification notification = new Notification.Builder(getApplicationContext(),"1").build();
startForeground(1, notification);
}
}
@Override
public void onDestroy() {
ChatObserverRepo.unregisterReceiveMessageObserve(receiveMessageObserver);
super.onDestroy();
}
}