-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
181 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/com/polzzak/domain/notification/dto/FcmMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.polzzak.domain.notification.dto; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor | ||
@Getter | ||
public class FcmMessage { | ||
|
||
private boolean validateOnly; | ||
private Message message; | ||
private String to; | ||
|
||
public FcmMessage(boolean validateOnly, Message message, String to) { | ||
this.validateOnly = validateOnly; | ||
this.message = message; | ||
this.to = to; | ||
} | ||
|
||
@NoArgsConstructor | ||
@Getter | ||
public static class Message { | ||
private Notification notification; | ||
private String token; | ||
|
||
public Message(Notification notification, String token) { | ||
this.notification = notification; | ||
this.token = token; | ||
} | ||
} | ||
|
||
@NoArgsConstructor | ||
@Getter | ||
public static class Notification { | ||
private String title; | ||
private String body; | ||
private String image; | ||
|
||
public Notification(String title, String body, String image) { | ||
this.title = title; | ||
this.body = body; | ||
this.image = image; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/main/java/com/polzzak/domain/pushtoken/repository/PushTokenRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
package com.polzzak.domain.pushtoken.repository; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import com.polzzak.domain.pushtoken.model.PushToken; | ||
import com.polzzak.domain.user.entity.Member; | ||
|
||
public interface PushTokenRepository extends JpaRepository<PushToken, Long> { | ||
|
||
List<PushToken> getPushTokensByMember(Member member); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/main/java/com/polzzak/global/infra/firebase/FirebaseCloudMessageService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.polzzak.global.infra.firebase; | ||
|
||
import java.io.FileInputStream; | ||
import java.util.List; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.firebase.FirebaseApp; | ||
import com.google.firebase.FirebaseOptions; | ||
import com.google.firebase.messaging.BatchResponse; | ||
import com.google.firebase.messaging.FirebaseMessaging; | ||
import com.google.firebase.messaging.FirebaseMessagingException; | ||
import com.google.firebase.messaging.MulticastMessage; | ||
import com.google.firebase.messaging.Notification; | ||
import com.polzzak.domain.pushtoken.model.PushToken; | ||
import com.polzzak.domain.pushtoken.service.PushTokenService; | ||
import com.polzzak.domain.user.entity.Member; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class FirebaseCloudMessageService { | ||
|
||
private final PushTokenService pushTokenService; | ||
|
||
public void sendPushNotification(Member member, String title, String body, String link) { | ||
try { | ||
FileInputStream serviceAccount = new FileInputStream( | ||
"src/main/resources/firebase/firebase_service_key.json"); | ||
|
||
FirebaseOptions options = new FirebaseOptions.Builder() | ||
.setCredentials(GoogleCredentials.fromStream(serviceAccount)) | ||
.build(); | ||
|
||
if (FirebaseApp.getApps().isEmpty()) { | ||
FirebaseApp.initializeApp(options); | ||
} | ||
|
||
List<String> registrationTokens = pushTokenService.getPushTokens(member).stream() | ||
.map(PushToken::getToken) | ||
.toList(); | ||
|
||
MulticastMessage message = MulticastMessage.builder() | ||
.setNotification(Notification.builder() | ||
.setTitle(title) | ||
.setBody(body) | ||
.build()) | ||
.addAllTokens(registrationTokens) | ||
.putData("link", link) | ||
.build(); | ||
BatchResponse response = null; | ||
try { | ||
response = FirebaseMessaging.getInstance().sendEachForMulticast(message); | ||
} catch (FirebaseMessagingException e) { | ||
e.printStackTrace(); | ||
} | ||
// See the BatchResponse reference documentation | ||
// for the contents of response. | ||
System.out.println(response.getSuccessCount() + " messages were sent successfully"); | ||
System.out.println(response); | ||
|
||
} catch (Exception e) { | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"type": "service_account", | ||
"project_id": "polzzak-57648", | ||
"private_key_id": "ead4311d449ac5facc6c507ec152a6d9f54a279d", | ||
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCuffOTpdr5Z23/\nn6c37jA+TkjDj8MZxBr67ovNVgKSUchidJe3A8VYS+Ck6tMaMr7/YPUu6OfkkHOS\n0VlW3UQwYRqj5Rk5kH6ByBxVH9ZI7Sh0FO9pH71d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xbu1HlfOivQkN7EhgnVyG1jbAfnnNaZk/8P4AG07+QiymsMcEDiQKBgAyx\ntXrnlQZiAhDdGrxJNDVg1N0AldL8vYzPSkT3tAD0zNUJ+VyKCrSVeDWcWEJsGEDk\nbfQq6KJzPeqlJQmMm4rmVQIPKF3pQTRKLVSwJamcZTnuDXT9LWk11CMswbCjdK5G\nRuDq9ZvPYxGvFC9jdwbmhZ6VdWWNIFxcWJgYrXGpAoGBAOwyu9ycczHwhaiITVnH\nDmG7S2LCj8Jq3u3351c6dv/Lt8wuZ6u2FmTuMh8id9hD7u2TO9QzIF8+S10Rc/J8\nzb9GI3KLg67bCCrReZQ0B1L2cjKUObpx5om2icxYXTV1k3GyVg7RJwQ5NA8ncQSP\nvXysN6nV8rcLOSuK8iGsS9Je\n-----END PRIVATE KEY-----\n", | ||
"client_email": "[email protected]", | ||
"client_id": "101961288027838881996", | ||
"auth_uri": "https://accounts.google.com/o/oauth2/auth", | ||
"token_uri": "https://oauth2.googleapis.com/token", | ||
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", | ||
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-mzmu4%40polzzak-57648.iam.gserviceaccount.com", | ||
"universe_domain": "googleapis.com" | ||
} |