This repository has been archived by the owner on Aug 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
NotificationSender.java
147 lines (116 loc) · 4.45 KB
/
NotificationSender.java
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package com.example;
import com.google.api.client.repackaged.com.google.common.base.Preconditions;
import com.google.auth.oauth2.AccessToken;
import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.gson.Gson;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
public class NotificationSender {
// [START send_notification_df]
final class Notification {
private final String title;
Notification(String title) {
this.title = title;
}
String getTitle() {
return title;
}
}
final class Target {
private final String userId;
private final String intent;
private final String locale;
Target(String userId, String intent, String locale) {
this.userId = userId;
this.intent = intent;
this.locale = locale;
}
String getUserId() {
return userId;
}
String getIntent() {
return intent;
}
String getLocale() {
return locale;
}
}
final class PushMessage {
private final Notification userNotification;
private final Target target;
PushMessage(Notification userNotification, Target target) {
this.userNotification = userNotification;
this.target = target;
}
Notification getUserNotification() {
return userNotification;
}
Target getTarget() {
return target;
}
}
final class PushNotification {
private final PushMessage customPushMessage;
private boolean isInSandbox;
PushNotification(PushMessage customPushMessage, boolean isInSandbox) {
this.customPushMessage = customPushMessage;
this.isInSandbox = isInSandbox;
}
PushMessage getCustomPushMessage() {
return customPushMessage;
}
boolean getIsInSandbox() {
return isInSandbox;
}
}
private PushNotification createNotification(String title, String userId, String intent, String locale) {
Notification notification = new Notification(title);
Target target = new Target(userId, intent, locale);
PushMessage message = new PushMessage(notification, target);
boolean isInSandbox = true;
return new PushNotification(message, isInSandbox);
}
private ServiceAccountCredentials loadCredentials() throws IOException {
String actionsApiServiceAccountFile =
this.getClass().getClassLoader().getResource("service-account.json").getFile();
InputStream actionsApiServiceAccount = new FileInputStream(actionsApiServiceAccountFile);
ServiceAccountCredentials serviceAccountCredentials =
ServiceAccountCredentials.fromStream(actionsApiServiceAccount);
return (ServiceAccountCredentials)
serviceAccountCredentials.createScoped(
Collections.singleton(
"https://www.googleapis.com/auth/actions.fulfillment.conversation"));
}
private String getAccessToken() throws IOException {
AccessToken token = loadCredentials().refreshAccessToken();
return token.getTokenValue();
}
public void sendNotification(String title, String userId, String intent, String locale) throws IOException {
Preconditions.checkNotNull(title, "title cannot be null.");
Preconditions.checkNotNull(userId, "userId cannot be null.");
Preconditions.checkNotNull(intent, "intent cannot be null.");
Preconditions.checkNotNull(locale, "locale cannot be null");
PushNotification notification = createNotification(title, userId, intent, locale);
HttpPost request = new HttpPost("https://actions.googleapis.com/v2/conversations:send");
String token = getAccessToken();
request.setHeader("Content-type", "application/json");
request.setHeader("Authorization", "Bearer " + token);
StringEntity entity = new StringEntity(new Gson().toJson(notification));
entity.setContentType(ContentType.APPLICATION_JSON.getMimeType());
request.setEntity(entity);
HttpClient httpClient = HttpClientBuilder.create().build();
httpClient.execute(request);
}
// [END send_notification_df]
public static void main(String[] args) throws IOException {
NotificationSender notificationSender = new NotificationSender();
notificationSender.sendNotification("Push Notification Title", "<UPDATES_USER_ID>", "Notification Intent", "en-US");
}
}