Set a picture in notification alert and other fix- android

3.5k views Asked by At

here is the code I use to set a notification for any event. But my requirement is, Instead of just only text I also want to display a picture in the notification panel, which is not a logo, a complete picture (or may be cropped). How to modify this code to get this work done.

Secondly, my notification does it job properly,but when there are too many text to display, it does not expands. Is there any way to make a notification expandable.

Thirdly, some portion of my code, you will see that, those commands are deprecated( when I am initiating the notification and also at notification.setLatestInfo()). So, what is the right way to write it, and if I write it would it work on lower version as well? thanks in advance.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ok = (Button) findViewById(R.id.dones);
    et = (EditText) findViewById(R.id.ets);


    ok.setOnClickListener(MainActivity.this);

    counter = getSharedPreferences("count", 0);

    notificationId = counter.getInt("val", 0);
}

@SuppressWarnings("deprecation")
@Override
public void onClick(View arg0) {
    String Text = et.getText().toString();

    if(Text.equals("")){
        Toast.makeText(getApplicationContext(), "You Don't have anything to clip", Toast.LENGTH_SHORT).show();
    }
    else{
        notification = new Notification(R.drawable.ic_launcher, "You Got Things ToDo!", System.currentTimeMillis());
        notificationmanager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent pending = PendingIntent.getActivity(this, 0, new Intent(this,MainActivity.class), 0);
        notification.setLatestEventInfo(getApplicationContext(), "I want to ..", Text, pending);

        notificationmanager.notify(notificationId, notification);
        notificationId++;

        notification.flags |= Notification.FLAG_NO_CLEAR;

    }
}
2

There are 2 answers

9
sddamico On BEST ANSWER

You should check out Notification.Builder, particularly setStyle():

http://developer.android.com/reference/android/app/Notification.Builder.html

and then Notification.BigPictureStyle should allow you to show an image like you desire:

http://developer.android.com/reference/android/app/Notification.BigPictureStyle.html

Edited with code sample, since you're processing the Bitmap as well, I've added an AsyncTask so that the processing doesn't occur on the UI thread:

else{
    new AsyncTask<Void, Void, Void>(){

        public Void doInBackround(Void... args){
            PendingIntent pending = PendingIntent.getActivity(arg0.getContext(), 0, new Intent(arg0.getContext(),MainActivity.class), 0);

            Bitmap bmp = BimapFactory.decodeResource(arg0.getContext().getResources(), R.id.my_img);

            notification = new NotificationCompat.Builder(arg0.getContext())
             .setContentTitle("I want to...")
             .setContentText(Text)
             .setContentIntent(pending)
             .setSmallIcon(R.drawable.ic_launcher)
             .setStyle(new NotificationCompat.BigPictureStyle()
                 .bigPicture(bmp))
             .build();
            notificationmanager = (NotificationManager)arg0.getContext().getSystemService(Context.NOTIFICATION_SERVICE);

            notificationmanager.notify(notificationId, notification);
            notificationId++;

        }
    }.execute();
}
3
Greg Ennis On

You can do this with RemoteViews in a Custom Notification Layout. If you have a specific question feel free to post it.