How to add a Dynamic image instead of notification icon in android?

爷,独闯天下 提交于 2019-12-20 01:08:23

问题


I used below code for displaying notification in notification Bar.

it worked fine.

But i need to display notification icon dynamically that will come from web service.

How i can do?

NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

Notification note = new Notification(R.drawable.image,"status message", System.currentTimeMillis());

        Intent in = new Intent(Notify.this,CommentU.class);
        PendingIntent pi = PendingIntent.getActivity(Notify.this, 0, in, 0);
        note.setLatestEventInfo(Notify.this,"NotificationTitle", "You have a new Commentio", pi);
        note.number = ++count;
        note.vibrate = new long[] { 500l, 200l, 200l, 500l };
        note.flags |= Notification.FLAG_AUTO_CANCEL;
        nm.notify(NOTIFY_ME_ID, note);

Thanks in Advance.


回答1:


I have one suggestion for you: you have to download that image which you want to show and then you can set that as bitmap: check below code. I have created one BITMAP. its look link :

for this you have to add android-support-v4.jar

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("My notification").setLargeIcon(BITMAP)
                .setContentText("Hello World!");

        Intent resultIntent = new Intent(this, test.class);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

        stackBuilder.addParentStack(test.class);

        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
                PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        mNotificationManager.notify(NOTIFY_ME_ID, mBuilder.build());

for more detail chekc this link.

Removing notifications

Notifications remain visible until one of the following happens:

The user dismisses the notification either individually or by using "Clear All" (if the notification can be cleared).

The user clicks the notification, and you called setAutoCancel() when you created the notification. You call cancel() for a specific notification ID. This method also deletes ongoing notifications.

You call cancelAll(), which removes all of the notifications you previously issued.

Edited: just replace this.

mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("My notification").setLargeIcon(BITMAP)
                .setAutoCancel(true)
                .setContentText("Hello World!");



回答2:


just add some icons in your resource folder and then

int myIcon = R.drawable.image;

your logic goes here ....

and change value of icon according to your logic like myIcon = R.drawable.somenewimage;

and then finally set your notification

Notification note = new Notification(myIcon,"status message", System.currentTimeMillis());



来源:https://stackoverflow.com/questions/15738800/how-to-add-a-dynamic-image-instead-of-notification-icon-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!