Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何解决从firebase云消息发送附加数据时firebase推送通知中未显示图像的问题?_Java_Android_Firebase_Push Notification_Firebase Cloud Messaging - Fatal编程技术网

Java 如何解决从firebase云消息发送附加数据时firebase推送通知中未显示图像的问题?

Java 如何解决从firebase云消息发送附加数据时firebase推送通知中未显示图像的问题?,java,android,firebase,push-notification,firebase-cloud-messaging,Java,Android,Firebase,Push Notification,Firebase Cloud Messaging,在我的Android项目中,我正在使用firebase云消息服务。我已经设置了发送通知的特定主题和通知通道。为了将用户从推送通知发送到特定屏幕,我使用了其他数据,如下图所示- 为了发送大图像,我首先将图像上传到firebase存储中,然后将链接复制到通知图像字段 做了以上所有的事情,当我发送带有图像的推送通知而不添加任何附加数据时,图像与推送通知配合得很好。但是,当我在推送通知中使用附加数据时,图像不会出现 这是整个班级的代码- MyFirebaseMessagingService.java

在我的Android项目中,我正在使用firebase云消息服务。我已经设置了发送通知的特定主题和通知通道。为了将用户从推送通知发送到特定屏幕,我使用了其他数据,如下图所示-

为了发送大图像,我首先将图像上传到firebase存储中,然后将链接复制到通知图像字段

做了以上所有的事情,当我发送带有图像的推送通知而不添加任何附加数据时,图像与推送通知配合得很好。但是,当我在推送通知中使用附加数据时,图像不会出现

这是整个班级的代码-

MyFirebaseMessagingService.java

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";
    String groupKey;
    String groupId;
    Intent intent;
    Bitmap bitmap2;
    String title;
    String message;
    NotificationCompat.Builder notificationBuilder;

    Target target = new Target() {
        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
           bitmap2  = bitmap;
        }

        @Override
        public void onBitmapFailed(Exception e, Drawable errorDrawable) {
        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {
        }
    };

    @Override
    public void handleIntent(Intent intent) {
        try {
            if (intent.getExtras() != null) {
                RemoteMessage.Builder builder = new RemoteMessage.Builder("MyFirebaseMessagingService");

                for (String key : intent.getExtras().keySet()) {
                    builder.addData(key, intent.getExtras().get(key).toString());
                }

                onMessageReceived(builder.build());
            } else {
                super.handleIntent(intent);
            }
        } catch (Exception e) {
            super.handleIntent(intent);
        }
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        ForegroundCheckTask foregroundCheckTask = new ForegroundCheckTask();
        boolean isforeground = foregroundCheckTask.isAppOnForeground(getApplicationContext());

        if(remoteMessage.getData()!=null) {
            getImage(remoteMessage);
        }

        if (isforeground != true) {

            if (remoteMessage.getData().size() > 0) {
                try {
                    for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet()) {
                        groupKey = entry.getKey();
                        groupId = entry.getValue();

                        String key = entry.getKey();
                        String value = entry.getValue();
                    }

                     title = remoteMessage.getNotification().getTitle(); //get title
                     message = remoteMessage.getNotification().getBody();

                    if (true) {
                        scheduleJob();
                    } else {
                        handleNow();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            // Check if message contains a notification payload.
            if (remoteMessage.getNotification() != null) {
                 title = remoteMessage.getNotification().getTitle(); //get title
                 message = remoteMessage.getNotification().getBody(); //get message
                String click_action = remoteMessage.getNotification().getClickAction(); //get click_action
                String click_action2 = remoteMessage.getNotification().getChannelId(); //get click_action
                String id = remoteMessage.getData().get("book_details"); //get click_action

                for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet()) {
                    groupKey = entry.getKey();
                    groupId = entry.getValue();

                    String key = entry.getKey();
                    String value = entry.getValue();
                }

            }

            sendNotification(title, message, groupKey, groupId);

        } else {

            try{

                Intent intent = new Intent(this, HomeActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
                String channelId = "Default";
                NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle(remoteMessage.getNotification().getTitle())
                        .setContentText(remoteMessage.getNotification().getBody()).setAutoCancel(true).setContentIntent(pendingIntent);
                ;
                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    NotificationChannel channel = new NotificationChannel(channelId, "abcd", NotificationManager.IMPORTANCE_DEFAULT);
                    manager.createNotificationChannel(channel);
                }

                int notificationId = new Random().nextInt(60000);
                manager.notify(notificationId, builder.build());

            } catch (Exception e){

            }

        }

    }

    @Override
    public void onNewToken(String token) {
        sendRegistrationToServer(token);
    }

    private void handleNow() {
    }

    private void sendRegistrationToServer(String token) {
    }

    private void sendNotification(String title, String messageBody, String groupKey, String id) {

        NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle();
        style.bigPicture(bitmap2);

        if (id.equals("HOMEACTIVITY")) {
            intent = new Intent(this, HomeActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        } else {
            intent = new Intent(this, HomeActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        }

        if (id != null && groupKey != null) {
            if (!id.equals("") && !groupKey.equals("")) {
                goToSpecificScreen(groupKey, id);
            }
        }

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        String channelId = getString(R.string.default_notification_channel_id);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        if(bitmap2 != null) {
            notificationBuilder =
                    new NotificationCompat.Builder(this, channelId)
                            .setSmallIcon(R.drawable.ic_stat_onesignal_default)
                            .setContentTitle(title)
                            .setContentText(messageBody)
                            .setAutoCancel(true)
                            .setSound(defaultSoundUri)
                            .setStyle(style)
                            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_stat_onesignal_default))
                            .setLights(Color.parseColor("#ffffffff"), 5000, 5000)
                            .setContentIntent(pendingIntent);
        } else {
            notificationBuilder =
                    new NotificationCompat.Builder(this, channelId)
                            .setSmallIcon(R.drawable.ic_stat_onesignal_default)
                            .setContentTitle(title)
                            .setContentText(messageBody)
                            .setAutoCancel(true)
                            .setSound(defaultSoundUri)
                            .setStyle(new NotificationCompat.BigTextStyle()
                                    .bigText(messageBody))
                            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_stat_onesignal_default))
                            .setLights(Color.parseColor("#ffffffff"), 5000, 5000)
                            .setContentIntent(pendingIntent);
        }


        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "abcd",
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }

        int notificationId = new Random().nextInt(60000);
        notificationManager.notify(notificationId, notificationBuilder.build());
    }

    private void goToSpecificScreen(String notifyKey, String notifyMsg) {

        if (notifyKey.equals("details")) {
            intent = new Intent(this, HomeActivity.class);
            intent.putExtra("product_id", notifyMsg);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        } else if (notifyKey.equals("Offer zone")) {
            intent = new Intent(this, AuthorDetailsNewActivity.class);
            intent.putExtra("categoryId", "1");
            intent.putExtra("filter_type", "category");
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        } 

    }

    private void getImage(final RemoteMessage remoteMessage) {
        Map<String, String> data = remoteMessage.getData();
        try {

            Uri uri = remoteMessage.getNotification().getImageUrl();
            if(remoteMessage.getData()!=null){

                Handler uiHandler = new Handler(Looper.getMainLooper());
                uiHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        Picasso.get().load(uri).into(target);
                    }
                }) ;
            }
        } catch (Exception e) {

      }

    }

}
公共类MyFirebaseMessagingService扩展了FirebaseMessagingService{
私有静态最终字符串TAG=“MyFirebaseMsgService”;
字符串组键;
字符串组ID;
意图;
位图位图位图2;
字符串标题;
字符串消息;
NotificationCompat.Builder notificationBuilder;
目标=新目标(){
@凌驾
已加载位图(位图位图,Picasso.LoadedFrom){
位图2=位图;
}
@凌驾
public void onBitmapFailed(异常e,Drawable errorDrawable){
}
@凌驾
准备加载时的公共无效(可提取占位符可提取){
}
};
@凌驾
公共无效手册内容(意图){
试一试{
if(intent.getExtras()!=null){
RemoteMessage.Builder=新的RemoteMessage.Builder(“MyFirebaseMessagingService”);
for(字符串键:intent.getExtras().keySet()){
builder.addData(key,intent.getExtras().get(key.toString());
}
onMessageReceived(builder.build());
}否则{
超级手册内容(意图);
}
}捕获(例外e){
超级手册内容(意图);
}
}
@凌驾
收到消息时公共无效(RemoteMessage RemoteMessage){
ForegroundCheckTask ForegroundCheckTask=新建ForegroundCheckTask();
布尔值isforeground=foregroundCheckTask.isAppOnForeground(getApplicationContext());
if(remoteMessage.getData()!=null){
getImage(远程消息);
}
if(is前台!=真){
如果(remoteMessage.getData().size()>0){
试一试{
对于(Map.Entry:remoteMessage.getData().entrySet()){
groupKey=entry.getKey();
groupId=entry.getValue();
String key=entry.getKey();
字符串值=entry.getValue();
}
title=remoteMessage.getNotification().getTitle();//获取标题
message=remoteMessage.getNotification().getBody();
如果(真){
scheduleJob();
}否则{
handleNow();
}
}捕获(例外e){
e、 printStackTrace();
}
}
//检查消息是否包含通知负载。
if(remoteMessage.getNotification()!=null){
title=remoteMessage.getNotification().getTitle();//获取标题
message=remoteMessage.getNotification().getBody();//获取消息
String click_action=remoteMessage.getNotification().getClickAction();//获取click_action
String click_action 2=remoteMessage.getNotification().getChannelId();//获取click_操作
String id=remoteMessage.getData().get(“book_details”);//获取单击操作
对于(Map.Entry:remoteMessage.getData().entrySet()){
groupKey=entry.getKey();
groupId=entry.getValue();
String key=entry.getKey();
字符串值=entry.getValue();
}
}
sendNotification(标题、消息、组密钥、组ID);
}否则{
试一试{
意向意向=新意向(此,HomeActivity.class);
intent.addFlags(intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
PendingEvent PendingEvent=PendingEvent.getActivity(this,0,intent,PendingEvent.FLAG_ONE_SHOT);
字符串channelId=“默认”;
NotificationCompat.Builder=新建NotificationCompat.Builder(此,channelId)
.setSmallIcon(R.mipmap.ic_启动器)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody()).setAutoCancel(true).setContentIntent(PendingContent);
;
NotificationManager=(NotificationManager)getSystemService(通知服务);
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.O){
NotificationChannel=新建NotificationChannel(channelId,“abcd”,NotificationManager.IMPORTANCE\u默认值);
manager.createNotificationChannel(频道);
}
int notificationId=new Random().nextInt(60000);
manager.notify(notificationId,builder.build());
}捕获(例外e){
}
}
}
@凌驾
公共void onNewToken(字符串标记){
sendRegistrationToServer(令牌);
}
私有无效handleNow(){
}
私有void sendRegistrationToServer(字符串令牌){
}
私有void sendNotification(字符串标题、字符串messageBody、字符串groupKey、字符串id){
知音。