当我在主类中运行代码时,它可以完美运行,但是在后端,它不起作用。
没有引发异常。 没有这样的错误迹象。
我放印象以了解发生了什么,但是从不显示印象3。
java版本:1.8 服务器:glassfish 5
该代码在主类中起作用,但在后端中不起作用,我认为这是因为它在我的本地服务器(本地主机)上运行,但是它已上传到服务器并且发生了同样的事情,所以该代码不起作用并且没有显示错误
使用过的Maven依赖
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>6.4.0</version>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
生成的导入
导入com.google.firebase.FirebaseApp; 导入com.google.firebase.FirebaseOptions; 导入com.google.firebase.messaging.AndroidConfig; 导入com.google.firebase.messaging.AndroidNotification; 导入com.google.firebase.messaging.ApnsConfig; 导入com.google.firebase.messaging.Aps; 导入com.google.firebase.messaging.FirebaseMessaging; 导入com.google.firebase.messaging.Notification; 导入com.google.firebase.messaging.TopicManagementResponse; 导入com.google.firebase.messaging.WebpushConfig;
代码在主体中起作用
try {
System.out.println("1");
FileInputStream serviceAccount= new FileInputStream("/serviceAccountKey.json");
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setServiceAccountId("projectId")
.setProjectId("projectId")
.build();
FirebaseApp.initializeApp(options);
System.out.println("2");
com.google.firebase.messaging.Message message =
com.google.firebase.messaging.Message.builder()
.putData("tipo", "cargueTareasFinalizado")
.putData("nombreAmigo", "DARIOOO")
.putData("pagina", "DARIOOO")
.setToken(token)
.setNotification(new Notification(
"Dario up 1.43% on the day",
"$GOOG gained 11.80 points to close at 835.67, up
1.43% on the day."))
.setAndroidConfig(AndroidConfig.builder()
.setTtl(3600 * 1000)
.setNotification(AndroidNotification.builder()
.setTitle("titulo android")
.setBody("body android")
.setIcon("icon_color")
.setColor("#247c2e")
.build())
.build())
.setApnsConfig(ApnsConfig.builder()
.setAps(Aps.builder()
.setBadge(0)
.build())
.putCustomData("titulo", "tiiiiiiii")
.putCustomData("body", "bodddd")
.putCustomData("tipo", "cargueTareasFinalizado")
.build())
.setWebpushConfig(WebpushConfig.builder()
.putData("titulo", "Tiiiiii")
.putData("body", "bodddy")
.putData("icon", "\"images/favicon.png\"")
.build())
.build();
System.out.println("3");
String response = FirebaseMessaging.getInstance().send(message);
} catch (Exception e) {
e.printStackTrace();
}