Search code examples
javaandroidandroid-notificationspicassoandroid-bitmap

How can I add picture to notification large icon with Picasso having only URL of this picture?


How can I set Large Icon to notification? I getting URL of the Image in timetable.getImage(). In my project, Picasso supports get() method, not with(). I am new in android, please help. You can see my notififcation on picture below. Icon image of notification is hardcoded. But I need this icon image from timetable.getImage(). This is my code:

public class EpisodeNotifyActivity extends AppCompatActivity {

private NotificationManagerCompat notificationManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ativity_episode_notify);


    final Timetable timetable = (Timetable) requestService.getResult();
    ImageView serImage = (ImageView) findViewById(R.id.seriesImage1);

    Button button = findViewById(R.id.notify);
    Picasso.get()
            .load(timetable.getImage())
            .into(serImage);    

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            sendNotification(timetable.getSeriesName().toUpperCase() + ", New Episode", timetable.getEpisodesSeason().toString() + "x" + timetable.getEpisodesNumber() + " " + timetable.getEpisodeName());
                      }        });    }  

private void sendNotification(String messageTitle, String messageBody) {
    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Intent intent = new Intent(this, EpisodeNotifyActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        @SuppressLint("WrongConstant")
        NotificationChannel notificationChannel = new NotificationChannel("my_notification", "n_channel", NotificationManager.IMPORTANCE_MAX);
        notificationChannel.setDescription("description");
        notificationChannel.setName("Channel Name");
        assert notificationManager != null;
        notificationManager.createNotificationChannel(notificationChannel);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_live_tv_black_24dp)
                .setContentTitle(messageTitle)
             //   .setLargeIcon(R.drawable.icon)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(soundUri)
                .setContentIntent(pendingIntent)
                .setDefaults(Notification.DEFAULT_ALL)
                .setOnlyAlertOnce(true)
                .setChannelId("my_notification")
                .setColor(Color.parseColor("#3F5996"));
        assert notificationManager != null;
        int m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
        notificationManager.notify(m, notificationBuilder.build());
    }    
}}


Solution

  • public class EpisodeNotifyActivity extends AppCompatActivity {
    
    private NotificationManagerCompat notificationManager;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ativity_episode_notify);
    
    
        final Timetable timetable = (Timetable) requestService.getResult();
        ImageView serImage = (ImageView) findViewById(R.id.seriesImage1);
    
        Button button = findViewById(R.id.notify);
        Picasso.get()
                .load(timetable.getImage())
                .into(serImage);
    
    
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Intent intent = new Intent(this, EpisodeNotifyActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            @SuppressLint("WrongConstant")
            NotificationChannel notificationChannel = new NotificationChannel("my_notification", "n_channel", NotificationManager.IMPORTANCE_MAX);
            notificationChannel.setDescription("description");
            notificationChannel.setName("Channel Name");
            assert notificationManager != null;
            notificationManager.createNotificationChannel(notificationChannel);
    
            final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_live_tv_black_24dp)
                    .setContentTitle(timetable.getSeriesName().toUpperCase() + ", New Episode")
                    .setContentText(timetable.getEpisodesSeason().toString() + "x" + timetable.getEpisodesNumber() + " " + timetable.getEpisodeName())
                    .setAutoCancel(true)
                    .setSound(soundUri)
                    .setContentIntent(pendingIntent)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setOnlyAlertOnce(true)
                    .setChannelId("my_notification")
                    .setColor(Color.parseColor("#3F5996"));
    
    
            Picasso.get().load(timetable.getImage())
                    .into(new Target() {
                        @Override
                        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
    
                            // !!!!!!!!  MAGIC HAPPENS HERE
                            notificationBuilder.setLargeIcon(bitmap);
                        }
    
                        @Override
                        public void onBitmapFailed(Exception e, Drawable errorDrawable) {
                        }
    
                        @Override
                        public void onPrepareLoad(Drawable placeHolderDrawable) {
    
                        }
                    });
    
    
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    sendNotification();
                }
            });
    
        }
    }
    
    private void sendNotification() {
        assert notificationManager != null;
        int m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
        notificationManager.notify(m, notificationBuilder.build());
    }
    }