@datatalks-ab/push-capacitor 0.0.0-stage → 0.5.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/DatatalksAbPushCapacitor.podspec +21 -0
  2. package/DatatalksPushExtension.podspec +21 -0
  3. package/Package.swift +35 -0
  4. package/README.md +212 -2
  5. package/android/build.gradle +48 -0
  6. package/android/maven/se/datatalks/push-android/0.5.0-beta.1/push-android-0.5.0-beta.1-sources.jar +0 -0
  7. package/android/maven/se/datatalks/push-android/0.5.0-beta.1/push-android-0.5.0-beta.1-sources.jar.md5 +1 -0
  8. package/android/maven/se/datatalks/push-android/0.5.0-beta.1/push-android-0.5.0-beta.1-sources.jar.sha1 +1 -0
  9. package/android/maven/se/datatalks/push-android/0.5.0-beta.1/push-android-0.5.0-beta.1-sources.jar.sha256 +1 -0
  10. package/android/maven/se/datatalks/push-android/0.5.0-beta.1/push-android-0.5.0-beta.1-sources.jar.sha512 +1 -0
  11. package/android/maven/se/datatalks/push-android/0.5.0-beta.1/push-android-0.5.0-beta.1.aar +0 -0
  12. package/android/maven/se/datatalks/push-android/0.5.0-beta.1/push-android-0.5.0-beta.1.aar.md5 +1 -0
  13. package/android/maven/se/datatalks/push-android/0.5.0-beta.1/push-android-0.5.0-beta.1.aar.sha1 +1 -0
  14. package/android/maven/se/datatalks/push-android/0.5.0-beta.1/push-android-0.5.0-beta.1.aar.sha256 +1 -0
  15. package/android/maven/se/datatalks/push-android/0.5.0-beta.1/push-android-0.5.0-beta.1.aar.sha512 +1 -0
  16. package/android/maven/se/datatalks/push-android/0.5.0-beta.1/push-android-0.5.0-beta.1.module +144 -0
  17. package/android/maven/se/datatalks/push-android/0.5.0-beta.1/push-android-0.5.0-beta.1.module.md5 +1 -0
  18. package/android/maven/se/datatalks/push-android/0.5.0-beta.1/push-android-0.5.0-beta.1.module.sha1 +1 -0
  19. package/android/maven/se/datatalks/push-android/0.5.0-beta.1/push-android-0.5.0-beta.1.module.sha256 +1 -0
  20. package/android/maven/se/datatalks/push-android/0.5.0-beta.1/push-android-0.5.0-beta.1.module.sha512 +1 -0
  21. package/android/maven/se/datatalks/push-android/0.5.0-beta.1/push-android-0.5.0-beta.1.pom +63 -0
  22. package/android/maven/se/datatalks/push-android/0.5.0-beta.1/push-android-0.5.0-beta.1.pom.md5 +1 -0
  23. package/android/maven/se/datatalks/push-android/0.5.0-beta.1/push-android-0.5.0-beta.1.pom.sha1 +1 -0
  24. package/android/maven/se/datatalks/push-android/0.5.0-beta.1/push-android-0.5.0-beta.1.pom.sha256 +1 -0
  25. package/android/maven/se/datatalks/push-android/0.5.0-beta.1/push-android-0.5.0-beta.1.pom.sha512 +1 -0
  26. package/android/maven/se/datatalks/push-android/maven-metadata.xml +13 -0
  27. package/android/maven/se/datatalks/push-android/maven-metadata.xml.md5 +1 -0
  28. package/android/maven/se/datatalks/push-android/maven-metadata.xml.sha1 +1 -0
  29. package/android/maven/se/datatalks/push-android/maven-metadata.xml.sha256 +1 -0
  30. package/android/maven/se/datatalks/push-android/maven-metadata.xml.sha512 +1 -0
  31. package/android/src/main/AndroidManifest.xml +2 -0
  32. package/android/src/main/java/se/datatalks/push/capacitor/DataTalksPushPlugin.java +255 -0
  33. package/dist/esm/definitions.d.ts +92 -0
  34. package/dist/esm/definitions.js +3 -0
  35. package/dist/esm/definitions.js.map +1 -0
  36. package/dist/esm/index.d.ts +4 -0
  37. package/dist/esm/index.js +7 -0
  38. package/dist/esm/index.js.map +1 -0
  39. package/dist/esm/web.d.ts +12 -0
  40. package/dist/esm/web.js +26 -0
  41. package/dist/esm/web.js.map +1 -0
  42. package/dist/plugin.cjs.js +40 -0
  43. package/dist/plugin.cjs.js.map +1 -0
  44. package/dist/plugin.js +43 -0
  45. package/dist/plugin.js.map +1 -0
  46. package/ios/Extension/DataTalksNotificationService.swift +120 -0
  47. package/ios/Extension/DeliveryReporter.swift +50 -0
  48. package/ios/Extension/ExtensionShared.swift +101 -0
  49. package/ios/Extension/PrivacyInfo.xcprivacy +27 -0
  50. package/ios/Extension/PushAttachment.swift +90 -0
  51. package/ios/Sources/DataTalksPushPlugin/Core/AppKeys.swift +34 -0
  52. package/ios/Sources/DataTalksPushPlugin/Core/DataTalksPush.swift +110 -0
  53. package/ios/Sources/DataTalksPushPlugin/Core/DataTalksPushListener.swift +72 -0
  54. package/ios/Sources/DataTalksPushPlugin/Core/DeviceFacts.swift +128 -0
  55. package/ios/Sources/DataTalksPushPlugin/Core/PrivacyInfo.xcprivacy +60 -0
  56. package/ios/Sources/DataTalksPushPlugin/Core/PushApi.swift +64 -0
  57. package/ios/Sources/DataTalksPushPlugin/Core/PushMessage.swift +47 -0
  58. package/ios/Sources/DataTalksPushPlugin/Core/PushRuntime.swift +369 -0
  59. package/ios/Sources/DataTalksPushPlugin/Core/PushStore.swift +465 -0
  60. package/ios/Sources/DataTalksPushPlugin/Core/PushSync.swift +381 -0
  61. package/ios/Sources/DataTalksPushPlugin/Core/SharedContainer.swift +104 -0
  62. package/ios/Sources/DataTalksPushPlugin/DataTalksPushPlugin.swift +197 -0
  63. package/package.json +55 -4
@@ -0,0 +1,255 @@
1
+ package se.datatalks.push.capacitor;
2
+
3
+ import android.app.Activity;
4
+ import android.content.Intent;
5
+ import android.content.pm.PackageManager;
6
+ import android.content.pm.ResolveInfo;
7
+ import android.os.Build;
8
+ import com.getcapacitor.JSObject;
9
+ import com.getcapacitor.Logger;
10
+ import com.getcapacitor.PermissionState;
11
+ import com.getcapacitor.Plugin;
12
+ import com.getcapacitor.PluginCall;
13
+ import com.getcapacitor.PluginMethod;
14
+ import com.getcapacitor.annotation.CapacitorPlugin;
15
+ import java.util.List;
16
+ import org.json.JSONException;
17
+ import org.json.JSONObject;
18
+ import se.datatalks.push.DataTalksIdentityResult;
19
+ import se.datatalks.push.DataTalksMessage;
20
+ import se.datatalks.push.DataTalksMessagingService;
21
+ import se.datatalks.push.DataTalksPermission;
22
+ import se.datatalks.push.DataTalksPush;
23
+ import se.datatalks.push.DataTalksPushListener;
24
+ import se.datatalks.push.DataTalksPushOptions;
25
+
26
+ /**
27
+ * Capacitor bridge to the Data Talks Android SDK. It has no logic of its own: each call forwards to {@link DataTalksPush},
28
+ * and the SDK's listener callbacks become plugin events.
29
+ */
30
+ @CapacitorPlugin(name = "DataTalksPush")
31
+ public class DataTalksPushPlugin extends Plugin {
32
+
33
+ private static final String TAG = "DataTalksPush";
34
+ private static final String MESSAGING_EVENT = "com.google.firebase.MESSAGING_EVENT";
35
+ private static final String C2DM_RECEIVE = "com.google.android.c2dm.intent.RECEIVE";
36
+ // Declared by Firebase itself, at the lowest priority, for apps without their own service.
37
+ private static final String FIREBASE_DEFAULT_SERVICE = "com.google.firebase.messaging.FirebaseMessagingService";
38
+ // Package prefix of Firebase's own receiver, which hands messages to the service.
39
+ private static final String FIREBASE_PACKAGE = "com.google.firebase.";
40
+
41
+ // The SDK holds one listener per process, and each bridge creates its own plugin instance. Only the newest may clear it.
42
+ private static DataTalksPushPlugin current;
43
+
44
+ private final DataTalksPushListener listener = new DataTalksPushListener() {
45
+ @Override
46
+ public void onNotificationOpened(String deepLink, JSONObject data) {
47
+ JSObject event = new JSObject();
48
+ event.put("deepLink", deepLink);
49
+ putData(event, data);
50
+ // Retained until the web app listens: a tap that launches the app arrives before its JavaScript runs.
51
+ notifyListeners("notificationOpened", event, true);
52
+ }
53
+
54
+ @Override
55
+ public void onMessageReceived(DataTalksMessage message) {
56
+ JSObject event = new JSObject();
57
+ event.put("uid", message.getUid());
58
+ event.put("title", message.getTitle());
59
+ event.put("body", message.getBody());
60
+ event.put("imageUrl", message.getImageUrl());
61
+ event.put("deepLink", message.getDeepLink());
62
+ putData(event, message.getData());
63
+ notifyListeners("messageReceived", event);
64
+ }
65
+
66
+ @Override
67
+ public void onIdentityResult(DataTalksIdentityResult result) {
68
+ JSObject event = new JSObject();
69
+ event.put("verified", result.getVerified());
70
+ event.put("errorCode", result.getErrorCode());
71
+ event.put("errorDetail", result.getErrorDetail());
72
+ notifyListeners("identityResult", event);
73
+ }
74
+ };
75
+
76
+ @Override
77
+ public void load() {
78
+ // Listener first: a tap the SDK is holding is delivered now and retained in the plugin until the web app listens.
79
+ setCurrent(this);
80
+ DataTalksPush.setListener(listener);
81
+ warnAboutOtherPushPlugins();
82
+
83
+ String appKey = getConfig().getString("appKey");
84
+ if (appKey == null || appKey.isEmpty()) {
85
+ Logger.error(TAG, "capacitor.config has no plugins.DataTalksPush.appKey: the SDK stays off.", null);
86
+ return;
87
+ }
88
+
89
+ DataTalksPushOptions options = new DataTalksPushOptions(getConfig().getString("baseUrl"), getConfig().getBoolean("openLinks", true));
90
+ DataTalksPush.initialize(getContext(), appKey, options);
91
+ }
92
+
93
+ /**
94
+ * Firebase delivers each message to a single service, so another push plugin's service also takes Data Talks pushes and
95
+ * they never show. This can't be fixed at run time, so the other service is logged at startup (Capacitor logs only in
96
+ * debug builds by default).
97
+ */
98
+ private void warnAboutOtherPushPlugins() {
99
+ PackageManager packages = getContext().getPackageManager();
100
+ String app = getContext().getPackageName();
101
+
102
+ for (ResolveInfo resolved : queryServices(packages, new Intent(MESSAGING_EVENT).setPackage(app))) {
103
+ String service = resolved.serviceInfo.name;
104
+ if (service.equals(FIREBASE_DEFAULT_SERVICE) || handsMessagesToSdk(service)) continue;
105
+
106
+ Logger.error(TAG, "Another service receives Firebase messages: " + service + ". Firebase gives each message to one "
107
+ + "service, so unless it forwards them to DataTalksPush.handleRemoteMessage, pushes from Data Talks never "
108
+ + "show. Remove the other push plugin, such as @capacitor/push-notifications.", null);
109
+ }
110
+
111
+ // Some SDKs, such as OneSignal, read the messages before Firebase does, with a broadcast receiver of their own.
112
+ for (ResolveInfo resolved : queryReceivers(packages, new Intent(C2DM_RECEIVE).setPackage(app))) {
113
+ String receiver = resolved.activityInfo.name;
114
+ if (!receiver.startsWith(FIREBASE_PACKAGE)) {
115
+ Logger.error(TAG, "Another push SDK receives Firebase messages: " + receiver + ". Pushes from Data Talks may never "
116
+ + "show, or show twice. Remove the other SDK, such as OneSignal.", null);
117
+ }
118
+ }
119
+ }
120
+
121
+ /** The SDK's own service, or an app service that extends it (see the Android SDK README): both forward Data Talks messages. */
122
+ private boolean handsMessagesToSdk(String service) {
123
+ try {
124
+ return DataTalksMessagingService.class.isAssignableFrom(Class.forName(service, false, getContext().getClassLoader()));
125
+ } catch (ClassNotFoundException | LinkageError e) {
126
+ // A check for the log must never keep the SDK from starting.
127
+ return false;
128
+ }
129
+ }
130
+
131
+ @SuppressWarnings("deprecation")
132
+ private static List<ResolveInfo> queryServices(PackageManager packages, Intent intent) {
133
+ return Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU
134
+ ? packages.queryIntentServices(intent, PackageManager.ResolveInfoFlags.of(0))
135
+ : packages.queryIntentServices(intent, 0);
136
+ }
137
+
138
+ @SuppressWarnings("deprecation")
139
+ private static List<ResolveInfo> queryReceivers(PackageManager packages, Intent intent) {
140
+ return Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU
141
+ ? packages.queryBroadcastReceivers(intent, PackageManager.ResolveInfoFlags.of(0))
142
+ : packages.queryBroadcastReceivers(intent, 0);
143
+ }
144
+
145
+ @Override
146
+ protected void handleOnDestroy() {
147
+ // A tap while no bridge exists waits in the SDK for the next one.
148
+ if (clearCurrent(this)) DataTalksPush.setListener(null);
149
+ }
150
+
151
+ @Override
152
+ @PluginMethod
153
+ public void checkPermissions(PluginCall call) {
154
+ call.resolve(permissionResult(DataTalksPush.permissionStatus(getContext())));
155
+ }
156
+
157
+ @Override
158
+ @PluginMethod
159
+ public void requestPermissions(PluginCall call) {
160
+ Activity activity = getActivity();
161
+ if (activity == null) {
162
+ call.reject("There is no activity to ask from.");
163
+ return;
164
+ }
165
+
166
+ DataTalksPush.requestPermission(activity, permission -> call.resolve(permissionResult(permission)));
167
+ }
168
+
169
+ @PluginMethod
170
+ public void identify(PluginCall call) {
171
+ String externalId = call.getString("externalId");
172
+ String email = call.getString("email");
173
+ String identityToken = call.getString("identityToken");
174
+ if (isEmpty(externalId) && isEmpty(email) && isEmpty(identityToken)) {
175
+ call.reject("identify needs an externalId, an email or an identityToken.");
176
+ return;
177
+ }
178
+
179
+ DataTalksPush.identify(externalId, email, identityToken);
180
+ call.resolve();
181
+ }
182
+
183
+ @PluginMethod
184
+ public void logout(PluginCall call) {
185
+ DataTalksPush.logout();
186
+ call.resolve();
187
+ }
188
+
189
+ // The Android SDK reads the FCM token from Firebase itself.
190
+ @PluginMethod
191
+ public void setFcmToken(PluginCall call) {
192
+ call.resolve();
193
+ }
194
+
195
+ @PluginMethod
196
+ public void setMarketingOptIn(PluginCall call) {
197
+ Boolean optIn = call.getBoolean("optIn");
198
+ if (optIn == null) {
199
+ call.reject("setMarketingOptIn needs optIn.");
200
+ return;
201
+ }
202
+
203
+ DataTalksPush.setMarketingOptIn(optIn);
204
+ call.resolve();
205
+ }
206
+
207
+ @PluginMethod
208
+ public void deleteLocalData(PluginCall call) {
209
+ DataTalksPush.deleteLocalData();
210
+ call.resolve();
211
+ }
212
+
213
+ private static synchronized void setCurrent(DataTalksPushPlugin plugin) {
214
+ current = plugin;
215
+ }
216
+
217
+ private static synchronized boolean clearCurrent(DataTalksPushPlugin plugin) {
218
+ if (current != plugin) return false;
219
+ current = null;
220
+ return true;
221
+ }
222
+
223
+ /** Maps to Capacitor's states: prompt until the app asks, on Android 13 and later. */
224
+ private static JSObject permissionResult(DataTalksPermission permission) {
225
+ PermissionState state;
226
+ switch (permission) {
227
+ case AUTHORIZED:
228
+ state = PermissionState.GRANTED;
229
+ break;
230
+ case DENIED:
231
+ state = PermissionState.DENIED;
232
+ break;
233
+ default:
234
+ state = PermissionState.PROMPT;
235
+ break;
236
+ }
237
+
238
+ JSObject result = new JSObject();
239
+ result.put("receive", state.toString());
240
+ return result;
241
+ }
242
+
243
+ private static void putData(JSObject event, JSONObject data) {
244
+ if (data == null) return;
245
+ try {
246
+ event.put("data", JSObject.fromJSONObject(data));
247
+ } catch (JSONException e) {
248
+ Logger.warn(TAG, "A push's data isn't an object, so the event goes without it.");
249
+ }
250
+ }
251
+
252
+ private static boolean isEmpty(String value) {
253
+ return value == null || value.isEmpty();
254
+ }
255
+ }
@@ -0,0 +1,92 @@
1
+ import type { PermissionState, PluginListenerHandle } from '@capacitor/core';
2
+ declare module '@capacitor/cli' {
3
+ interface PluginsConfig {
4
+ /** Read by the native side at launch, so the SDK starts before the web app has loaded. */
5
+ DataTalksPush?: {
6
+ /** The app's publishable key from the portal, `dtpk_…`. Without it the SDK stays off. */
7
+ appKey?: string;
8
+ /**
9
+ * Default true: the SDK opens a push's link, as in a native app. False suits an app that routes in JS: a tap only opens
10
+ * the app, and the link reaches `notificationOpened`.
11
+ */
12
+ openLinks?: boolean;
13
+ /** Replaces the host the key selects. Only for testing against a local ingress. */
14
+ baseUrl?: string;
15
+ };
16
+ }
17
+ }
18
+ export interface PermissionStatus {
19
+ /** `prompt` until the app asks (iOS, and Android 13 and later). */
20
+ receive: PermissionState;
21
+ }
22
+ export interface RequestPermissionsOptions {
23
+ /** iOS: ask for provisional authorization, which shows no prompt and delivers quietly. Ignored on Android. */
24
+ provisional?: boolean;
25
+ }
26
+ export interface IdentifyOptions {
27
+ /** Your id for the user: the same value as the identity token's `sub`. */
28
+ externalId?: string;
29
+ email?: string;
30
+ /** The token your backend signed for this user. When the push app requires verified identity, a call without one is refused. */
31
+ identityToken?: string;
32
+ }
33
+ export interface MarketingOptInOptions {
34
+ optIn: boolean;
35
+ }
36
+ export interface FcmTokenOptions {
37
+ token: string;
38
+ }
39
+ export interface NotificationOpenedEvent {
40
+ deepLink?: string;
41
+ data?: Record<string, unknown>;
42
+ }
43
+ /** A Data Talks push. On iOS, only while the app is open; otherwise the system shows it. */
44
+ export interface MessageReceivedEvent {
45
+ uid?: string;
46
+ title?: string;
47
+ body?: string;
48
+ /** The picture's https URL. On iOS, the notification service extension (`DataTalksPushExtension`) shows it. */
49
+ imageUrl?: string;
50
+ deepLink?: string;
51
+ data?: Record<string, unknown>;
52
+ }
53
+ export interface IdentityResultEvent {
54
+ verified: boolean;
55
+ /** Set when the identify was refused. It isn't retried: call `identify` again at the next sign-in. */
56
+ errorCode?: string;
57
+ /** Why an identity token failed. It never says whether a profile was found. */
58
+ errorDetail?: string;
59
+ }
60
+ /**
61
+ * Calls resolve at once: the native SDK does the work in the background and retries it, and answers arrive as events.
62
+ */
63
+ export interface DataTalksPushPlugin {
64
+ checkPermissions(): Promise<PermissionStatus>;
65
+ /** Shows the system prompt where one exists, and resolves with the answer. */
66
+ requestPermissions(options?: RequestPermissionsOptions): Promise<PermissionStatus>;
67
+ /**
68
+ * Links this install to the signed-in user; the answer comes as an `identityResult` event. Repeating the same `externalId` and
69
+ * `email` (with or without a token, as before) on the same UTC day sends nothing: the event repeats that day's answer, which says
70
+ * nothing about this call's token. A token-only identify is always sent.
71
+ */
72
+ identify(options: IdentifyOptions): Promise<void>;
73
+ /** Unlinks this install from its user. The install stays registered. */
74
+ logout(): Promise<void>;
75
+ /**
76
+ * For apps whose iOS pushes go through their own Firebase project (transport FCM in the portal): pass the FCM registration token
77
+ * each time Firebase reports it. Not needed when the AppDelegate posts the token as Capacitor's Firebase setup does. On Android the
78
+ * SDK reads the token itself, so the call does nothing there.
79
+ */
80
+ setFcmToken(options: FcmTokenOptions): Promise<void>;
81
+ /** Records the user's in-app marketing choice on this device. Transactional pushes aren't affected. */
82
+ setMarketingOptIn(options: MarketingOptInOptions): Promise<void>;
83
+ /** Deletes this install and everything the SDK keeps on the device. The SDK starts a new install at the next launch. */
84
+ deleteLocalData(): Promise<void>;
85
+ /** A tap on a notification. A tap that started the app waits until the first listener is added. */
86
+ addListener(eventName: 'notificationOpened', listenerFunc: (event: NotificationOpenedEvent) => void): Promise<PluginListenerHandle>;
87
+ /** A Data Talks push arrived: always on Android, only while the app is open on iOS. */
88
+ addListener(eventName: 'messageReceived', listenerFunc: (event: MessageReceivedEvent) => void): Promise<PluginListenerHandle>;
89
+ /** The service answered an `identify`. */
90
+ addListener(eventName: 'identityResult', listenerFunc: (event: IdentityResultEvent) => void): Promise<PluginListenerHandle>;
91
+ removeAllListeners(): Promise<void>;
92
+ }
@@ -0,0 +1,3 @@
1
+ /// <reference types="@capacitor/cli" />
2
+ export {};
3
+ //# sourceMappingURL=definitions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"definitions.js","sourceRoot":"","sources":["../../src/definitions.ts"],"names":[],"mappings":"AAAA,wCAAwC","sourcesContent":["/// <reference types=\"@capacitor/cli\" />\n\nimport type { PermissionState, PluginListenerHandle } from '@capacitor/core';\n\ndeclare module '@capacitor/cli' {\n export interface PluginsConfig {\n /** Read by the native side at launch, so the SDK starts before the web app has loaded. */\n DataTalksPush?: {\n /** The app's publishable key from the portal, `dtpk_…`. Without it the SDK stays off. */\n appKey?: string;\n /**\n * Default true: the SDK opens a push's link, as in a native app. False suits an app that routes in JS: a tap only opens\n * the app, and the link reaches `notificationOpened`.\n */\n openLinks?: boolean;\n /** Replaces the host the key selects. Only for testing against a local ingress. */\n baseUrl?: string;\n };\n }\n}\n\nexport interface PermissionStatus {\n /** `prompt` until the app asks (iOS, and Android 13 and later). */\n receive: PermissionState;\n}\n\nexport interface RequestPermissionsOptions {\n /** iOS: ask for provisional authorization, which shows no prompt and delivers quietly. Ignored on Android. */\n provisional?: boolean;\n}\n\nexport interface IdentifyOptions {\n /** Your id for the user: the same value as the identity token's `sub`. */\n externalId?: string;\n email?: string;\n /** The token your backend signed for this user. When the push app requires verified identity, a call without one is refused. */\n identityToken?: string;\n}\n\nexport interface MarketingOptInOptions {\n optIn: boolean;\n}\n\nexport interface FcmTokenOptions {\n token: string;\n}\n\nexport interface NotificationOpenedEvent {\n deepLink?: string;\n data?: Record<string, unknown>;\n}\n\n/** A Data Talks push. On iOS, only while the app is open; otherwise the system shows it. */\nexport interface MessageReceivedEvent {\n uid?: string;\n title?: string;\n body?: string;\n /** The picture's https URL. On iOS, the notification service extension (`DataTalksPushExtension`) shows it. */\n imageUrl?: string;\n deepLink?: string;\n data?: Record<string, unknown>;\n}\n\nexport interface IdentityResultEvent {\n verified: boolean;\n /** Set when the identify was refused. It isn't retried: call `identify` again at the next sign-in. */\n errorCode?: string;\n /** Why an identity token failed. It never says whether a profile was found. */\n errorDetail?: string;\n}\n\n/**\n * Calls resolve at once: the native SDK does the work in the background and retries it, and answers arrive as events.\n */\nexport interface DataTalksPushPlugin {\n checkPermissions(): Promise<PermissionStatus>;\n\n /** Shows the system prompt where one exists, and resolves with the answer. */\n requestPermissions(options?: RequestPermissionsOptions): Promise<PermissionStatus>;\n\n /**\n * Links this install to the signed-in user; the answer comes as an `identityResult` event. Repeating the same `externalId` and\n * `email` (with or without a token, as before) on the same UTC day sends nothing: the event repeats that day's answer, which says\n * nothing about this call's token. A token-only identify is always sent.\n */\n identify(options: IdentifyOptions): Promise<void>;\n\n /** Unlinks this install from its user. The install stays registered. */\n logout(): Promise<void>;\n\n /**\n * For apps whose iOS pushes go through their own Firebase project (transport FCM in the portal): pass the FCM registration token\n * each time Firebase reports it. Not needed when the AppDelegate posts the token as Capacitor's Firebase setup does. On Android the\n * SDK reads the token itself, so the call does nothing there.\n */\n setFcmToken(options: FcmTokenOptions): Promise<void>;\n\n /** Records the user's in-app marketing choice on this device. Transactional pushes aren't affected. */\n setMarketingOptIn(options: MarketingOptInOptions): Promise<void>;\n\n /** Deletes this install and everything the SDK keeps on the device. The SDK starts a new install at the next launch. */\n deleteLocalData(): Promise<void>;\n\n /** A tap on a notification. A tap that started the app waits until the first listener is added. */\n addListener(eventName: 'notificationOpened', listenerFunc: (event: NotificationOpenedEvent) => void): Promise<PluginListenerHandle>;\n\n /** A Data Talks push arrived: always on Android, only while the app is open on iOS. */\n addListener(eventName: 'messageReceived', listenerFunc: (event: MessageReceivedEvent) => void): Promise<PluginListenerHandle>;\n\n /** The service answered an `identify`. */\n addListener(eventName: 'identityResult', listenerFunc: (event: IdentityResultEvent) => void): Promise<PluginListenerHandle>;\n\n removeAllListeners(): Promise<void>;\n}\n"]}
@@ -0,0 +1,4 @@
1
+ import type { DataTalksPushPlugin } from './definitions';
2
+ declare const DataTalksPush: DataTalksPushPlugin;
3
+ export * from './definitions';
4
+ export { DataTalksPush };
@@ -0,0 +1,7 @@
1
+ import { registerPlugin } from '@capacitor/core';
2
+ const DataTalksPush = registerPlugin('DataTalksPush', {
3
+ web: () => import('./web').then((m) => new m.DataTalksPushWeb()),
4
+ });
5
+ export * from './definitions';
6
+ export { DataTalksPush };
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAIjD,MAAM,aAAa,GAAG,cAAc,CAAsB,eAAe,EAAE;IACzE,GAAG,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,gBAAgB,EAAE,CAAC;CACjE,CAAC,CAAC;AAEH,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,aAAa,EAAE,CAAC","sourcesContent":["import { registerPlugin } from '@capacitor/core';\n\nimport type { DataTalksPushPlugin } from './definitions';\n\nconst DataTalksPush = registerPlugin<DataTalksPushPlugin>('DataTalksPush', {\n web: () => import('./web').then((m) => new m.DataTalksPushWeb()),\n});\n\nexport * from './definitions';\nexport { DataTalksPush };\n"]}
@@ -0,0 +1,12 @@
1
+ import { WebPlugin } from '@capacitor/core';
2
+ import type { DataTalksPushPlugin, PermissionStatus } from './definitions';
3
+ /** Push needs the native SDKs: in a browser every call is refused as unimplemented. */
4
+ export declare class DataTalksPushWeb extends WebPlugin implements DataTalksPushPlugin {
5
+ checkPermissions(): Promise<PermissionStatus>;
6
+ requestPermissions(): Promise<PermissionStatus>;
7
+ identify(): Promise<void>;
8
+ logout(): Promise<void>;
9
+ setFcmToken(): Promise<void>;
10
+ setMarketingOptIn(): Promise<void>;
11
+ deleteLocalData(): Promise<void>;
12
+ }
@@ -0,0 +1,26 @@
1
+ import { WebPlugin } from '@capacitor/core';
2
+ /** Push needs the native SDKs: in a browser every call is refused as unimplemented. */
3
+ export class DataTalksPushWeb extends WebPlugin {
4
+ async checkPermissions() {
5
+ throw this.unimplemented('Data Talks push runs on Android and iOS only.');
6
+ }
7
+ async requestPermissions() {
8
+ throw this.unimplemented('Data Talks push runs on Android and iOS only.');
9
+ }
10
+ async identify() {
11
+ throw this.unimplemented('Data Talks push runs on Android and iOS only.');
12
+ }
13
+ async logout() {
14
+ throw this.unimplemented('Data Talks push runs on Android and iOS only.');
15
+ }
16
+ async setFcmToken() {
17
+ throw this.unimplemented('Data Talks push runs on Android and iOS only.');
18
+ }
19
+ async setMarketingOptIn() {
20
+ throw this.unimplemented('Data Talks push runs on Android and iOS only.');
21
+ }
22
+ async deleteLocalData() {
23
+ throw this.unimplemented('Data Talks push runs on Android and iOS only.');
24
+ }
25
+ }
26
+ //# sourceMappingURL=web.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web.js","sourceRoot":"","sources":["../../src/web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAI5C,uFAAuF;AACvF,MAAM,OAAO,gBAAiB,SAAQ,SAAS;IAC7C,KAAK,CAAC,gBAAgB;QACpB,MAAM,IAAI,CAAC,aAAa,CAAC,+CAA+C,CAAC,CAAC;IAC5E,CAAC;IAED,KAAK,CAAC,kBAAkB;QACtB,MAAM,IAAI,CAAC,aAAa,CAAC,+CAA+C,CAAC,CAAC;IAC5E,CAAC;IAED,KAAK,CAAC,QAAQ;QACZ,MAAM,IAAI,CAAC,aAAa,CAAC,+CAA+C,CAAC,CAAC;IAC5E,CAAC;IAED,KAAK,CAAC,MAAM;QACV,MAAM,IAAI,CAAC,aAAa,CAAC,+CAA+C,CAAC,CAAC;IAC5E,CAAC;IAED,KAAK,CAAC,WAAW;QACf,MAAM,IAAI,CAAC,aAAa,CAAC,+CAA+C,CAAC,CAAC;IAC5E,CAAC;IAED,KAAK,CAAC,iBAAiB;QACrB,MAAM,IAAI,CAAC,aAAa,CAAC,+CAA+C,CAAC,CAAC;IAC5E,CAAC;IAED,KAAK,CAAC,eAAe;QACnB,MAAM,IAAI,CAAC,aAAa,CAAC,+CAA+C,CAAC,CAAC;IAC5E,CAAC;CACF","sourcesContent":["import { WebPlugin } from '@capacitor/core';\n\nimport type { DataTalksPushPlugin, PermissionStatus } from './definitions';\n\n/** Push needs the native SDKs: in a browser every call is refused as unimplemented. */\nexport class DataTalksPushWeb extends WebPlugin implements DataTalksPushPlugin {\n async checkPermissions(): Promise<PermissionStatus> {\n throw this.unimplemented('Data Talks push runs on Android and iOS only.');\n }\n\n async requestPermissions(): Promise<PermissionStatus> {\n throw this.unimplemented('Data Talks push runs on Android and iOS only.');\n }\n\n async identify(): Promise<void> {\n throw this.unimplemented('Data Talks push runs on Android and iOS only.');\n }\n\n async logout(): Promise<void> {\n throw this.unimplemented('Data Talks push runs on Android and iOS only.');\n }\n\n async setFcmToken(): Promise<void> {\n throw this.unimplemented('Data Talks push runs on Android and iOS only.');\n }\n\n async setMarketingOptIn(): Promise<void> {\n throw this.unimplemented('Data Talks push runs on Android and iOS only.');\n }\n\n async deleteLocalData(): Promise<void> {\n throw this.unimplemented('Data Talks push runs on Android and iOS only.');\n }\n}\n"]}
@@ -0,0 +1,40 @@
1
+ 'use strict';
2
+
3
+ var core = require('@capacitor/core');
4
+
5
+ const DataTalksPush = core.registerPlugin('DataTalksPush', {
6
+ web: () => Promise.resolve().then(function () { return web; }).then((m) => new m.DataTalksPushWeb()),
7
+ });
8
+
9
+ /** Push needs the native SDKs: in a browser every call is refused as unimplemented. */
10
+ class DataTalksPushWeb extends core.WebPlugin {
11
+ async checkPermissions() {
12
+ throw this.unimplemented('Data Talks push runs on Android and iOS only.');
13
+ }
14
+ async requestPermissions() {
15
+ throw this.unimplemented('Data Talks push runs on Android and iOS only.');
16
+ }
17
+ async identify() {
18
+ throw this.unimplemented('Data Talks push runs on Android and iOS only.');
19
+ }
20
+ async logout() {
21
+ throw this.unimplemented('Data Talks push runs on Android and iOS only.');
22
+ }
23
+ async setFcmToken() {
24
+ throw this.unimplemented('Data Talks push runs on Android and iOS only.');
25
+ }
26
+ async setMarketingOptIn() {
27
+ throw this.unimplemented('Data Talks push runs on Android and iOS only.');
28
+ }
29
+ async deleteLocalData() {
30
+ throw this.unimplemented('Data Talks push runs on Android and iOS only.');
31
+ }
32
+ }
33
+
34
+ var web = /*#__PURE__*/Object.freeze({
35
+ __proto__: null,
36
+ DataTalksPushWeb: DataTalksPushWeb
37
+ });
38
+
39
+ exports.DataTalksPush = DataTalksPush;
40
+ //# sourceMappingURL=plugin.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.cjs.js","sources":["esm/index.js","esm/web.js"],"sourcesContent":["import { registerPlugin } from '@capacitor/core';\nconst DataTalksPush = registerPlugin('DataTalksPush', {\n web: () => import('./web').then((m) => new m.DataTalksPushWeb()),\n});\nexport * from './definitions';\nexport { DataTalksPush };\n//# sourceMappingURL=index.js.map","import { WebPlugin } from '@capacitor/core';\n/** Push needs the native SDKs: in a browser every call is refused as unimplemented. */\nexport class DataTalksPushWeb extends WebPlugin {\n async checkPermissions() {\n throw this.unimplemented('Data Talks push runs on Android and iOS only.');\n }\n async requestPermissions() {\n throw this.unimplemented('Data Talks push runs on Android and iOS only.');\n }\n async identify() {\n throw this.unimplemented('Data Talks push runs on Android and iOS only.');\n }\n async logout() {\n throw this.unimplemented('Data Talks push runs on Android and iOS only.');\n }\n async setFcmToken() {\n throw this.unimplemented('Data Talks push runs on Android and iOS only.');\n }\n async setMarketingOptIn() {\n throw this.unimplemented('Data Talks push runs on Android and iOS only.');\n }\n async deleteLocalData() {\n throw this.unimplemented('Data Talks push runs on Android and iOS only.');\n }\n}\n//# sourceMappingURL=web.js.map"],"names":["registerPlugin","WebPlugin"],"mappings":";;;;AACK,MAAC,aAAa,GAAGA,mBAAc,CAAC,eAAe,EAAE;AACtD,IAAI,GAAG,EAAE,MAAM,mDAAe,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,gBAAgB,EAAE,CAAC;AACpE,CAAC;;ACFD;AACO,MAAM,gBAAgB,SAASC,cAAS,CAAC;AAChD,IAAI,MAAM,gBAAgB,GAAG;AAC7B,QAAQ,MAAM,IAAI,CAAC,aAAa,CAAC,+CAA+C,CAAC;AACjF,IAAI;AACJ,IAAI,MAAM,kBAAkB,GAAG;AAC/B,QAAQ,MAAM,IAAI,CAAC,aAAa,CAAC,+CAA+C,CAAC;AACjF,IAAI;AACJ,IAAI,MAAM,QAAQ,GAAG;AACrB,QAAQ,MAAM,IAAI,CAAC,aAAa,CAAC,+CAA+C,CAAC;AACjF,IAAI;AACJ,IAAI,MAAM,MAAM,GAAG;AACnB,QAAQ,MAAM,IAAI,CAAC,aAAa,CAAC,+CAA+C,CAAC;AACjF,IAAI;AACJ,IAAI,MAAM,WAAW,GAAG;AACxB,QAAQ,MAAM,IAAI,CAAC,aAAa,CAAC,+CAA+C,CAAC;AACjF,IAAI;AACJ,IAAI,MAAM,iBAAiB,GAAG;AAC9B,QAAQ,MAAM,IAAI,CAAC,aAAa,CAAC,+CAA+C,CAAC;AACjF,IAAI;AACJ,IAAI,MAAM,eAAe,GAAG;AAC5B,QAAQ,MAAM,IAAI,CAAC,aAAa,CAAC,+CAA+C,CAAC;AACjF,IAAI;AACJ;;;;;;;"}
package/dist/plugin.js ADDED
@@ -0,0 +1,43 @@
1
+ var capacitorDataTalksPush = (function (exports, core) {
2
+ 'use strict';
3
+
4
+ const DataTalksPush = core.registerPlugin('DataTalksPush', {
5
+ web: () => Promise.resolve().then(function () { return web; }).then((m) => new m.DataTalksPushWeb()),
6
+ });
7
+
8
+ /** Push needs the native SDKs: in a browser every call is refused as unimplemented. */
9
+ class DataTalksPushWeb extends core.WebPlugin {
10
+ async checkPermissions() {
11
+ throw this.unimplemented('Data Talks push runs on Android and iOS only.');
12
+ }
13
+ async requestPermissions() {
14
+ throw this.unimplemented('Data Talks push runs on Android and iOS only.');
15
+ }
16
+ async identify() {
17
+ throw this.unimplemented('Data Talks push runs on Android and iOS only.');
18
+ }
19
+ async logout() {
20
+ throw this.unimplemented('Data Talks push runs on Android and iOS only.');
21
+ }
22
+ async setFcmToken() {
23
+ throw this.unimplemented('Data Talks push runs on Android and iOS only.');
24
+ }
25
+ async setMarketingOptIn() {
26
+ throw this.unimplemented('Data Talks push runs on Android and iOS only.');
27
+ }
28
+ async deleteLocalData() {
29
+ throw this.unimplemented('Data Talks push runs on Android and iOS only.');
30
+ }
31
+ }
32
+
33
+ var web = /*#__PURE__*/Object.freeze({
34
+ __proto__: null,
35
+ DataTalksPushWeb: DataTalksPushWeb
36
+ });
37
+
38
+ exports.DataTalksPush = DataTalksPush;
39
+
40
+ return exports;
41
+
42
+ })({}, capacitorExports);
43
+ //# sourceMappingURL=plugin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.js","sources":["esm/index.js","esm/web.js"],"sourcesContent":["import { registerPlugin } from '@capacitor/core';\nconst DataTalksPush = registerPlugin('DataTalksPush', {\n web: () => import('./web').then((m) => new m.DataTalksPushWeb()),\n});\nexport * from './definitions';\nexport { DataTalksPush };\n//# sourceMappingURL=index.js.map","import { WebPlugin } from '@capacitor/core';\n/** Push needs the native SDKs: in a browser every call is refused as unimplemented. */\nexport class DataTalksPushWeb extends WebPlugin {\n async checkPermissions() {\n throw this.unimplemented('Data Talks push runs on Android and iOS only.');\n }\n async requestPermissions() {\n throw this.unimplemented('Data Talks push runs on Android and iOS only.');\n }\n async identify() {\n throw this.unimplemented('Data Talks push runs on Android and iOS only.');\n }\n async logout() {\n throw this.unimplemented('Data Talks push runs on Android and iOS only.');\n }\n async setFcmToken() {\n throw this.unimplemented('Data Talks push runs on Android and iOS only.');\n }\n async setMarketingOptIn() {\n throw this.unimplemented('Data Talks push runs on Android and iOS only.');\n }\n async deleteLocalData() {\n throw this.unimplemented('Data Talks push runs on Android and iOS only.');\n }\n}\n//# sourceMappingURL=web.js.map"],"names":["registerPlugin","WebPlugin"],"mappings":";;;AACK,UAAC,aAAa,GAAGA,mBAAc,CAAC,eAAe,EAAE;IACtD,IAAI,GAAG,EAAE,MAAM,mDAAe,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,gBAAgB,EAAE,CAAC;IACpE,CAAC;;ICFD;IACO,MAAM,gBAAgB,SAASC,cAAS,CAAC;IAChD,IAAI,MAAM,gBAAgB,GAAG;IAC7B,QAAQ,MAAM,IAAI,CAAC,aAAa,CAAC,+CAA+C,CAAC;IACjF,IAAI;IACJ,IAAI,MAAM,kBAAkB,GAAG;IAC/B,QAAQ,MAAM,IAAI,CAAC,aAAa,CAAC,+CAA+C,CAAC;IACjF,IAAI;IACJ,IAAI,MAAM,QAAQ,GAAG;IACrB,QAAQ,MAAM,IAAI,CAAC,aAAa,CAAC,+CAA+C,CAAC;IACjF,IAAI;IACJ,IAAI,MAAM,MAAM,GAAG;IACnB,QAAQ,MAAM,IAAI,CAAC,aAAa,CAAC,+CAA+C,CAAC;IACjF,IAAI;IACJ,IAAI,MAAM,WAAW,GAAG;IACxB,QAAQ,MAAM,IAAI,CAAC,aAAa,CAAC,+CAA+C,CAAC;IACjF,IAAI;IACJ,IAAI,MAAM,iBAAiB,GAAG;IAC9B,QAAQ,MAAM,IAAI,CAAC,aAAa,CAAC,+CAA+C,CAAC;IACjF,IAAI;IACJ,IAAI,MAAM,eAAe,GAAG;IAC5B,QAAQ,MAAM,IAAI,CAAC,aAAa,CAAC,+CAA+C,CAAC;IACjF,IAAI;IACJ;;;;;;;;;;;"}
@@ -0,0 +1,120 @@
1
+ import Foundation
2
+ import UserNotifications
3
+
4
+ /// The notification service extension for an app that uses DataTalksPush. It attaches a Data Talks push's picture (`dt_image`) and
5
+ /// reports its delivery immediately, which only an extension can do since the app may not be running. The app's extension target is
6
+ /// this class and nothing more:
7
+ ///
8
+ /// ```swift
9
+ /// import DataTalksPushExtension
10
+ ///
11
+ /// class NotificationService: DataTalksNotificationService {}
12
+ /// ```
13
+ ///
14
+ /// Delivery receipts also need an App Group shared by the app and the extension, named in both Info.plists as `DataTalksAppGroup`.
15
+ /// Without it the picture still shows, and the service counts a push as delivered once it's opened. Other pushes pass through unchanged.
16
+ open class DataTalksNotificationService: UNNotificationServiceExtension {
17
+ // Guarded by `lock`: set when a push arrives, read when it's shown, which either the finished work or the time limit can trigger.
18
+ private let lock = NSLock()
19
+ private var contentHandler: ((UNNotificationContent) -> Void)?
20
+ private var original: UNNotificationContent?
21
+ // The content to show. The picture is attached as soon as it downloads, so a push shown on timeout still has it.
22
+ private var bestAttempt: UNMutableNotificationContent?
23
+
24
+ // Internal so that tests can replace it.
25
+ var environment = ExtensionEnvironment.live
26
+
27
+ override public init() {
28
+ super.init()
29
+ }
30
+
31
+ override open func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
32
+ let userInfo = request.content.userInfo
33
+ let content = userInfo["dt_v"] == nil ? nil : request.content.mutableCopy() as? UNMutableNotificationContent
34
+
35
+ lock.lock()
36
+ self.contentHandler = contentHandler
37
+ original = request.content
38
+ bestAttempt = content
39
+ lock.unlock()
40
+
41
+ guard content != nil else {
42
+ finish(with: request.content)
43
+ return
44
+ }
45
+
46
+ let work = DispatchGroup()
47
+
48
+ if let uid = userInfo["dt_uid"] as? String, !uid.isEmpty, let shared = environment.shared() {
49
+ let state = shared.state()
50
+ if let delivery = shared.recordDelivery(uid: uid, at: environment.now(), installId: state?.installId), let state {
51
+ work.enter()
52
+ DeliveryReporter(session: environment.session).report(delivery, state: state) { answered in
53
+ if answered { shared.forget(delivery) }
54
+ work.leave()
55
+ }
56
+ }
57
+ }
58
+
59
+ if let url = PushAttachment.url(userInfo["dt_image"] as? String) {
60
+ work.enter()
61
+ PushAttachment.download(url, session: environment.session) { [self] attachment in
62
+ if let attachment { attach(attachment) }
63
+ work.leave()
64
+ }
65
+ }
66
+
67
+ work.notify(queue: .global()) { [self] in
68
+ if let shown = contentSoFar { finish(with: shown) }
69
+ }
70
+ }
71
+
72
+ /// Time is up: shows the push with whatever has arrived. An unacknowledged delivery stays in the inbox for the app to send.
73
+ override open func serviceExtensionTimeWillExpire() {
74
+ if let shown = contentSoFar { finish(with: shown) }
75
+ }
76
+
77
+ /// The content as it would show now.
78
+ var contentSoFar: UNNotificationContent? {
79
+ lock.lock()
80
+ defer { lock.unlock() }
81
+ return bestAttempt ?? original
82
+ }
83
+
84
+ // Only before the push is shown: after that the system owns the content.
85
+ private func attach(_ attachment: UNNotificationAttachment) {
86
+ lock.lock()
87
+ defer { lock.unlock() }
88
+ if contentHandler != nil { bestAttempt?.attachments = [attachment] }
89
+ }
90
+
91
+ // Calls the content handler once only.
92
+ private func finish(with content: UNNotificationContent) {
93
+ lock.lock()
94
+ let handler = contentHandler
95
+ contentHandler = nil
96
+ lock.unlock()
97
+
98
+ handler?(content)
99
+ }
100
+ }
101
+
102
+ /// The extension's outside dependencies, replaceable in tests.
103
+ struct ExtensionEnvironment {
104
+ var shared: () -> ExtensionShared?
105
+ var session: URLSession
106
+ var now: () -> TimeInterval
107
+
108
+ static let live = ExtensionEnvironment(
109
+ shared: { ExtensionShared.forExtension() },
110
+ session: URLSession(configuration: liveConfiguration),
111
+ now: { Date().timeIntervalSince1970 }
112
+ )
113
+
114
+ /// A request's own timeout only covers idle time; this caps each call's total time, well within the extension's 30 seconds.
115
+ static var liveConfiguration: URLSessionConfiguration {
116
+ let configuration = URLSessionConfiguration.ephemeral
117
+ configuration.timeoutIntervalForResource = PushAttachment.downloadTimeout
118
+ return configuration
119
+ }
120
+ }