#java #android #android-studio #kotlin #notifications
Вопрос:
Я хочу получать значки из уведомлений, размещенных в любых приложениях на мобильных устройствах.
Для этой цели я создал службу NotificationListener и переопределил метод публикации уведомлений.
Я пробовал приведенные ниже методы, но ни один из них не работает. Пожалуйста, кто-нибудь, помогите, я новенькая и стройная. заранее спасибо.
@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());
}
}
Ответ №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()
счастливого кодирования 🙂