首页 > 解决方案 > 如何从 NotificationListener 获取大小图标

问题描述

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

为此,我创建了 NotificationListener 服务并覆盖了 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());
             
        }
}

标签: javaandroidandroid-studiokotlinnotifications

解决方案


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

我的广播接收器是:

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

快乐编码:)


推荐阅读