简体   繁体   English

如何将图片添加到通知大图标,毕加索只有这张图片的 URL?

[英]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().我在 timetable.getImage() 中得到图像的 URL。 In my project, Picasso supports get() method, not with().在我的项目中,毕加索支持 get() 方法,而不是 with()。 I am new in android, please help.我是 android 的新手,请帮忙。 You can see my notififcation on picture below.你可以在下面的图片中看到我的通知。 Icon image of notification is hardcoded.通知的图标图像是硬编码的。 But I need this icon image from timetable.getImage().但我需要 timetable.getImage() 中的这个图标图像。 This is my code:这是我的代码:

public class EpisodeNotifyActivity extends AppCompatActivity {公共 class EpisodeNotifyActivity 扩展 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());
    }    
}}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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