我正在尝试在推送通知中添加图像和操作按钮。但这不能正常工作。

当我第一次单击该按钮并转到其他活动时(该图像丢失),通知如下所示:

java - 推送通知不正确-Android-LMLPHP

但是,如果我回到并再次单击该按钮,则会显示带有多个按钮的图像:

java - 推送通知不正确-Android-LMLPHP

此外,未显示通知的上下文文本。这是代码:

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


新的完整更新功能:

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


有人知道我在这里错过了什么吗?

最佳答案

尝试这个 -

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

10-07 13:07
查看更多