Push Notification does not fit - Android

I am trying to add image and action buttons to a push notification. But this does not work properly.

The notification looks like this when I first click on the button and go to another activity (no image):

enter image description here

But if I go back again and press the button again, the image will appear with a few buttons:

enter image description here

In addition, the context text of the notification is not displayed. Here is the code:

    notification.setContentTitle("Successfully Signed Up");
    notification.setContentText("Hi, you just Signed Up as a Vendor");
    notification.setWhen(System.currentTimeMillis());
    notification.setSmallIcon(R.mipmap.ic_launcher);
    Intent i=new Intent(this,OrderTypes.class);
    PendingIntent pi=PendingIntent.getActivity(this, 0, i, 0);
    notification.addAction(R.mipmap.ic_launcher,"Accept",pi);
    notification.addAction(R.mipmap.ic_launcher, "Decline", pi);

    /*NotificationCompat.MediaStyle mediaStyle=new NotificationCompat.MediaStyle();
    MediaSessionCompat mediaSession=new MediaSessionCompat(this,TAG);
    mediaStyle.setShowActionsInCompactView(1);
    mediaStyle.setMediaSession(mediaSession.getSessionToken());
    notification.setStyle(mediaStyle);*/


    NotificationCompat.BigPictureStyle notiStyle = new NotificationCompat.BigPictureStyle();
    //notiStyle.setBigContentTitle("Big Picture Expanded");
    //notiStyle.setSummaryText("Nice big picture.");

    new Thread(new Runnable() {
        @Override
        public void run() {
            try
            {
                remote_picture = BitmapFactory.decodeStream((InputStream) new URL(getIntent().getExtras().getString("imageurl")).getContent());
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }).start();
    notiStyle.bigPicture(remote_picture);

    notification.setStyle(notiStyle);
    Intent intent = new Intent(this, VendorDetails.class);
    intent.putExtra("Phone", num);
    PendingIntent pendingIntent = PendingIntent.getActivity(Verify.this, 0, intent, 0);
    notification.setContentIntent(pendingIntent);

    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nm.notify(uniqueID, notification.build());

New full updated feature:

      public void defineNotification(String num) {
    new Thread(new Runnable()
    {
        public void run() {
            try {
                notification.setContentTitle("New Order Received")

                notification.setContentText("Train No.12724 \n Amount 500");
                notification.setWhen(System.currentTimeMillis());
                notification.setSmallIcon(R.drawable.omitra_notification_icon);
                Intent i = new Intent(this, OrderTypes.class);
                PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);
                notification.addAction(R.drawable.pink_accept, "Accept", pi);
                notification.addAction(R.drawable.pink_decline, "Decline", pi);
                notification.setPriority(Notification.PRIORITY_MAX);

                NotificationCompat.BigPictureStyle notiStyle = new NotificationCompat.BigPictureStyle();

                //String url=getIntent().getExtras().getString("imageurl");
                //ImageLoader imageLoader=new ImageLoader();
                try {
                    remote_picture = BitmapFactory.decodeStream((InputStream) new URL(getIntent().getExtras().getString("imageurl")).getContent());
                } catch (IOException e) {
                    e.printStackTrace();
                }

                notiStyle.setSummaryText("Amount : 500");
                notiStyle.bigPicture(remote_picture);
                notification.setStyle(notiStyle);


                Intent intent = new Intent(this, VendorDetails.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                intent.putExtra("Phone", num);
                PendingIntent pendingIntent = PendingIntent.getActivity(Verify.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                notification.setContentIntent(pendingIntent);
                NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                //nm.cancel(uniqueID);
                nm.notify(uniqueID, notification.build());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

Does anyone know if I missed something?

+2
source share
1 answer

Try it -

public void defineNotification(String num) {
    new Thread(new Runnable()
    {
        public void run() {
            try {
                Intent i = new Intent(Verify.this, OrderTypes.class);
                PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);

                notification.setContentTitle("New Order Received")
                notification.setContentText("Train No.12724 \n Amount 500");
                notification.setWhen(System.currentTimeMillis());
                notification.setSmallIcon(R.drawable.omitra_notification_icon);
                notification.addAction(R.drawable.pink_accept, "Accept", pi);
                notification.addAction(R.drawable.pink_decline, "Decline", pi);
                notification.setPriority(Notification.PRIORITY_MAX);

                remote_picture = BitmapFactory.decodeStream((InputStream) new URL(getIntent().getExtras().getString("imageurl")).getContent());
                NotificationCompat.BigPictureStyle notiStyle = new NotificationCompat.BigPictureStyle();
                notiStyle.setSummaryText("Amount : 500");
                notiStyle.bigPicture(remote_picture);
                notification.setStyle(notiStyle);

                Intent intent = new Intent(Verify.this, VendorDetails.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                intent.putExtra("Phone", num);
                PendingIntent pendingIntent = PendingIntent.getActivity(Verify.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
                notification.setContentIntent(pendingIntent);
                NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                nm.notify(uniqueID, notification.build());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
}
+1
source

Source: https://habr.com/ru/post/1620947/


All Articles