通知未显示大图和图标未显示

时间:2018-11-10 19:04:43

标签: android push-notification

showNotificationWithImage(bitmap)

我收到通知,但图像未显示 虽然助手工作正常,但为什么其他人没有得到我在api 23中测试过的图像图标却没有显示,所以任何人都可以得到空白图像

我已经发布了带有导入代码的代码,我在做什么错,这正在影响这个问题

 package com.blipclap.creativegraphy.FirebaseService;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;

import com.blipclap.creativegraphy.Common.Common;
import com.blipclap.creativegraphy.Helper.NotificationHelper;
import com.blipclap.creativegraphy.HomeActivity;
import com.blipclap.creativegraphy.R;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;

import java.util.Objects;
import java.util.Random;

public class mFirebaseInstanceService extends FirebaseMessagingService {
    @Override
    public void onNewToken(String s) {
        super.onNewToken(s);
    }

    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(Common.title,Common.message,bitmap);
        helper.getManager().notify(0,(builder.build()));
    }

    private void showNotificationWithImage(Bitmap bitmap) {
        String NOTIFICATION_CHANNEL_ID = "com.blipclap.creativegraphy.PNCG";
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification",
                    NotificationManager.IMPORTANCE_DEFAULT);

            notificationChannel.setDescription("CreativeGraphy");
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.BLUE);
            notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
            notificationChannel.enableLights(true);
            Objects.requireNonNull(notificationManager).createNotificationChannel(notificationChannel);
        }

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

        Intent intent =new Intent(getApplicationContext(),HomeActivity.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(this, NOTIFICATION_CHANNEL_ID);
        NotificationCompat.BigPictureStyle style =new NotificationCompat.BigPictureStyle();
        notificationBuilder.setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSound(defaultSound)
                .setSmallIcon(R.drawable.creativegraphy)
                .setLargeIcon(bitmap)
                .setStyle(style.bigPicture(bitmap).bigLargeIcon(null))
                .setContentTitle(Common.title)
                .setContentText(Common.message)
                .setContentInfo("Info")
                .setContentIntent(pendingIntent);

        if (notificationManager != null) {
            notificationManager.notify(new Random().nextInt(), notificationBuilder.build());
        }

    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        if (!remoteMessage.getData().isEmpty())
            getImage(remoteMessage);
    }

    private void getImage(RemoteMessage remoteMessage) {
        Common.message=Objects.requireNonNull(remoteMessage.getNotification()).getBody();
        Common.title=remoteMessage.getNotification().getTitle();

        if (!remoteMessage.getData().isEmpty()){
            Common.imageLink =remoteMessage.getData().get("image");
            Handler handler=new Handler(Looper.getMainLooper());
            handler.post(() -> Picasso.get()
                    .load(remoteMessage.getData().get("image"))
                    .into(target));
        }
    }
}

此辅助程序类的图像很好,显示在api 26上方

package com.blipclap.creativegraphy.Helper;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.support.annotation.RequiresApi;

import com.blipclap.creativegraphy.Common.Common;
import com.blipclap.creativegraphy.HomeActivity;
import com.blipclap.creativegraphy.R;

public class NotificationHelper extends ContextWrapper {
    private static final String CreativeGraphy_CHANNEL_ID = "com.blipclap.creativegraphy.PNCG";
    private static final String CreativeGraphy_CHANNEL_Name = "PNCG";
    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(CreativeGraphy_CHANNEL_ID, CreativeGraphy_CHANNEL_Name, NotificationManager.IMPORTANCE_DEFAULT);
        channel.enableLights(true);
        channel.enableVibration(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).bigLargeIcon((Bitmap) null);

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

    return new Notification.Builder(this, CreativeGraphy_CHANNEL_ID)
            .setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.creativegraphy)
            .setContentTitle(Common.title)
            .setContentText(Common.message)
            .setLargeIcon(bitmap)
            .setStyle(style)
            .setContentInfo("Info")
            .setContentIntent(pendingIntent);
}
}

1 个答案:

答案 0 :(得分:0)

调整毕加索的大小,它将开始正常工作

Picasso.get()
                .load(remoteMessage.getData().get("image"))
                .resize(500,500)
                .centerCrop()
                .memoryPolicy(MemoryPolicy.NO_CACHE)
                .networkPolicy(NetworkPolicy.NO_CACHE)
                .into(target));