111
This commit is contained in:
@@ -22,9 +22,15 @@ 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);
|
||||
@@ -35,36 +41,70 @@ public class MyNotificationService extends FirebaseMessagingService {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public void onMessageReceived(RemoteMessage remoteMessage) {
|
||||
Log.e("gmc","remoteMessage");
|
||||
|
||||
public void onMessageReceived(RemoteMessage message) {
|
||||
// do nothing
|
||||
sendNotification("remoteMessage");
|
||||
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);
|
||||
|
||||
}
|
||||
// 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);
|
||||
//
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user