图片未显示在Firebase推送通知中

时间:2018-11-03 23:45:21

标签: android firebase push-notification firebase-cloud-messaging

我正在尝试使用毕加索显示图像通知,但仅显示文本通知。由于某些原因,图像只是不想显示。我已经为此工作了几个小时,但是不知道我在做什么错。请帮忙。

这是我的MyFirebaseMessagingService.java-

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(Exception e, 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);

    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP | intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);

    NotificationCompat.Builder notificationbuilder = new NotificationCompat.Builder(getApplicationContext())
            .setSmallIcon(R.drawable.notification)
            .setContentTitle(Config.title)
            .setAutoCancel(true)
            .setSound(defaultSound)
            .setContentIntent(pendingIntent)
            .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) {
    //set message and title
    Config.message = remoteMessage.getNotification().getBody();
    Config.title = remoteMessage.getNotification().getTitle();

    //Create thread to fetch Image from notification
    if (remoteMessage.getData() != null) {
        Handler uiHandler = new Handler(Looper.getMainLooper());
        uiHandler.post(new Runnable() {
            @Override
            public void run() {
                //get image from notification data
                Picasso.get()
                        .load(remoteMessage.getData().get("image"))
                        .into(target);
            }
        });
    }
}

@Override
public void onNewToken(String s) {
    Log.d("TAGG", "Refreshed token: " + s);
}

和NotificationHelper.java-

public class NotificationHelper extends ContextWrapper {
private static final String BHARTI_CHANNEL_ID = "org.bhartinews.bhartinews";
private static final String BHARTI_CHANNEL_NAME = "BhartiNews";
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(BHARTI_CHANNEL_ID, BHARTI_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) {
    String picture = "http://www.ddbharat.com/wp-content/uploads/2018/11/1541251436.jpg";
    Notification.Style style = new Notification.BigPictureStyle().bigPicture(bitmap);

    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP | intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);

    Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    return new Notification.Builder(getApplicationContext(), BHARTI_CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_stat_name)
            .setContentTitle(Config.title)
            .setAutoCancel(true)
            .setSound(defaultSound)
            .setContentIntent(pendingIntent)
            .setStyle(style);
}

感谢您的帮助。

0 个答案:

没有答案