如何通过仅具有此图片URL的毕加索将图片添加到通知大图标中?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何通过仅具有此图片URL的毕加索将图片添加到通知大图标中?相关的知识,希望对你有一定的参考价值。

如何将大图标设置为通知?我在timetable.getImage()中获取图像的URL。在我的项目中,毕加索支持get()方法,而不支持with()。我是android新手,请帮忙。您可以在下面的图片中看到我的通知。通知的图标图像已硬编码。但是我需要来自timetable.getImage()的图标图像。这是我的代码:

公共类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);

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

以上是关于如何通过仅具有此图片URL的毕加索将图片添加到通知大图标中?的主要内容,如果未能解决你的问题,请参考以下文章

将图片从 URL 上传到 Firebase 存储

Picasso(毕加索)加载圆形图片圆角图片

Picasso(毕加索)加载圆形图片圆角图片

在 Froala 编辑器中上传图片时如何替换图片 URL?

如何通过api获取rct-image-store的Image URL上传图片

毕加索没有在我的 ImageView 中显示图片我该怎么办?