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

前端 未结 2 2003
刺人心
刺人心 2021-01-19 23:01

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 w

相关标签:
2条回答
  • 2021-01-19 23:41

    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 :

    enter image description here

    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!");
    
    0 讨论(0)
  • 2021-01-19 23:41

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

    0 讨论(0)
提交回复
热议问题