Skip to content

Commit cbe806e

Browse files
committed
Add native Notification API (Alternative for FCM Client Notification API)
1 parent 48103e0 commit cbe806e

File tree

3 files changed

+228
-0
lines changed

3 files changed

+228
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package gwt.material.design.client.pwa.push.js;
2+
3+
import gwt.material.design.jquery.client.api.Functions;
4+
import gwt.material.design.jquery.client.api.Promise;
5+
import jsinterop.annotations.*;
6+
7+
/**
8+
* The Notification interface of the Notifications API is used to configure and display desktop notifications to the user.
9+
* This is an alternative if you are not using external client notification libraries like FCM (FireBaseCloudMessaging).
10+
* See all the notification display options at {@link NotificationOptions}.
11+
* <pre>
12+
* // Will request a notification
13+
* Notification.requestPermission(status -> MaterialToast.fireToast("Status: " + status));
14+
*
15+
* if (Notification.getPermission().equals("granted")) {
16+
* NotificationOptions options = new NotificationOptions();
17+
* options.body = "I love GMD";
18+
* options.icon = "https://user.oc-static.com/upload/2017/05/03/14938342186053_01-duration-and-easing.png";
19+
* new Notification("GMD Says", options);
20+
* } else {
21+
* MaterialToast.fireToast("Permission Denied. Update it thru the browser setting");
22+
* }
23+
* </pre>
24+
*
25+
* @author kevzlou7979
26+
*/
27+
@JsType(isNative = true, namespace = JsPackage.GLOBAL)
28+
public class Notification {
29+
30+
@JsConstructor
31+
public Notification(String message) {
32+
}
33+
34+
@JsConstructor
35+
public Notification(String message, NotificationOptions notification) {
36+
}
37+
38+
@JsProperty
39+
private static String permission;
40+
41+
@JsProperty
42+
private Functions.Func1<Object> onclick;
43+
44+
@JsProperty
45+
private Functions.Func1<Object> onclose;
46+
47+
@JsProperty
48+
private Functions.Func1<Object> onerror;
49+
50+
@JsProperty
51+
private Functions.Func1<Object> onshow;
52+
53+
/**
54+
* A string representing the current permission to display notifications.
55+
* <ul>
56+
* <li>denied — The user refuses to have notifications displayed.</li>
57+
* <li>granted — The user accepts having notifications displayed.</li>
58+
* <li>default — The user choice is unknown and therefore the browser will act as if the value were denied.</li>
59+
* </ul>
60+
*/
61+
@JsOverlay
62+
public static final String getPermission() {
63+
return permission;
64+
}
65+
66+
/**
67+
* A handler for the click event. It is triggered each time the user clicks on the notification.
68+
*/
69+
@JsOverlay
70+
public final void setOnclick(Functions.Func1<Object> onclick) {
71+
this.onclick = onclick;
72+
}
73+
74+
/**
75+
* A handler for the close event. It is triggered when the user closes the notification.
76+
*/
77+
@JsOverlay
78+
public final void setOnclose(Functions.Func1<Object> onclose) {
79+
this.onclose = onclose;
80+
}
81+
82+
/**
83+
* A handler for the error event. It is triggered each time the notification encounters an error.
84+
*/
85+
@JsOverlay
86+
public final void setOnerror(Functions.Func1<Object> onerror) {
87+
this.onerror = onerror;
88+
}
89+
90+
/**
91+
* A handler for the show event. It is triggered when the notification is displayed.
92+
*/
93+
@JsOverlay
94+
public final void setOnshow(Functions.Func1<Object> onshow) {
95+
this.onshow = onshow;
96+
}
97+
98+
/**
99+
* Requests permission from the user to display notifications.
100+
*/
101+
@JsMethod
102+
public static native Promise requestPermission(Functions.Func1<String> status);
103+
104+
/**
105+
* Programmatically closes a notification.
106+
*/
107+
@JsMethod
108+
public static native void close();
109+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package gwt.material.design.client.pwa.push.js;
2+
3+
import com.google.gwt.core.client.JavaScriptObject;
4+
import jsinterop.annotations.JsPackage;
5+
import jsinterop.annotations.JsProperty;
6+
import jsinterop.annotations.JsType;
7+
8+
import java.util.Date;
9+
10+
@JsType(isNative = true, name = "Object", namespace = JsPackage.GLOBAL)
11+
public class NotificationOptions {
12+
13+
/**
14+
* The actions array of the notification as specified in the options parameter of the constructor.
15+
*/
16+
@JsProperty
17+
public String[] actions;
18+
19+
/**
20+
* The URL of the image used to represent the notification when there is not enough space to display the
21+
* notification itself.
22+
*/
23+
@JsProperty
24+
public String badge;
25+
26+
/**
27+
* The body string of the notification as specified in the options parameter of the constructor.
28+
*/
29+
@JsProperty
30+
public String body;
31+
32+
/**
33+
* Returns a structured clone of the notification’s data.
34+
*/
35+
@JsProperty
36+
public JavaScriptObject data;
37+
38+
/**
39+
* The text direction of the notification as specified in the options parameter of the constructor.
40+
*/
41+
@JsProperty
42+
public String dir;
43+
44+
/**
45+
* The language code of the notification as specified in the options parameter of the constructor.
46+
*/
47+
@JsProperty
48+
public String lang;
49+
50+
/**
51+
* The ID of the notification (if any) as specified in the options parameter of the constructor.
52+
*/
53+
@JsProperty
54+
public String tag;
55+
56+
/**
57+
* The URL of the image used as an icon of the notification as specified in the options parameter of the constructor.
58+
*/
59+
@JsProperty
60+
public String icon;
61+
62+
/**
63+
* The URL of an image to be displayed as part of the notification, as specified in the options parameter of
64+
* the constructor.
65+
*/
66+
@JsProperty
67+
public String image;
68+
69+
/**
70+
* Specifies whether the user should be notified after a new notification replaces an old one.
71+
*/
72+
@JsProperty
73+
public boolean renotify;
74+
75+
/**
76+
* A Boolean indicating that a notification should remain active until the user clicks or dismisses it, rather than
77+
* closing automatically.
78+
*/
79+
@JsProperty
80+
public boolean requireInteraction;
81+
82+
/**
83+
* Specifies whether the notification should be silent, i.e. no sounds or vibrations should be issued, regardless
84+
* of the device settings.
85+
*/
86+
@JsProperty
87+
public boolean silent;
88+
89+
/**
90+
* Specifies the time at which a notification is created or applicable (past, present, or future).
91+
*/
92+
@JsProperty
93+
public Date timestamp;
94+
95+
/**
96+
* The title of the notification as specified in the first parameter of the constructor.
97+
*/
98+
@JsProperty
99+
public String title;
100+
101+
/**
102+
* Specifies a vibration pattern for devices with vibration hardware to emit.
103+
*/
104+
@JsProperty
105+
public boolean vibrate;
106+
}

gwt-material/src/main/java/gwt/material/design/client/pwa/serviceworker/js/ServiceWorkerRegistration.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package gwt.material.design.client.pwa.serviceworker.js;
2121

2222

23+
import gwt.material.design.client.pwa.push.js.Notification;
2324
import gwt.material.design.jquery.client.api.Functions;
2425
import gwt.material.design.jscore.client.api.promise.Promise;
2526
import gwt.material.design.client.pwa.push.js.PushManager;
@@ -102,4 +103,16 @@ public class ServiceWorkerRegistration {
102103
*/
103104
@JsMethod
104105
public native Promise unregister();
106+
107+
/**
108+
* Returns a Promise that resolves to an array of {@link Notification} objects.
109+
*/
110+
@JsMethod
111+
public native Promise getNotifications();
112+
113+
/**
114+
* Displays the notification with the requested title.
115+
*/
116+
@JsMethod
117+
public native void showNotification(String title, Notification notification);
105118
}

0 commit comments

Comments
 (0)