4

I made a service and i'm trying to launch it in foreground. It works but the problem is that it shows just a standard notification instead of my custom notification. The notification which is showed is a standard one with this standard message "name_of_application is running click here if you want more information"

The code of my acitivty in which i launch the service is:

    void StartServiceNavigation(){


            Intent startIntent = new Intent(PlanTheTripActivity.this, FollowUserService.class);
            startIntent.setAction(Constants.ACTION.STARTFOREGROUND_ACTION);
            startService(startIntent);

and the code on my service is:

        public class FollowUserService extends Service {
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {

            if (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) {
                Toast.makeText(this,"Start Service in ForeGround",Toast.LENGTH_SHORT).show();
                Log.i("service", "Received Start Foreground Intent ");


                Intent notificationIntent = new Intent(this, PlanTheTripActivity.class);
                notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
                notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationInten`enter code here`t, 0);

                RemoteViews notificationView = new RemoteViews(getPackageName(),R.layout.notification);
                notificationView.setTextViewText(R.id.testo_custom,"provaprova");

                 Intent previousIntent = new Intent(this, FollowUserService.class);
                 PendingIntent ppreviousIntent = PendingIntent.getService(this, 0, previousIntent, 0);


                 Bitmap icon = BitmapFactory.decodeResource(getResources(),R.drawable.muoversi_a_roma);

                Notification notificationCustom = new NotificationCompat.Builder(this)
                        .setContentTitle("Navigazione Muovi Roma")
                        .setTicker("AOOOOOO")
                        .setContentText("CHE FINE HA FATTO")
                        .setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false))
                        .setContent(notificationView)
                        .setContentIntent(pendingIntent)
                        .addAction(R.drawable.ic_media_play, "Play", ppreviousIntent)
                        .setOngoing(true)
                        .build();



                startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE,
                        notificationCustom);
            }





            return super.onStartCommand(intent, flags, startId);
        }

        @Override
        public void onCreate() {
            super.onCreate();

and the code of my custom layout is:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/notification_image"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:src="@drawable/muoversi_a_roma"/>

    <TextView
        android:id="@+id/testo_custom"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:text="prova"
        />
cec91
  • 45
  • 6
  • have you solved this? [This](https://stackoverflow.com/questions/8725909/startforeground-does-not-show-my-notification) looks like a similar question – Jakub Licznerski Oct 21 '21 at 12:45

0 Answers0