【问题标题】:How can I add picture to notification large icon with Picasso having only URL of this picture?如何将图片添加到通知大图标,毕加索只有这张图片的 URL?
【发布时间】:2020-09-01 12:21:21
【问题描述】:

如何将大图标设置为通知?我在 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());
    }    
}}

【问题讨论】:

    标签: java android android-notifications picasso android-bitmap


    【解决方案1】:
    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());
    }
    }
    

    【讨论】:

    • 不,我需要使用 set.largeIcon() 方法将其加载到通知中。而且我不知道如何从 Picasso 获取位图,因为 set.largeIcon() 需要位图
    • 我的回答就是这样。如果您查看毕加索代码中onBitmapLoaded() 方法中的代码notificationBuilder.setLargeIcon(bitmap);。实际上,我制作了这段代码以完全匹配您想要的。您甚至可以复制/粘贴整个 EpisodeNotifyActivity 类,它应该可以工作。您想要实现的目标(图片来自互联网到 Picasso 的通知中)我测试成功,而不是更改代码以匹配您的。
    • 你有没有向下滚动我的答案?
    • 哦,对不起,我明白了。是的,上次我看到你用我的手机接听电话,但没有看到所有内容。谢谢!
    猜你喜欢
    • 2015-11-16
    • 2017-04-23
    • 1970-01-01
    • 2018-07-29
    • 1970-01-01
    • 2017-07-23
    • 2015-08-24
    • 1970-01-01
    • 2018-09-07
    相关资源
    最近更新 更多