Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 推送通知不正确-Android_Java_Android - Fatal编程技术网

Java 推送通知不正确-Android

Java 推送通知不正确-Android,java,android,Java,Android,我正在尝试在按钮通知中添加图像和操作按钮。但这并不正常 当我第一次单击按钮并移动到其他活动(图像丢失)时,通知如下所示: 但如果我返回并再次单击该按钮,则会显示带有多个按钮的图像: 此外,未显示通知的上下文文本。代码如下: notification.setContentTitle("Successfully Signed Up"); notification.setContentText("Hi, you just Signed Up as a Vendor"); n

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

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

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

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

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

首先,你应该得到一个位图(在你的例子中是远程图片)。确保在非UI线程中对其进行解码。此外,我建议您正确处理加载位图时可能出现的异常。得到位图后,请在通知中使用它。谢谢M.SI。您知道如何管理此图像的高度和宽度吗?我尝试过这样做,但在实例化意图时出现了一个错误,即“无法解析构造函数”请共享完整的函数它仍然在意图构造函数中给我错误。我们不能在线程中使用“this”,因为它将指向一个可运行的对象,而不是那个类,我找不到任何其他选项可以这样做。让我们一起来吧。谢谢@Trush。它正在工作。您能告诉我您对我的代码做了哪些更改吗?
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();
}