2015-12-21 23 views
2

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

通知是这样的,当我第一次点击该按钮,并移动到其他活动(图片丢失):

enter image description here

但是,如果我回去,然后再次单击该按钮,图像显示带有多个按钮:

enter image description here

此外,该通知的上下文文本未示出。下面是代码:

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

有谁知道,如果我错过了什么吗?

+0

首先你应该得到一个位图(remote_picture你的情况)。确保你在非UI线程中解码它。另外,我建议您正确处理加载位图时可能发生的异常。获得位图后,在通知中使用它。 –

+0

谢谢M.SI.你知道我们如何管理这个图像的高度和宽度? –

回答

1

试试这个 -

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

我试过这样做,但这给了我一个错误,而实例化意图说“不能解决构造函数” –

+0

请分享完整的功能 – Deepak

+0

它仍然给我错误的Intent构造函数。我们不能在线程中使用“this”,因为它会指向一个Runnable对象,而不是该类,并且我找不到任何其他选项来执行此操作。 –