Files
web_fengzhuang/Judy88/src/main/java/com/web/judy88/MyFirebaseMessageingService.java
2025-02-06 17:39:24 +08:00

172 lines
7.9 KiB
Java

package com.web.judy88;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.google.gson.Gson;
import com.web.base.GsonUtils;
import com.web.base.MessageInfo;
import java.util.Map;
import java.util.Random;
public class MyFirebaseMessageingService extends FirebaseMessagingService {
public MyFirebaseMessageingService() {
}
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Map<String, String> serviceData = remoteMessage.getData(); //后台推送数据
if (serviceData != null && serviceData.containsKey("message")) {
String value = serviceData.get("message");
Gson gson = new Gson();
MessageInfo messageInfo = gson.fromJson(value, MessageInfo.class);
showNotification(messageInfo);
// if (remoteMessage.getNotification() != null) {
// showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
// }
} else {
//收到通知 创建notify
if (remoteMessage.getNotification() != null) {
showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
}
}
}
private void showNotification(MessageInfo messageInfo) {
Intent notifyIntent = new Intent(this, com.web.base.MainActivity2.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) {
ComponentName launchComponent = null;
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); // 必须
notifyIntent.putExtra("message", messageInfo);
PendingIntent pendingIntent = PendingIntent.getActivity(this, new Random().nextInt(10000), notifyIntent, PendingIntent.FLAG_IMMUTABLE);
// Intent notifyIntent = new Intent(this, MainActivity2.class);
// notifyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
// // notifyIntent.putExtra("message", messageInfo);
// notifyIntent.setAction(Intent.ACTION_VIEW);
// notifyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 必须
// PendingIntent pendingIntent;
// pendingIntent = PendingIntent.getActivity
// (this, 0, notifyIntent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channelwinway = null;
NotificationCompat.Builder notificationBuilder = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
channelwinway = new NotificationChannel(getString(com.web.base.R.string.app_name), "notify", NotificationManager.IMPORTANCE_DEFAULT);
channelwinway.enableLights(true);
channelwinway.enableVibration(true);
notificationManager.createNotificationChannel(channelwinway);
notificationBuilder = new NotificationCompat.Builder(this, channelwinway.getId())
.setSmallIcon(com.web.base.R.mipmap.ic_launcher)
.setContentTitle(messageInfo.getTitle())
.setContentText(messageInfo.getContent())
.setAutoCancel(true)
.setContentIntent(pendingIntent);
} else {
notificationBuilder = new NotificationCompat.Builder(this, getString(com.web.base.R.string.app_name))
.setSmallIcon(com.web.base.R.mipmap.ic_launcher)
.setContentTitle(messageInfo.getTitle())
.setContentText(messageInfo.getContent())
.setAutoCancel(true)
.setContentIntent(pendingIntent);
}
notificationManager.notify(0, notificationBuilder.build());
}
private void showNotification(String title, String body) {
Intent notifyIntent = new Intent(this, MainActivity2.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) {
ComponentName launchComponent = null;
launchComponent = getApplication()
.getPackageManager()
.getLaunchIntentForPackage(getApplication().getPackageName())
.getComponent();
notifyIntent.setComponent(launchComponent);
}
notifyIntent.putExtra("message", body);
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, new Random().nextInt(10000), notifyIntent, PendingIntent.FLAG_IMMUTABLE);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channelwinway = null;
NotificationCompat.Builder notificationBuilder = null;
MessageInfo messageInfo = GsonUtils.getObjFromJSON(body, MessageInfo.class);
if (messageInfo != null) {
body = messageInfo.getContent();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
channelwinway = new NotificationChannel(getString(com.web.base.R.string.app_name), "notify", NotificationManager.IMPORTANCE_DEFAULT);
channelwinway.enableLights(true);
channelwinway.enableVibration(true);
notificationManager.createNotificationChannel(channelwinway);
notificationBuilder = new NotificationCompat.Builder(this, channelwinway.getId())
.setSmallIcon(com.web.base.R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
} else {
notificationBuilder = new NotificationCompat.Builder(this, getString(com.web.base.R.string.app_name))
.setSmallIcon(com.web.base.R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
}
notificationManager.notify(0, notificationBuilder.build());
//存储数据
// saveNotifyMessage(body);
}
// public void saveNotifyMessage(String body) {
// MessageInfo messageInfo = GsonUtils.getObjFromJSON(body, MessageInfo.class);
// String savenotify = Utils.get(getApplication(),ApiService.savenotify,"");
// if(messageInfo!=null){
// if(TextUtils.isEmpty(savenotify)){
// GsonUtils.getListFromJSON(savenotify,)
// }
// }
// String jsonString = GsonUtils.beanToJSONString(chatMessageBeans);
// }
//
// /**
// * @param key 要设置的key
// */
// public static void set(Context activity, String key, String is) {
// SharedPreferences nameSetting = getConfigShared(activity);
// SharedPreferences.Editor namePref = nameSetting.edit();
// namePref.putString(key, is);
// namePref.commit();
// }
}