Notifications with android firebase

1

Good morning I have a problem. I would like you to help me as I am implementing push notifications with firebase in android the notification has the title body and an image I send from < strong> php my code is as follows:

function.php

        function enviarNotificacion($result, $msj, $title, $photo)
        {

            $registrationIds = [0];
            $x = 0;
            while($r = mysqli_fetch_object($result))
            {
                $registrationIds[$x] = $r->token;
            print("                /               ");
            print $r->token;
                $x++;
            }

            // prep the bundle
    //        $msg = [
      //          'title'         =>  $title,
        //        'body'          =>  $msj,
          //      'sound'         => 'default',
            //    'vibrate'       =>'1'
    $msg = array ("body" => $msj , "title" => $title, "sound" => 'default',"vibrate" =>'1');
            //];
    $dat = [
         'imagen' => $photo
    ];
            $fields = [
                'registration_ids'  => $registrationIds,
                'notification'              => $msg,
                'data'    => $dat
            ];
     $headers = [
                'Authorization: key=' . API_ACCESS_KEY,
                'Content-Type: application/json'
            ];
            $fields = json_encode( $fields );

            //apt-get install -y php5-curl

            $ch = curl_init();
            curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
            curl_setopt( $ch,CURLOPT_POST, true );
            curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
            curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
            curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
            curl_setopt( $ch,CURLOPT_POSTFIELDS, $fields );
            $result = curl_exec($ch );
            curl_close( $ch );
            session_start();
            exitosamente";

            return $result;
        }

class in java:

notification.java

    public class NoticiasFirebaseMessaginService extends FirebaseMessagingService 
    {
     private static final String TAG = "NoticiasMessaginService";
        private   Bitmap image ;
         public  String img ;

        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            super.onMessageReceived(remoteMessage);
            Log.d(TAG, "from "+ remoteMessage.getFrom());
            Noticia noticia = new Noticia();
            noticia.setTipo(remoteMessage.getNotification().getTitle());
            noticia.setDescripcion(remoteMessage.getNotification().getBody());
            //noticia.setPhoto(remoteMessage.getNotification().getIcon());
            img = remoteMessage.getData().get("imagen");
            showNotification(noticia);

        }

        public void showNotification(Noticia noticia)
        {
            Intent intent = new Intent(this, PrincipalActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent
                    = PendingIntent.getActivity( this,0 ,intent,PendingIntent.FLAG_ONE_SHOT);
            Uri defaultSoundUri  = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

            try {
                image = Picasso.with(this).load(img).get();
                Log.i(TAG,"el el try");
            } catch (IOException e) {

                Log.i(TAG,"el el catch");
                e.printStackTrace();
            }

           NotificationCompat.Builder  notificationBuilder= new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.logo_sin_fondo)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.useragricultor))
                    .setContentTitle(noticia.getTipo())
                    .setContentText(noticia.getDescripcion())
                    .setStyle(new  NotificationCompat.BigPictureStyle().bigPicture(image).setSummaryText(noticia.getDescripcion()))
                    .setAutoCancel(true)
                    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                    .setSound(defaultSoundUri)
                    .setVibrate(new long[]{0,300,200,300})
                    .setContentIntent(pendingIntent);
                     NotificationManager notificationManager
                    = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(0,notificationBuilder.build());

        }

    }

When the app is open the notification comes to me in the following way:

But if the app is closed only something like this arrives:

I do not know where the error is or what needs to be done so that the notification comes from being closed the app in the same way as when it is open thanks to those who can help me, it would be very useful.
Thank you.

    
asked by Cediendo_Lo_Aprendido 21.07.2017 в 20:12
source

1 answer

0

In the notification you are precisely defining a style which shows the image when expanding the notification, it deletes the image that it loads using the bigPicture() method:

    //.setStyle(new  NotificationCompat.BigPictureStyle().bigPicture(image).setSummaryText(noticia.getDescripcion()))
.setStyle(new  NotificationCompat.BigPictureStyle().setSummaryText(noticia.getDescripcion()))

The problem is that if you use NotificationCompat.BigPictureStyle () and you do not want the image, you would see something similar to this:

I advise you to better use BigTextStyle ()

 .setStyle(new  NotificationCompat.BigTextStyle().setSummaryText(noticia.getDescripcion()))

Regarding your question:

  

I do not know where the error is or what needs to be done so that the   notification comes from being closed the app in the same way as   when it is open.

If you want it to be displayed in the same way, it is simply not assigning a style

//.setStyle(new  NotificationCompat.BigPictureStyle().bigPicture(image).setSummaryText(Summary noticia.getDescripcion()))

Update:

The user wants the notification to be expanded when it is received by the device, this is not possible until today , notifications are received in this way:

and later you can interact with it, either by clicking or expanding it, if the defined style allows it.

    
answered by 21.07.2017 в 23:11