【问题标题】:How to get small and large Icons from NotificationListener如何从 NotificationListener 获取大小图标
【发布时间】:2021-06-23 13:43:20
【问题描述】:

我想从发布的任何移动应用程序的通知中获取图标。

为此,我创建了 NotificationListener Service 并覆盖了 on notification Posted 方法。

我尝试了以下方法,但都没有奏效。请有人帮助我是新的和更瘦的。提前致谢。

@Override
public void onNotificationPosted(StatusBarNotification sbn){
        Intent intent = new  Intent("com.example.notify");
        intent.putExtra("Notification sbn", sbn);
        sendBroadcast(intent);
  
}


    public class ImageChangeBroadcastReceiver extends BroadcastReceiver {
            @Override
            public void onReceive(Context context, Intent intent) {
                StatusBarNotification sbn = intent.getParcelableExtra("Notification sbn");
                Bitmap bitmap = (Bitmap) sbn.getNotification().extras.get(Notification.EXTRA_LARGE_ICON);
                
                    image1.setImageBitmap(bitmap);
                     image2.setImageBitmap(sbn.getNotification().largeIcon);
                Notification notification = sbn.getNotification();
                notification.getLargeIcon();
                image3.setImageIcon(notification.getLargeIcon());
             
        }
}

【问题讨论】:

    标签: java android android-studio kotlin notifications


    【解决方案1】:

    在我的情况下,我使用自定义图像女巫我进入服务器并以大视图或任何大小的需要显示。

    我的广播接收器是:

    public class CustomNotificationBroadcast extends BroadcastReceiver {
    
    private   String channelID = "1";
    private   String channelName = "news";
    private   String channelDesc = "news description";
    
    @Override
    public void onReceive(final Context context, final Intent intent) {
        mContext = context;
    
    
        Bundle extra = intent.getExtras();
        if (extra != null) {
            getNotification(modelNotification, null);
    
        }
    
    }
    
    public void getNotification(ModelNotification remoteMessage, Class<?> action_click) {
         Notification notification;
         NotificationManager notificationManager;
    
        notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Log.e(TAG, "getNotification   Notif Type:  if ");
    
             channelID = "1";
             channelName = "news";
             channelDesc = "news description";
            NotificationChannel notificationChannel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);
            notificationChannel.setDescription(channelDesc);
            notificationChannel.enableLights(true);
            notificationChannel.setSound(null, null);
            notificationChannel.setLightColor(Color.GREEN);
            notificationManager.createNotificationChannel(notificationChannel);
    
        }
    
            RingtoneManager.getRingtone(mContext,
                    RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)).play();
    
        NotificationCompat.Builder   builder = new NotificationCompat.Builder(mContext, channelID)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(remoteMessage.getNotifTitle())
                    .setContentText(remoteMessage.getNotifBody())
                    .setVibrate(new long[]{100, 500, 500, 500, 500})
                    .setAutoCancel(true)
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher))
                    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
        if (remoteMessage.getDataImage() != null) {
            Bitmap bitmap = getBitmapfromUrl(remoteMessage.getDataImage());
            builder.setStyle(
                    new NotificationCompat.BigPictureStyle()
                            .bigPicture(bitmap)
                            .bigLargeIcon(null)
            ).setLargeIcon(bitmap);
        }
    
    
        notification = builder.build();
            notificationManager.notify(notfiId,  notification);
    
      
    
    }
    
    
    public Bitmap getBitmapfromUrl(String imageUrl) {
        try {
            URL url = new URL(imageUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            return BitmapFactory.decodeStream(input);
    
          } catch (Exception e) {
            Log.e("awesome", "Error in getting notification image: " + 
          e.getLocalizedMessage());
            return null;
          }
        }
    

    我用这个方法处理图片getBitmapfromUrl()

    编码愉快 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-18
      • 1970-01-01
      • 2010-09-12
      • 1970-01-01
      • 1970-01-01
      • 2019-03-31
      • 2020-03-14
      • 2011-12-15
      相关资源
      最近更新 更多