简体   繁体   中英

Is there a way to set dynamic images (Drawables) as Notification Icons?

I am creating notificaitons and would like to be able to dynamically change the icon displayed in the Notifications (that I am creating/posting).

I am getting error below when I try to do this --> ncomp.setSmallIcon(Drawable)

Is the only way to set a notification icon is from a drawable that is part of my apk package in drawable folder? Is there no way to set dynamic images - like i download several images and set different icons each time i post a notification?????

private void addNotificationtoStatusBar(String packageName, Context ctxt,
        CharSequence tickerText, Notification notifObj) {
    NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    NotificationCompat.Builder ncomp = new NotificationCompat.Builder(ctxt);
    ncomp.setContentTitle(mAppInfos
            .getAppNamefromPackage(packageName, ctxt));
    ncomp.setContentText(tickerText);
    ncomp.setTicker(tickerText);

    PendingIntent resultPendingIntent = notifObj.contentIntent;

    ncomp.setContentIntent(resultPendingIntent);


    Drawable d = myClass.getDrawable();
    ncomp.setSmallIcon(d); // <--**THIS GIVES ERROR**


    ncomp.setAutoCancel(true);
    nManager.notify((int) System.currentTimeMillis(), ncomp.build());
}

I'm answering from my phone so I can't look up the specific methods for you, but I'll answer and you look up in the documentation which method to use.

There are two ways of putting images in the icon.

  • using a fixed drawable from your drawable folder, you pass to the builder simply as the resource ID 'R.drawable.myIcon'

  • you can use a bitmap. This bitmap might be from internet or you generate by code, it doesn't matter, a Bitmap. For that you should use the Builder from the NotifucationsCompat and possibly add themes to it. As a way to download n cache the bitmap I suggest using the Picasso library. 'Picasso.with(context).load(URL).fetch();' or one of the async methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM