113 lines
4.7 KiB
Java
113 lines
4.7 KiB
Java
/*
|
|
package com.shayu.phonelive.utils;
|
|
|
|
import android.app.Notification;
|
|
import android.app.NotificationChannel;
|
|
import android.app.NotificationManager;
|
|
import android.app.PendingIntent;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.graphics.Bitmap;
|
|
import android.graphics.BitmapFactory;
|
|
import android.media.RingtoneManager;
|
|
import android.net.Uri;
|
|
import android.util.Log;
|
|
import android.widget.RemoteViews;
|
|
|
|
import androidx.core.app.NotificationCompat;
|
|
import androidx.core.app.NotificationManagerCompat;
|
|
|
|
import com.google.android.gms.common.internal.zzm;
|
|
import com.google.firebase.messaging.FirebaseMessagingService;
|
|
import com.google.firebase.messaging.RemoteMessage;
|
|
import com.yunbao.common.utils.ToastUtil;
|
|
import com.yunbao.main.activity.MainActivity;
|
|
|
|
import org.json.JSONException;
|
|
import org.json.JSONObject;
|
|
|
|
import java.util.Map;
|
|
|
|
import myname.pdlive.shayu.R;
|
|
|
|
public class MyNotificationService extends FirebaseMessagingService {
|
|
|
|
private String createNotificationChannel(String channelID, String channelNAME, int level) {
|
|
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
|
|
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
|
NotificationChannel channel = new NotificationChannel(channelID, channelNAME, level);
|
|
manager.createNotificationChannel(channel);
|
|
return channelID;
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public void onMessageReceived(RemoteMessage message) {
|
|
// do nothing
|
|
Map<String, String> params = message.getData();
|
|
JSONObject object = new JSONObject(params);
|
|
Log.e("JSON_OBJECT", object.toString());
|
|
JSONObject remoteMessage = null;
|
|
try {
|
|
remoteMessage = new JSONObject(object.getString("message"));
|
|
Log.e("gmc","remoteMessage1"+remoteMessage.toString());
|
|
Log.e("gmc","remoteMessage"+remoteMessage.getString("fromUserName"));
|
|
} catch (JSONException e) {
|
|
e.printStackTrace();
|
|
}
|
|
// if(remoteMessage!=null && remoteMessage.isNull("fcm_token")) {
|
|
// String title = remoteMessage.getData().get("title");
|
|
// String message = remoteMessage.getData().get("text");
|
|
// String username = remoteMessage.getData().get("username");
|
|
// String uid = remoteMessage.getData().get("uid");
|
|
// String fcmToken = remoteMessage.getData().get("fcm_token");
|
|
// Log.e("gmc","remoteMessage"+remoteMessage.getData().get("username"));
|
|
//
|
|
// //Muestro la notifiación
|
|
//// sendNotification(title, message, username, uid, fcmToken);
|
|
// }else {
|
|
// /// Si es de tipo inserción la muestro sino no.
|
|
// //Es una nueva notificación de que alguien ha creado algo
|
|
// if(remoteMessage.getData().get("accion")!=null &&
|
|
// remoteMessage.getData().get("accion").compareTo("insert")==0)
|
|
//// displayNotification(remoteMessage.getNotification(), remoteMessage.getData());
|
|
//// //Envío los datos al RecyclerView correspondiente para que se actualice
|
|
//// addNotificacion(remoteMessage);
|
|
// Log.e("gmc","remoteMessage"+remoteMessage.getData().get("username"));
|
|
//
|
|
// }
|
|
|
|
// sendNotification("remoteMessage");
|
|
}
|
|
|
|
|
|
|
|
// private void sendNotification(String messageBody) {
|
|
//
|
|
// // 设置通知的点击行为:这里启动一个 Activity
|
|
// Intent intent = new Intent(this, MainActivity.class);
|
|
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
|
// PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
|
|
// String channelId = createNotificationChannel("my_channel_ID", "my_channel_NAME", NotificationManager.IMPORTANCE_HIGH);
|
|
// // 构建 remoteView
|
|
//// RemoteViews remoteView = new RemoteViews(getPackageName(), R.layout.notification_message);
|
|
//
|
|
//// remoteView.setTextViewText(R.id.tvMsg, "RemoteViews");
|
|
//// remoteView.setImageViewResource(R.id.ivIcon, R.mipmap.ic_launcher_round);
|
|
// NotificationCompat.Builder builder = new NotificationCompat.Builder(this,channelId);
|
|
// // 设置自定义 RemoteViews
|
|
// builder.setContent(remoteView).setSmallIcon(R.mipmap.ic_launcher);
|
|
// // 设置通知的优先级(悬浮通知)
|
|
// builder.setPriority(NotificationCompat.PRIORITY_MAX);
|
|
// builder.setContentIntent(pendingIntent);
|
|
// builder.setAutoCancel(true);
|
|
// Notification notification = builder.build();
|
|
// NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
|
|
// manager.notify(0, notification);
|
|
//
|
|
// }
|
|
|
|
}
|
|
*/
|