Not able to show the dynamic layout created using java code when running a background service(FirebaseMessagingService) I want my app to show a Layout created dynamically using java code whenever it received a notification. Here is the code I implemented for that. You can find the code inside the onMessageReceived()
Now I am stuck don't know what to do?
package com.fitness.client.services;
import android.annotation.SuppressLint;
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.Color;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RemoteViews;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkManager;
import com.fitness.client.App;
import com.fitness.client.R;
import com.fitness.client.api.RetroFitFactory;
import com.fitness.client.api.order.OrderDetails;
import com.fitness.client.api.order.OrderService;
import com.fitness.client.objects.Order;
import com.fitness.client.ui.UiActivity;
import com.fitness.client.ui.main.MainActivity;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import java.util.Objects;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
public class NotificationReceiver extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
TextView userName, extraPeople, slots, date, area;
Button accept, reject;
RelativeLayout mainUi;
public static Boolean notifyService = false;
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service Created", Toast.LENGTH_SHORT).show();
}
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
//Start UiActivity
notifyService = true;
long objectId = Long.parseLong(Objects.requireNonNull(Objects.requireNonNull(remoteMessage.getNotification()).getBody()));
Log.e("Check","order id"+ Objects.requireNonNull(remoteMessage.getNotification()).getBody());
Log.e(TAG, "onMessageReceived: orderId: " + objectId);
// TODO: 08-04-2020 Call a get request for order api end-point using id
RetroFitFactory.getRetrofitCallFor(OrderService.class)
.getOrderDetails(objectId)
.enqueue(new Callback<OrderDetails>() {
@Override
public void onResponse(@NonNull Call<OrderDetails> call, @NonNull Response<OrderDetails> response) {
Order object = null;
if (response.body() != null) {
object = response.body().getData();
}
Context context = NotificationReceiver.this;
assert object != null;
String username = object.getUser().getName();
Log.e(TAG, "onResponse: Username" + username );
String extrapeople ="1";
String userslots = object.getSlots().getFrom() + object.getSlots().getTo();
Log.e(TAG, "onResponse: slots" + userslots );
String userdate = object.getBooking_date();
String userarea = "Navi Mumbai";
LinearLayout mainlayot = new LinearLayout(context);
mainlayot.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
));
mainlayot.setBackgroundColor(Color.parseColor("#8F75D8"));
TextView user_name = new TextView(context);
user_name.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
user_name.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
user_name.setText(username);
mainlayot.addView(user_name);
TextView extra_people = new TextView(context);
extra_people.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
extra_people.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
extra_people.setText("1");
mainlayot.addView(extra_people);
TextView date_booking = new TextView(context);
date_booking.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
date_booking.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
date_booking.setText(userdate);
mainlayot.addView(date_booking);
TextView user_slot = new TextView(context);
user_slot.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
user_slot.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
user_slot.setText(userslots);
mainlayot.addView(user_slot);
TextView user_area = new TextView(context);
user_area.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
user_area.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
user_area.setText("Navi Mumbai");
mainlayot.addView(user_area);
mainlayot.setVisibility(View.VISIBLE);
}
@Override
public void onFailure(@NonNull Call<OrderDetails> call, @NonNull Throwable t) {
Toast.makeText(NotificationReceiver.this, t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
Please login or Register to submit your answer