Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.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 带有Spring引导服务器通知()的FCM消息传递构造函数问题_Java_Android_Firebase_Firebase Cloud Messaging - Fatal编程技术网

Java 带有Spring引导服务器通知()的FCM消息传递构造函数问题

Java 带有Spring引导服务器通知()的FCM消息传递构造函数问题,java,android,firebase,firebase-cloud-messaging,Java,Android,Firebase,Firebase Cloud Messaging,我正在尝试使用Firebase SDK向基于令牌的设备发送推送通知。 我已经在Spring Boot中实现了服务器。我可以用下面的信息向我的设备发送信息 有效载荷 { "token":"eXn97K74SoavdyblW3-8cFQDgLFzVhLCIDugdvmofbIzSmwY4ty3MwuTdFQ48sIQU9QSdLKC1BWE6NeL8zYS.....", "notification":{ "title":"Portugal vs. Denmark",

我正在尝试使用Firebase SDK向基于令牌的设备发送推送通知。 我已经在Spring Boot中实现了服务器。我可以用下面的信息向我的设备发送信息 有效载荷

{
    "token":"eXn97K74SoavdyblW3-8cFQDgLFzVhLCIDugdvmofbIzSmwY4ty3MwuTdFQ48sIQU9QSdLKC1BWE6NeL8zYS.....",
    "notification":{
      "title":"Portugal vs. Denmark",
      "body":"great match!"
    },
    "data" : {
      "body" : "Mario",
      "title" : "PortugalVSDenmark",
      "click_action":"test"
    }
  }
但若我将click_操作字段添加到通知构造函数中,那个么我在服务器中的代码只会这样说 允许使用标题和正文字段。然而令人惊讶的是,在Android Studio中,its显示了一个名为getNotification()点getClickAction()的方法,所以我这里的问题是如何使这个click\u action属性适合我的服务器逻辑。任何建议都会有帮助

here is my Service class in Spring boot

package com.example.demo.service;

import com.example.demo.dto.NotificationPlusDataDTO;
import com.example.demo.dto.NotificationRequestDto;
import com.example.demo.dto.SubscriptionRequestDto;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.messaging.*;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.IOException;


@Slf4j
@Service
public class NotificationService {
    @Value("${app.firebase-config}")
    private String firebaseConfig;
    private FirebaseApp firebaseApp;
    @PostConstruct
    private void initialize() {
        try {
            FirebaseOptions options = new FirebaseOptions.Builder()
                    .setCredentials(GoogleCredentials.fromStream(new ClassPathResource(firebaseConfig).getInputStream())).build();

            if (FirebaseApp.getApps().isEmpty()) {
                this.firebaseApp = FirebaseApp.initializeApp(options);
            } else {
                this.firebaseApp = FirebaseApp.getInstance();
            }
        } catch (IOException e) {
            System.out.println( e+" Some Error ");
        }
    }


    public String sendPnsToDevice(NotificationRequestDto notificationRequestDto) {
        Message message = Message.builder()
                .setToken(notificationRequestDto.getTarget())
                .setNotification(new Notification(notificationRequestDto.getTitle(), notificationRequestDto.getBody()))
                .putData("content", notificationRequestDto.getTitle())
                .putData("body", notificationRequestDto.getBody())
                .build();
        String response = null;
        try {
            response = FirebaseMessaging.getInstance().send(message);
        } catch (FirebaseMessagingException e) {
            System.out.println("Fail to send firebase notification"+ e);
        }
        return response;
    }


    public String sendDataPlusNotification(NotificationPlusDataDTO notificationPlusDataDTO){
        Message message= Message.builder()
                .setToken(notificationPlusDataDTO.getToken())
                .setNotification(new com.example.demo.dto.Notification(notificationPlusDataDTO.getNotification().getTitle(), notificationPlusDataDTO.getData().getBody()))
                .putData("title",notificationPlusDataDTO.getData().getTitle())
                .putData("body",notificationPlusDataDTO.getData().getBody())
                .putData("click_action",notificationPlusDataDTO.getData().getClick_action())
                .build();
        String response = null;
        try {
            response = FirebaseMessaging.getInstance().send(message);
        } catch (FirebaseMessagingException e) {
            System.out.println("Fail to send firebase notification"+ e);
        }
        return response;


}}

当您提出请求时,只需将其放入数据:

"data": {
        "key1": "Value 1",
        "key2": "Value 2",
        "key3": "Value 3",
        "key4": "Value 4",
        "click_action": "FLUTTER_NOTIFICATION_CLICK"
    }

我想发送这样的东西。{“token”:“eXn97K74SoavdyblW3 awo:APA91BHPPVGNIA8CFQDGLFZVHLCIDUGDVMOYHZYD68ZNHAZAK4L8K9MKHDK4QHNZMRFIBUNQDSDxeikdKJpZIUSUMIUBILFBIZMWY4WUTDFQ48SIQU9QSDLKC1BWE6NEL8ZYS”,“通知”:“标题”:“葡萄牙对丹麦”,“正文”:“伟大的比赛!”,“点击动作”:“测试”},“数据”:“正文”:“马里奥”,“title”:“PortugalVSDenmark”,“click_action”:“test”}}您找到解决方案了吗?