• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • About
  • Life
  • Tech
  • Travel
  • Work
  • Questions
  • Contact

Welcome

.

Not able to show the dynamic layout created using java code when running a background service(FirebaseMessagingService)

April 10, 2020 by

Questions › Not able to show the dynamic layout created using java code when running a background service(FirebaseMessagingService)
0
Vote Up
Vote Down
Garmaine asked 3 years ago

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();
                    }
                });
    }
}

Are you looking for the answer?
Original Question and Possible Answers can be found on `http://stackoverflow.com`

Question Tags: android, java

Please login or Register to submit your answer




Primary Sidebar

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Newsletter

Complete the form below, and we'll send you all the latest news.

Footer

Footer Funnies

Who knew that reading the footer could be such a hilarious adventure? As we navigate websites, books, and documents, we often stumble upon the unassuming space at the bottom, only to discover a treasure trove of amusement. In this side-splitting compilation, we present 100 jokes that celebrate the unsung hero of content – the footer. Get ready to chuckle, giggle, and maybe even snort as we dive into the world of footnotes, disclaimers, and hidden comedic gems. Brace yourself for a wild ride through the footer!

Recent

  • Unveiling the Enigma: Almost-Magical Lamp Lights Highway Turns
  • The Impact of Young Onset Dementia on Employment and Finances: Optimizing Post-Diagnostic Approaches
  • 11 Wonders of 2023 Technological Breakthrough – Unveiling the Future
  • Work from Home and Stay Mentally Sane – Achieve Productivity and Well-being
  • Hidden Gems of New York City – Uncover the Must-See Treasures!

Search

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Copyright © 2023