-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFcmService.java
More file actions
66 lines (54 loc) · 1.99 KB
/
Copy pathFcmService.java
File metadata and controls
66 lines (54 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.semosan.api.common.fcm;
import com.google.firebase.messaging.ApnsConfig;
import com.google.firebase.messaging.Aps;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingException;
import com.google.firebase.messaging.Message;
import com.google.firebase.messaging.Notification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.Map;
@Slf4j
@Service
public class FcmService {
@Value("${fcm.apns-environment:production}")
private String apnsEnvironment;
public String sendMessage(
String token,
String title,
String body,
Map<String, String> data,
boolean dataOnly
) throws FirebaseMessagingException {
Message.Builder builder = Message.builder()
.setToken(token);
if (!dataOnly) {
Notification notification = Notification.builder()
.setTitle(title)
.setBody(body)
.build();
builder.setNotification(notification);
}
builder.setApnsConfig(dataOnly ? silentPushApnsConfig() : normalPushApnsConfig());
if (data != null && !data.isEmpty()) {
builder.putAllData(data);
}
String response = FirebaseMessaging.getInstance().send(builder.build());
log.info("FCM 발송 성공: {}", response);
return response;
}
private ApnsConfig normalPushApnsConfig() {
return ApnsConfig.builder()
.putHeader("apns-environment", apnsEnvironment)
.build();
}
private ApnsConfig silentPushApnsConfig() {
return ApnsConfig.builder()
.putHeader("apns-environment", apnsEnvironment)
.setAps(Aps.builder()
.setContentAvailable(true)
.build())
.build();
}
}