BrightSide Workbench Full Report + Source Code
PushMessage.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.push;
20 
21 import java.io.StringWriter;
22 import javax.json.Json;
23 import javax.json.stream.JsonGenerator;
24 import org.turro.string.Strings;
25 import org.turro.elephant.context.ElephantContext;
26 
31 public class PushMessage {
32 
33  private final StringWriter writer;
34  private final JsonGenerator json;
35 
36  private String tag, primaryKey, icon, badge;
37 
38  public PushMessage message(String message) {
39  json.write("message", Strings.truncate(message, 240));
40  return this;
41  }
42 
43  public PushMessage click(String target) {
44  json.write("clickTarget", target);
45  return this;
46  }
47 
48  public PushMessage tag(String tag) {
49  this.tag = tag;
50  return this;
51  }
52 
53  public PushMessage primaryKey(String primaryKey) {
54  this.primaryKey = primaryKey;
55  return this;
56  }
57 
58  public PushMessage icon(String icon) {
59  this.icon = icon;
60  return this;
61  }
62 
63  public PushMessage badge(String badge) {
64  this.badge = badge;
65  return this;
66  }
67 
68  public String json() {
69  String imageFolder = ElephantContext.getRootWebPath() + "/_internal/repository/push/";
70  json.write("tag", tag)
71  .write("primaryKey", primaryKey)
72  .write("icon", imageFolder + icon)
73  .write("badge", imageFolder + badge)
74  .writeEnd()
75  .close();
76  return writer.toString();
77  }
78 
79  public static PushMessage title(String title) {
80  return new PushMessage(title);
81  }
82 
83  private PushMessage(String title) {
84  tag = "elephant";
85  primaryKey = "1";
86  icon = "iconPush.png";
87  badge = "badgePush.png";
88  writer = new StringWriter();
89  json = Json.createGenerator(writer);
90  json.writeStartObject().write("title", Strings.truncate(title, 65));
91  }
92 
93 }
PushMessage icon(String icon)
static PushMessage title(String title)
PushMessage tag(String tag)
PushMessage click(String target)
PushMessage badge(String badge)
PushMessage message(String message)
PushMessage primaryKey(String primaryKey)