В моем случае, текстовое push-уведомление Firebase работает нормально, но изображение не отображается в push-уведомлении. Пожалуйста, скажите мне, что может быть причиной, а также, пожалуйста, исправьте мой код, потому что я новичок в Android Studio.
MyFirebaseMessagingService.java
package com.iaritoppers.FirebaseService;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.iaritoppers.Config.Config;
import com.iaritoppers.Helper.NotificationHelper;
import com.iaritoppers.R;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
showNotificationWithImageLevel26 (bitmap);
else
showNotificationWithImage(bitmap);
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
@RequiresApi(api = Build.VERSION_CODES.O)
private void showNotificationWithImageLevel26(Bitmap bitmap) {
NotificationHelper helper = new NotificationHelper(getBaseContext());
Notification.Builder builder = helper.getchannel(Config.title,Config.message,bitmap);
helper.getManager().notify(0,builder.build() );
}
private void showNotificationWithImage(Bitmap bitmap) {
NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle();
style.setSummaryText(Config.message);
style.bigPicture(bitmap);
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.ic_launcher_round)
.setAutoCancel(true)
.setSound(defaultSound)
.setStyle(style);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, notificationBuilder.build());
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData() !=null)
getImage(remoteMessage);
}
private void getImage(final RemoteMessage remoteMessage) {
Config.message = remoteMessage.getNotification().getBody();
Config.title = remoteMessage.getNotification().getTitle();
if (remoteMessage.getData() != null) {
Handler uiHandler = new Handler(Looper.getMainLooper());
uiHandler.post(new Runnable() {
@Override
public void run() {
Picasso.with(getApplicationContext())
.load(remoteMessage.getData().get("image"))
.into(target);
}
});
}
}
}
MyFirebaseService.java
package com.iaritoppers.FirebaseService;
import android.util.Log;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
public class MyFirebaseService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
super.onTokenRefresh();
sendNewTokenToServer (FirebaseInstanceId.getInstance().getToken());
}
private void sendNewTokenToServer(String token) {
Log.d("EDMTTToken",token);
}
}
NotificationHelper.java
package com.iaritoppers.Helper;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.Bitmap;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import com.iaritoppers.Config.Config;
import com.iaritoppers.R;
public class NotificationHelper extends ContextWrapper {
private static final String EDMT_CHANNEL_ID = "com.iaritoppers.EDMTDev";
private static final String EDMT_CHANNEL_Name = "EDMTDev";
private NotificationManager manager;
public NotificationHelper(Context base) {
super(base);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannel();
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void createChannel() {
NotificationChannel channel = new NotificationChannel(EDMT_CHANNEL_ID,EDMT_CHANNEL_Name,NotificationManager.IMPORTANCE_DEFAULT);
channel.enableVibration(true);
channel.enableLights(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
getManager().createNotificationChannel(channel);
}
public NotificationManager getManager() {
if (manager == null)
manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
return manager;
}
@RequiresApi(api = Build.VERSION_CODES.O)
public Notification.Builder getchannel(String title, String body, Bitmap bitmap)
{
Notification.Style style = new Notification.BigPictureStyle().bigPicture(bitmap);
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
return new Notification.Builder(getApplicationContext(),EDMT_CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setAutoCancel(true)
.setSound(defaultSound)
.setStyle(style);
}
}
Config.java
package com.iaritoppers.Config;
public class Config {
public static String message="";
public static String title="";
}
Manifest.xml
<service android:name=".FirebaseService.MyFirebaseService">
<intent-filter>
<action android:name="com.google.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service android:name=".FirebaseService.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>