-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathAndroidPushRegistrationPlugin.java
More file actions
162 lines (141 loc) · 5.42 KB
/
Copy pathAndroidPushRegistrationPlugin.java
File metadata and controls
162 lines (141 loc) · 5.42 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package ai.vellum.assistant;
import ai.vellum.assistant.push.NativePushRenderer;
import com.capacitorjs.plugins.pushnotifications.PushNotificationsPlugin;
import com.getcapacitor.JSArray;
import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;
import java.util.function.Consumer;
@CapacitorPlugin(name = "AndroidPushRegistration")
public class AndroidPushRegistrationPlugin extends Plugin {
/**
* Advertised on token registration. The platform sends data-only pushes
* only to tokens claiming this, so a build that cannot render them
* natively must never claim it.
*/
static final String NATIVE_NOTIFICATION_RENDER = "native-notification-render";
static final int NOTIFICATION_OWNERSHIP_VERSION = 1;
/**
* Whether the web runtime currently holds a handler for foreground pushes.
* The plugin instance outlives that handler, so the renderer asks this
* instead: a push handed to a torn-down handler is simply lost.
*/
private static volatile boolean foregroundHandler;
private static volatile boolean notificationOwnership;
private static int bridgeGeneration;
@PluginMethod
public void register(PluginCall call) {
invokeSafely(call, plugin -> plugin.register(call));
}
@PluginMethod
public void unregister(PluginCall call) {
invokeSafely(call, plugin -> {
plugin.unregister(call);
// The conversation shortcuts carry the previous account's titles
// and avatars, so they leave with its token, and only once the
// token is actually on its way out.
NativeFailureGuard.run(
"Unable to remove the Android conversation shortcuts",
() -> NativePushRenderer.clearConversationShortcuts(getContext())
);
});
}
@PluginMethod
public void getCapabilities(PluginCall call) {
call.resolve(capabilitiesPayload());
}
@PluginMethod
public void setForegroundHandler(PluginCall call) {
setForegroundHandler(Boolean.TRUE.equals(call.getBoolean("active", false)));
call.resolve();
}
@PluginMethod
public void getNotificationOwnershipGeneration(PluginCall call) {
call.resolve(notificationOwnershipGenerationPayload());
}
@PluginMethod
public void setNotificationOwnership(PluginCall call) {
Integer version = call.getInt("version");
Integer generation = call.getInt("generation");
Boolean active = call.getBoolean("active");
boolean accepted = negotiateNotificationOwnership(version, generation, active);
call.resolve(notificationOwnershipPayload(accepted));
}
static void setForegroundHandler(boolean active) {
foregroundHandler = active;
}
public static boolean hasForegroundHandler() {
return foregroundHandler;
}
public static boolean hasNotificationOwnership() {
return notificationOwnership;
}
static synchronized boolean negotiateNotificationOwnership(
Integer version,
Integer generation,
Boolean active
) {
if (
version == null
|| version != NOTIFICATION_OWNERSHIP_VERSION
|| generation == null
|| generation != bridgeGeneration
|| active == null
) {
return false;
}
notificationOwnership = active;
return true;
}
static synchronized int bridgeGeneration() {
return bridgeGeneration;
}
public static synchronized void clearBridgeState() {
foregroundHandler = false;
notificationOwnership = false;
bridgeGeneration = bridgeGeneration == Integer.MAX_VALUE
? 0
: bridgeGeneration + 1;
}
static void clearBridgeStateSerialized(Consumer<Runnable> bridgeExecutor) {
clearBridgeState();
bridgeExecutor.accept(AndroidPushRegistrationPlugin::clearBridgeState);
}
static JSObject capabilitiesPayload() {
JSArray capabilities = new JSArray();
capabilities.put(NATIVE_NOTIFICATION_RENDER);
JSObject payload = new JSObject();
payload.put("capabilities", capabilities);
return payload;
}
static synchronized JSObject notificationOwnershipGenerationPayload() {
return new JSObject()
.put("version", NOTIFICATION_OWNERSHIP_VERSION)
.put("generation", bridgeGeneration);
}
static synchronized JSObject notificationOwnershipPayload(boolean accepted) {
JSObject payload = new JSObject();
payload.put("version", NOTIFICATION_OWNERSHIP_VERSION);
payload.put("generation", bridgeGeneration);
payload.put("active", notificationOwnership);
payload.put("accepted", accepted);
return payload;
}
@Override
protected void handleOnDestroy() {
clearBridgeState();
super.handleOnDestroy();
}
private void invokeSafely(PluginCall call, Consumer<PushNotificationsPlugin> operation) {
PushRegistrationGuard.call(call, () -> {
PushNotificationsPlugin plugin = PushNotificationsPlugin.getPushNotificationsInstance();
if (plugin == null) {
PushRegistrationGuard.reject(call);
return;
}
operation.accept(plugin);
});
}
}