Compare commits
23 Commits
edit-proj-
...
notify-api
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b9f19d3898 | ||
|
|
24957e0c6f | ||
|
|
8ba84888ee | ||
|
|
230dc52974 | ||
|
|
2c8aa21fa5 | ||
|
|
9f44a53047 | ||
|
|
c9ea2e4120 | ||
|
|
43c9b95c14 | ||
|
|
f4ee507918 | ||
|
|
0ebad3b497 | ||
|
|
aaee3bbbd2 | ||
|
|
d4cdee0698 | ||
|
|
178dcec5b8 | ||
|
|
e121db5fcf | ||
|
|
1389a166fa | ||
|
|
3c262c9eeb | ||
|
|
e155e55e49 | ||
|
|
263b12c37e | ||
|
|
1df47f17c4 | ||
|
|
6f066a7e23 | ||
|
|
9a23e2beba | ||
| 8ac6dd6ce0 | |||
| c0678385df |
@@ -1419,8 +1419,8 @@ The recommended way to build for Android is using the automated build script:
|
||||
##### 1. Bump the version in package.json, then update these versions & run:
|
||||
|
||||
```bash
|
||||
perl -p -i -e 's/versionCode .*/versionCode 65/g' android/app/build.gradle
|
||||
perl -p -i -e 's/versionName .*/versionName "1.3.8"/g' android/app/build.gradle
|
||||
perl -p -i -e 's/versionCode .*/versionCode 66/g' android/app/build.gradle
|
||||
perl -p -i -e 's/versionName .*/versionName "1.4.1"/g' android/app/build.gradle
|
||||
```
|
||||
|
||||
##### 2. Build
|
||||
|
||||
@@ -37,8 +37,8 @@ android {
|
||||
applicationId "app.timesafari.app"
|
||||
minSdkVersion rootProject.ext.minSdkVersion
|
||||
targetSdkVersion rootProject.ext.targetSdkVersion
|
||||
versionCode 65
|
||||
versionName "1.3.8"
|
||||
versionCode 66
|
||||
versionName "1.4.1"
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
aaptOptions {
|
||||
// Files and dirs to omit from the packaged assets dir, modified to accommodate modern web apps.
|
||||
|
||||
@@ -70,6 +70,10 @@ public class MainActivity extends BridgeActivity {
|
||||
// Register DailyNotification plugin
|
||||
// Plugin is written in Kotlin but compiles to Java-compatible bytecode
|
||||
registerPlugin(org.timesafari.dailynotification.DailyNotificationPlugin.class);
|
||||
|
||||
// Register native content fetcher for API-driven daily notifications (Endorser.ch)
|
||||
org.timesafari.dailynotification.DailyNotificationPlugin.setNativeFetcher(
|
||||
new TimeSafariNativeFetcher(this));
|
||||
|
||||
// Initialize SQLite
|
||||
//registerPlugin(SQLite.class);
|
||||
|
||||
@@ -1,73 +1,395 @@
|
||||
package app.timesafari;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
|
||||
import org.timesafari.dailynotification.FetchContext;
|
||||
import org.timesafari.dailynotification.NativeNotificationContentFetcher;
|
||||
import org.timesafari.dailynotification.NotificationContent;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
* Native content fetcher for API-driven daily notifications.
|
||||
* Calls Endorser.ch plansLastUpdatedBetween with configured credentials and
|
||||
* starred plan IDs (from plugin's updateStarredPlans), then returns notification content.
|
||||
*/
|
||||
public class TimeSafariNativeFetcher implements NativeNotificationContentFetcher {
|
||||
|
||||
private static final String TAG = "TimeSafariNativeFetcher";
|
||||
private final Context context;
|
||||
private static final String ENDORSER_ENDPOINT = "/api/v2/report/plansLastUpdatedBetween";
|
||||
private static final int CONNECT_TIMEOUT_MS = 10000;
|
||||
private static final int READ_TIMEOUT_MS = 15000;
|
||||
private static final int MAX_RETRIES = 3;
|
||||
/** Max chars of response body logged at DEBUG (avoids huge log lines). */
|
||||
private static final int MAX_RESPONSE_BODY_LOG_CHARS = 4096;
|
||||
private static final int RETRY_DELAY_MS = 1000;
|
||||
|
||||
// Must match plugin's SharedPreferences name and keys (DailyNotificationPlugin / TimeSafariIntegrationManager)
|
||||
private static final String PREFS_NAME = "daily_notification_timesafari";
|
||||
private static final String KEY_STARRED_PLAN_IDS = "starredPlanIds";
|
||||
private static final String KEY_LAST_ACKED_JWT_ID = "last_acked_jwt_id";
|
||||
|
||||
private final Gson gson = new Gson();
|
||||
private final Context appContext;
|
||||
private final SharedPreferences prefs;
|
||||
|
||||
// Configuration from TypeScript (set via configure())
|
||||
private volatile String apiBaseUrl;
|
||||
private volatile String activeDid;
|
||||
private volatile String jwtToken;
|
||||
/** Distinct JWTs from configureNativeFetcher `jwtTokens`; null = use jwtToken only. */
|
||||
@Nullable
|
||||
private List<String> jwtTokenPool;
|
||||
|
||||
public TimeSafariNativeFetcher(Context context) {
|
||||
this.context = context;
|
||||
this.appContext = context.getApplicationContext();
|
||||
this.prefs = appContext.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(String apiBaseUrl, String activeDid, String jwtToken) {
|
||||
configure(apiBaseUrl, activeDid, jwtToken, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(
|
||||
String apiBaseUrl,
|
||||
String activeDid,
|
||||
String jwtToken,
|
||||
@Nullable List<String> jwtTokenPool) {
|
||||
this.apiBaseUrl = apiBaseUrl;
|
||||
this.activeDid = activeDid;
|
||||
this.jwtToken = jwtToken;
|
||||
Log.i(TAG, "Fetcher configured with API: " + apiBaseUrl + ", DID: " + activeDid);
|
||||
this.jwtTokenPool =
|
||||
jwtTokenPool != null && !jwtTokenPool.isEmpty()
|
||||
? new ArrayList<>(jwtTokenPool)
|
||||
: null;
|
||||
int starredCount = getStarredPlanIds().size();
|
||||
Log.i(
|
||||
TAG,
|
||||
"Configured with API: "
|
||||
+ apiBaseUrl
|
||||
+ ", starredPlanIds count="
|
||||
+ starredCount
|
||||
+ (this.jwtTokenPool != null
|
||||
? ", jwtPoolSize=" + this.jwtTokenPool.size()
|
||||
: ""));
|
||||
}
|
||||
|
||||
/** One pool entry per UTC day (epoch day mod pool size); else primary jwtToken. */
|
||||
private String selectBearerTokenForRequest() {
|
||||
List<String> pool = jwtTokenPool;
|
||||
if (pool == null || pool.isEmpty()) {
|
||||
return jwtToken;
|
||||
}
|
||||
long epochDay = System.currentTimeMillis() / (24L * 60 * 60 * 1000);
|
||||
int idx = (int) (epochDay % pool.size());
|
||||
String t = pool.get(idx);
|
||||
if (t == null || t.isEmpty()) {
|
||||
return jwtToken;
|
||||
}
|
||||
Log.i(TAG, "Bearer from JWT pool: index=" + idx + " of " + pool.size());
|
||||
return t;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public CompletableFuture<List<NotificationContent>> fetchContent(@NonNull FetchContext fetchContext) {
|
||||
Long scheduled = fetchContext.scheduledTime;
|
||||
Log.i(
|
||||
TAG,
|
||||
"fetchContent START trigger="
|
||||
+ fetchContext.trigger
|
||||
+ " scheduledTime="
|
||||
+ (scheduled != null ? scheduled : "null")
|
||||
+ " callerThread="
|
||||
+ Thread.currentThread().getName());
|
||||
Log.d(TAG, "Fetching notification content, trigger: " + fetchContext.trigger);
|
||||
return fetchContentWithRetry(fetchContext, 0);
|
||||
}
|
||||
|
||||
private CompletableFuture<List<NotificationContent>> fetchContentWithRetry(
|
||||
@NonNull FetchContext context, int retryCount) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
// TODO: Implement actual content fetching for TimeSafari
|
||||
// This should query the TimeSafari API for notification content
|
||||
// using the configured apiBaseUrl, activeDid, and jwtToken
|
||||
Log.i(TAG, "fetchContent worker thread=" + Thread.currentThread().getName());
|
||||
String bearer = selectBearerTokenForRequest();
|
||||
if (apiBaseUrl == null || activeDid == null || bearer == null || bearer.isEmpty()) {
|
||||
Log.e(TAG, "Not configured. Call configureNativeFetcher() from TypeScript first.");
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
// For now, return a placeholder notification
|
||||
long scheduledTime = fetchContext.scheduledTime != null
|
||||
? fetchContext.scheduledTime
|
||||
: System.currentTimeMillis() + 60000; // 1 minute from now
|
||||
String urlString = apiBaseUrl + ENDORSER_ENDPOINT;
|
||||
URL url = new URL(urlString);
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setConnectTimeout(CONNECT_TIMEOUT_MS);
|
||||
connection.setReadTimeout(READ_TIMEOUT_MS);
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setRequestProperty("Content-Type", "application/json");
|
||||
connection.setRequestProperty("Authorization", "Bearer " + bearer);
|
||||
connection.setDoOutput(true);
|
||||
|
||||
NotificationContent content = new NotificationContent(
|
||||
"TimeSafari Update",
|
||||
"Check your starred projects for updates!",
|
||||
scheduledTime
|
||||
);
|
||||
Map<String, Object> requestBody = new HashMap<>();
|
||||
List<String> planIds = getStarredPlanIds();
|
||||
requestBody.put("planIds", planIds);
|
||||
String afterId = getLastAcknowledgedJwtId();
|
||||
if (afterId == null || afterId.isEmpty()) {
|
||||
afterId = "0";
|
||||
}
|
||||
requestBody.put("afterId", afterId);
|
||||
Log.i(
|
||||
TAG,
|
||||
"POST "
|
||||
+ ENDORSER_ENDPOINT
|
||||
+ " planCount="
|
||||
+ planIds.size()
|
||||
+ " afterId="
|
||||
+ (afterId.length() > 12 ? afterId.substring(0, 12) + "…" : afterId));
|
||||
|
||||
List<NotificationContent> results = new ArrayList<>();
|
||||
results.add(content);
|
||||
String jsonBody = gson.toJson(requestBody);
|
||||
try (OutputStream os = connection.getOutputStream()) {
|
||||
byte[] input = jsonBody.getBytes(StandardCharsets.UTF_8);
|
||||
os.write(input, 0, input.length);
|
||||
}
|
||||
|
||||
Log.d(TAG, "Returning " + results.size() + " notification(s)");
|
||||
return results;
|
||||
int responseCode = connection.getResponseCode();
|
||||
Log.i(TAG, "HTTP response code: " + responseCode);
|
||||
|
||||
if (responseCode == 200) {
|
||||
StringBuilder response = new StringBuilder();
|
||||
try (BufferedReader reader = new BufferedReader(
|
||||
new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
response.append(line);
|
||||
}
|
||||
}
|
||||
String responseBody = response.toString();
|
||||
String snippet =
|
||||
responseBody.length() <= MAX_RESPONSE_BODY_LOG_CHARS
|
||||
? responseBody
|
||||
: responseBody.substring(0, MAX_RESPONSE_BODY_LOG_CHARS) + "…";
|
||||
Log.d(
|
||||
TAG,
|
||||
"plansLastUpdatedBetween response len="
|
||||
+ responseBody.length()
|
||||
+ " body="
|
||||
+ snippet);
|
||||
List<NotificationContent> contents = parseApiResponse(responseBody, context);
|
||||
if (!contents.isEmpty()) {
|
||||
updateLastAckedJwtIdFromResponse(responseBody);
|
||||
}
|
||||
Log.i(TAG, "Fetched " + contents.size() + " notification(s)");
|
||||
return contents;
|
||||
}
|
||||
|
||||
if (retryCount < MAX_RETRIES && (responseCode >= 500 || responseCode == 429)) {
|
||||
int delayMs = RETRY_DELAY_MS * (1 << retryCount);
|
||||
String errBody = readHttpErrorBodySnippet(connection);
|
||||
Log.w(
|
||||
TAG,
|
||||
"Retryable error "
|
||||
+ responseCode
|
||||
+ (errBody.isEmpty() ? "" : " body: " + errBody)
|
||||
+ ", retrying in "
|
||||
+ delayMs
|
||||
+ "ms");
|
||||
try {
|
||||
Thread.sleep(delayMs);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return fetchContentWithRetry(context, retryCount + 1).join();
|
||||
}
|
||||
|
||||
String errBody = readHttpErrorBodySnippet(connection);
|
||||
if (errBody.isEmpty()) {
|
||||
Log.e(TAG, "API error " + responseCode);
|
||||
} else {
|
||||
Log.e(TAG, "API error " + responseCode + " body: " + errBody);
|
||||
}
|
||||
return Collections.emptyList();
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Fetch failed", e);
|
||||
if (retryCount < MAX_RETRIES) {
|
||||
try {
|
||||
Thread.sleep(RETRY_DELAY_MS * (1 << retryCount));
|
||||
} catch (InterruptedException ie) {
|
||||
Thread.currentThread().interrupt();
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return fetchContentWithRetry(context, retryCount + 1).join();
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads error response body for logging (HttpURLConnection puts 4xx/5xx bodies on
|
||||
* {@link HttpURLConnection#getErrorStream()}).
|
||||
*/
|
||||
private static String readHttpErrorBodySnippet(HttpURLConnection connection) {
|
||||
InputStream stream = connection.getErrorStream();
|
||||
if (stream == null) {
|
||||
return "";
|
||||
}
|
||||
final int maxChars = 4096;
|
||||
try (BufferedReader reader =
|
||||
new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
if (sb.length() > 0) {
|
||||
sb.append('\n');
|
||||
}
|
||||
if (sb.length() + line.length() > maxChars) {
|
||||
sb.append(line, 0, Math.max(0, maxChars - sb.length()));
|
||||
sb.append("…");
|
||||
break;
|
||||
}
|
||||
sb.append(line);
|
||||
}
|
||||
return sb.toString().trim();
|
||||
} catch (IOException e) {
|
||||
return "(read error body failed: " + e.getMessage() + ")";
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> getStarredPlanIds() {
|
||||
try {
|
||||
String idsJson = prefs.getString(KEY_STARRED_PLAN_IDS, "[]");
|
||||
if (idsJson == null || idsJson.isEmpty() || "[]".equals(idsJson)) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
JsonArray arr = JsonParser.parseString(idsJson).getAsJsonArray();
|
||||
List<String> list = new ArrayList<>();
|
||||
for (int i = 0; i < arr.size(); i++) {
|
||||
list.add(arr.get(i).getAsString());
|
||||
}
|
||||
return list;
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Error loading starred plan IDs", e);
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
||||
private String getLastAcknowledgedJwtId() {
|
||||
return prefs.getString(KEY_LAST_ACKED_JWT_ID, null);
|
||||
}
|
||||
|
||||
private void updateLastAckedJwtIdFromResponse(String responseBody) {
|
||||
try {
|
||||
JsonObject root = JsonParser.parseString(responseBody).getAsJsonObject();
|
||||
if (!root.has("data")) return;
|
||||
JsonArray dataArray = root.getAsJsonArray("data");
|
||||
if (dataArray == null || dataArray.size() == 0) return;
|
||||
JsonObject lastItem = dataArray.get(dataArray.size() - 1).getAsJsonObject();
|
||||
String jwtId = null;
|
||||
if (lastItem.has("jwtId")) {
|
||||
jwtId = lastItem.get("jwtId").getAsString();
|
||||
} else if (lastItem.has("plan")) {
|
||||
JsonObject plan = lastItem.getAsJsonObject("plan");
|
||||
if (plan.has("jwtId")) {
|
||||
jwtId = plan.get("jwtId").getAsString();
|
||||
}
|
||||
}
|
||||
if (jwtId != null && !jwtId.isEmpty()) {
|
||||
prefs.edit().putString(KEY_LAST_ACKED_JWT_ID, jwtId).apply();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.w(TAG, "Could not extract JWT ID from response", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display title for a plansLastUpdatedBetween row; prefers {@code plan.name}, else "Unnamed Project".
|
||||
*/
|
||||
private String extractProjectDisplayTitle(JsonObject item) {
|
||||
if (item.has("plan")) {
|
||||
JsonObject plan = item.getAsJsonObject("plan");
|
||||
if (plan.has("name") && !plan.get("name").isJsonNull()) {
|
||||
String name = plan.get("name").getAsString();
|
||||
if (name != null && !name.trim().isEmpty()) {
|
||||
return name.trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
return "Unnamed Project";
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private String extractJwtIdFromItem(JsonObject item) {
|
||||
if (item.has("plan")) {
|
||||
JsonObject plan = item.getAsJsonObject("plan");
|
||||
if (plan.has("jwtId") && !plan.get("jwtId").isJsonNull()) {
|
||||
return plan.get("jwtId").getAsString();
|
||||
}
|
||||
}
|
||||
if (item.has("jwtId") && !item.get("jwtId").isJsonNull()) {
|
||||
return item.get("jwtId").getAsString();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private List<NotificationContent> parseApiResponse(String responseBody, FetchContext context) {
|
||||
List<NotificationContent> contents = new ArrayList<>();
|
||||
try {
|
||||
JsonObject root = JsonParser.parseString(responseBody).getAsJsonObject();
|
||||
JsonArray dataArray = root.has("data") ? root.getAsJsonArray("data") : null;
|
||||
if (dataArray == null || dataArray.size() == 0) {
|
||||
return contents;
|
||||
}
|
||||
|
||||
JsonObject firstItem = dataArray.get(0).getAsJsonObject();
|
||||
String firstTitle = extractProjectDisplayTitle(firstItem);
|
||||
String jwtId = extractJwtIdFromItem(firstItem);
|
||||
|
||||
NotificationContent content = new NotificationContent();
|
||||
content.setId("endorser_" + (jwtId != null ? jwtId : ("batch_" + System.currentTimeMillis())));
|
||||
int n = dataArray.size();
|
||||
String quotedFirst = "\u201C" + firstTitle + "\u201D";
|
||||
if (n == 1) {
|
||||
content.setTitle("Starred Project Update");
|
||||
content.setBody(quotedFirst + " has been updated.");
|
||||
} else {
|
||||
content.setTitle("Starred Project Updates");
|
||||
int more = n - 1;
|
||||
content.setBody(quotedFirst + " + " + more + " more have been updated.");
|
||||
}
|
||||
content.setScheduledTime(
|
||||
context.scheduledTime != null
|
||||
? context.scheduledTime
|
||||
: (System.currentTimeMillis() + 3600000));
|
||||
content.setPriority("default");
|
||||
content.setSound(true);
|
||||
contents.add(content);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Error parsing API response", e);
|
||||
}
|
||||
return contents;
|
||||
}
|
||||
}
|
||||
|
||||
129
doc/PLUGIN_NOTIFICATION_FIX_ANDROID.md
Normal file
129
doc/PLUGIN_NOTIFICATION_FIX_ANDROID.md
Normal file
@@ -0,0 +1,129 @@
|
||||
# Android plugin: New Activity notification when API has no activities
|
||||
|
||||
**Audience:** Maintainers of `@timesafari/daily-notification-plugin` (Android / Kotlin).
|
||||
**Host app:** TimeSafari (`crowd-funder-for-time-pwa`) — this file lives in the **app** repo only as a handoff; apply changes in the **plugin** repo.
|
||||
|
||||
**Problem (product):** “New Activity” should notify only when the API reports new/updated activity. The host’s native fetcher (`TimeSafariNativeFetcher`) returns an **empty** `List<NotificationContent>` when the API’s `data` array is empty. Users still see a **daily** local notification.
|
||||
|
||||
**Version note:** This diagnosis was first written against older plugin builds (e.g. **2.1.x / 2.2.x**). After upgrading the host to **`@timesafari/daily-notification-plugin` 3.0.0**, the Android files below were **re-read** from `node_modules`. The relevant logic is **unchanged** in 3.0.0: the same two mechanisms still explain unwanted daily notifications when the API returns no rows. If you maintain the plugin, re-verify after each major release.
|
||||
|
||||
**Root cause (Android, confirmed in plugin v3.0.0 sources under `node_modules`):** Two mechanisms interact:
|
||||
|
||||
1. **`FetchWorker.kt` — empty native fetch is converted to synthetic JSON instead of “skip”**
|
||||
When the dual prefetch runs with the native fetcher and the list is empty, `notificationContentsToDualPayloadBytes` **replaces** the empty list with a JSON payload `"No updates"` / `"No new content"`, and the work unit still completes successfully. The dual path then **always** arms the chained notify alarm when `isDual && nextNotifyAt > 0L` — so a notification is still scheduled for the notify window.
|
||||
|
||||
Reference (plugin):
|
||||
|
||||
```kotlin
|
||||
// FetchWorker.kt — notificationContentsToDualPayloadBytes (~371–374 in v3.0.0)
|
||||
if (contents.isEmpty()) {
|
||||
return """{"title":"No updates","body":"No new content"}""".toByteArray(Charsets.UTF_8)
|
||||
}
|
||||
```
|
||||
|
||||
```kotlin
|
||||
// FetchWorker.kt — doWork(), tail of success path (~306–309 in v3.0.0)
|
||||
if (isDual && nextNotifyAt > 0L) {
|
||||
DualScheduleNotifyScheduler.scheduleChainedNotifyAlarm(applicationContext, nextNotifyAt)
|
||||
DualScheduleFetchRecovery.enqueueFromPersistedConfig(applicationContext)
|
||||
}
|
||||
```
|
||||
|
||||
2. **`DualScheduleHelper.kt` — `fallbackBehavior: "show_default"` uses `userNotification` defaults**
|
||||
At display time, if there is **no** fresh dual-scope cache within `relationship.contentTimeout`, the helper falls back to the **persisted** `userNotification.title` / `userNotification.body` when `fallbackBehavior` is `"show_default"`. The host app sets those defaults to copy such as “New Activity” / “Check your starred projects…”, so the user sees that **even when the API had nothing**, if the cache path doesn’t supply something else.
|
||||
|
||||
Reference (plugin):
|
||||
|
||||
```kotlin
|
||||
// DualScheduleHelper.kt — resolveDualContentBlocking (simplified; ~31–57 in v3.0.0)
|
||||
val fallbackBehavior = relationship?.optString("fallbackBehavior", "show_default") ?: "show_default"
|
||||
val defaultTitle = userNotification.optString("title", "Daily Notification")
|
||||
val defaultBody = userNotification.optString("body", "Your daily update is ready")
|
||||
// ...
|
||||
} else {
|
||||
if (fallbackBehavior != "show_default") return null
|
||||
Pair(defaultTitle, defaultBody)
|
||||
}
|
||||
```
|
||||
|
||||
**TypeScript contract (plugin `src/definitions.ts` in v3.0.0 — `DualScheduleConfiguration.relationship`):**
|
||||
|
||||
```ts
|
||||
relationship?: {
|
||||
autoLink: boolean;
|
||||
contentTimeout: number;
|
||||
fallbackBehavior: 'skip' | 'show_default' | 'retry';
|
||||
};
|
||||
```
|
||||
|
||||
`skip` is only partially useful on Android **with the current fetch implementation**: it avoids the **default title/body** branch in `DualScheduleHelper` when cache is missing/stale, but it does **not** by itself stop a notification if the fetch path still materializes content (including the synthetic `"No updates"` payload) or if chained notify is already armed.
|
||||
|
||||
**3.0.0 vs 2.2.x:** Plugin **3.0.0** advertises broader features (e.g. TTL-at-fire, observability). Those do **not** replace the dual-fetch pipeline inspected here: `FetchWorker` still maps an empty native list to JSON and still schedules the chained notify on success; `DualScheduleHelper` still applies `show_default` vs defaults when cache is absent or outside `contentTimeout`. Revisit this doc if a future release changes `notificationContentsToDualPayloadBytes` or the dual notify gate.
|
||||
|
||||
---
|
||||
|
||||
## Recommended plugin changes (Android)
|
||||
|
||||
### 1) Treat empty native fetch as “no notification” (primary)
|
||||
|
||||
**File:** `android/src/main/java/org/timesafari/dailynotification/FetchWorker.kt`
|
||||
|
||||
**Issue:** `notificationContentsToDualPayloadBytes` must not turn an empty list into a non-empty payload if the product contract is “no rows in API → no notification.”
|
||||
|
||||
**Direction:**
|
||||
|
||||
- **Before:** Empty list → JSON `No updates` / `No new content` → success → chained notify scheduled.
|
||||
- **After (one of):**
|
||||
- **A)** Return a dedicated sentinel payload (e.g. `{ "skipNotification": true }`) and teach **`NotifyReceiver` / worker** that resolves dual content to **not post** when that sentinel is present; **or**
|
||||
- **B)** On empty list, **do not** call `DualScheduleNotifyScheduler.scheduleChainedNotifyAlarm` for this cycle (and optionally persist “last fetch had no content” for the helper); **or**
|
||||
- **C)** Store an empty/marker cache row that `DualScheduleHelper.resolveDualContentBlocking` interprets as “return null” (no notification).
|
||||
|
||||
Pick one strategy and keep behavior consistent with `relationship.fallbackBehavior`:
|
||||
|
||||
- If `fallbackBehavior == "skip"`: skip notification when fetch returns empty or when sentinel indicates skip.
|
||||
- If `fallbackBehavior == "show_default"`: keep current default-title/body behavior **only** when the product intends it (may be wrong for TimeSafari).
|
||||
|
||||
### 2) Honor `relationship.fallbackBehavior` end-to-end
|
||||
|
||||
**Files:** `FetchWorker.kt`, `DualScheduleHelper.kt`, any worker/receiver that posts the dual notification.
|
||||
|
||||
**Issue:** `DualScheduleHelper` reads `fallbackBehavior`, but the fetch path does not use the same semantics for “empty API result.”
|
||||
|
||||
**Direction:** When persisting dual config, pass `fallbackBehavior` into the fetch success path so that **empty fetch + `skip`** never schedules or displays a notification.
|
||||
|
||||
### 3) Tests
|
||||
|
||||
- Dual fetch + native fetcher returns **empty list** → **no** notification posted (or no chained alarm), matching host expectation.
|
||||
- Non-empty list → notification with fetcher-provided title/body.
|
||||
- Optional: `fallbackBehavior` matrix (`skip` / `show_default`) with stale cache vs fresh cache.
|
||||
|
||||
---
|
||||
|
||||
## Host app follow-up (separate PR in `crowd-funder-for-time-pwa`)
|
||||
|
||||
After the plugin implements empty-fetch semantics, set in `buildDualScheduleConfig` (`src/services/notifications/dualScheduleConfig.ts`):
|
||||
|
||||
```ts
|
||||
relationship: {
|
||||
autoLink: true,
|
||||
contentTimeout: 5 * 60 * 1000,
|
||||
fallbackBehavior: "skip", // was "show_default"
|
||||
},
|
||||
```
|
||||
|
||||
Only do this once Android behavior matches the contract (otherwise users may get **no** notification even when you would want defaults on network failure — product decision).
|
||||
|
||||
---
|
||||
|
||||
## References in this repo (context only)
|
||||
|
||||
- Host native fetcher returns no content when API `data` is empty: `android/app/src/main/java/app/timesafari/TimeSafariNativeFetcher.java` (`parseApiResponse`).
|
||||
- Host dual config today uses `fallbackBehavior: "show_default"`: `src/services/notifications/dualScheduleConfig.ts`.
|
||||
|
||||
---
|
||||
|
||||
## Plugin version verification
|
||||
|
||||
- **Last verified against:** `@timesafari/daily-notification-plugin` **3.0.0** (`node_modules/.../package.json`).
|
||||
- **Prior builds:** Behavior matched the earlier **2.1.x** analysis; **2.2.0 → 3.0.0** did not remove the empty-list → synthetic JSON mapping or the chained-notify success path in the inspected sources.
|
||||
- Re-verify line numbers after rebasing or patching the plugin repo.
|
||||
152
doc/endorser-jwt-background-prefetch-options.md
Normal file
152
doc/endorser-jwt-background-prefetch-options.md
Normal file
@@ -0,0 +1,152 @@
|
||||
# Options: expired JWT during background “New Activity” prefetch (mobile)
|
||||
|
||||
**Date:** 2026-03-26 17:29 PST
|
||||
**Audience:** TimeSafari / crowd-funder team; **Endorser server** maintainers (auth + API policy)
|
||||
**Context:** Android Capacitor app, `POST /api/v2/report/plansLastUpdatedBetween`, native `TimeSafariNativeFetcher` invoked from WorkManager at **T−5 minutes** before the daily notification.
|
||||
|
||||
---
|
||||
|
||||
## Problem (short)
|
||||
|
||||
New Activity notifications prefetch Endorser data in **background** (no JavaScript, no WebView). The HTTP client uses a **Bearer JWT** supplied earlier via `configureNativeFetcher` / `getHeaders(activeDid)`.
|
||||
|
||||
If the **access token’s `exp`** is **before** prefetch time, the API returns **400** with a body like:
|
||||
|
||||
```json
|
||||
{
|
||||
"error": {
|
||||
"message": "JWT failed verification: ... JWT has expired: exp: … < now: …",
|
||||
"code": "JWT_VERIFY_FAILED"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
We **cannot** rely on the user opening the app immediately before prefetch (T−5), so **client-only** mitigations (e.g. refresh JWT on app resume) **reduce** failures but **do not guarantee** a valid token for headless background work.
|
||||
|
||||
---
|
||||
|
||||
## Why this is different from normal in-app API calls
|
||||
|
||||
| In-app | Background prefetch |
|
||||
|--------|----------------------|
|
||||
| `getHeaders()` runs in JS when needed; user often recently active | WorkManager runs **without** Capacitor / passkey / session refresh |
|
||||
| Short TTL tokens are refreshed as the user uses the app | Same token may sit in native memory until **T−5** (or longer) |
|
||||
|
||||
So **server-side** and **architecture** choices matter for this feature.
|
||||
|
||||
---
|
||||
|
||||
## Options (for decision)
|
||||
|
||||
### 1. Increase access token TTL (Endorser / IdP)
|
||||
|
||||
**Idea:** Issue access JWTs with a longer `exp` so that **configure time → prefetch time** (often **5+ minutes**, sometimes **24h+** if the user rarely opens the app) usually still falls inside validity.
|
||||
|
||||
| Pros | Cons |
|
||||
|------|------|
|
||||
| Simple to explain; one policy change | Longer-lived bearer tokens increase risk if exfiltrated; mitigate with scope, rotation, monitoring |
|
||||
| No client protocol change | May not fit strict security posture without a dedicated scope |
|
||||
|
||||
**Endorser owner:** token lifetime, scopes, and whether a **dedicated** lifetime or scope for “mobile background read” is acceptable.
|
||||
|
||||
---
|
||||
|
||||
### 2. Scoped long-lived token for report reads only (Endorser)
|
||||
|
||||
**Idea:** Mint a **separate** access token (or sub-scope) valid only for **read-only report** endpoints (`plansLastUpdatedBetween`, etc.), with a **longer TTL** than the interactive session token.
|
||||
|
||||
| Pros | Cons |
|
||||
|------|------|
|
||||
| Limits blast radius vs “longer JWT for everything” | Requires auth model + issuance path; client must store/use this token only for prefetch |
|
||||
|
||||
**Endorser owner:** feasibility of **narrow scope** + **longer TTL** for this use case.
|
||||
|
||||
---
|
||||
|
||||
### 3. Refresh token or device grant (Endorser + mobile native)
|
||||
|
||||
**Idea:** Client stores a **refresh token** (or OAuth **device** grant) in **Android Keystore / iOS Keychain**. Before `plansLastUpdatedBetween`, **native** code (no JS) exchanges it for a **new access token**.
|
||||
|
||||
| Pros | Cons |
|
||||
|------|------|
|
||||
| Standard pattern; short TTL for access tokens remains | Endorser must support refresh (or equivalent); secure storage + rotation; **both** client and server work |
|
||||
| Works when app is backgrounded for days | Implementation cost on mobile |
|
||||
|
||||
**Endorser owner:** refresh endpoint, token rotation, revocation.
|
||||
**Mobile owner:** native fetch path, secure storage, failure handling.
|
||||
|
||||
---
|
||||
|
||||
### 4. Backend proxy / BFF (TimeSafari backend + Endorser)
|
||||
|
||||
**Idea:** Phone calls **your** backend with a **device session** (or FCM registration id); **server** uses **server-to-server** credentials or a **service account** to call Endorser. The device **never** sends an Endorser JWT for this path.
|
||||
|
||||
| Pros | Cons |
|
||||
|------|------|
|
||||
| No Endorser JWT lifetime problem on device | New service, auth, rate limits, privacy review |
|
||||
| Central place for logging, abuse control | Operational cost |
|
||||
|
||||
**Endorser owner:** partner / S2S auth model for the BFF.
|
||||
**Product team:** hosting and trust boundaries.
|
||||
|
||||
---
|
||||
|
||||
### 5. “Cron” or periodic jobs on the device to refresh JWT (JS)
|
||||
|
||||
**Idea:** Use something like a **cron** schedule to refresh tokens.
|
||||
|
||||
**Reality:** Scheduled **native** jobs can run, but **Capacitor / `getHeaders()` / passkey** do **not** run reliably in that context without waking the **WebView**. So **“cron”** only helps if refresh is **fully native** (see option 3) or you accept **unreliable** wake + JS.
|
||||
|
||||
**Not recommended** as the primary fix unless paired with **native refresh** or **server** changes.
|
||||
|
||||
---
|
||||
|
||||
### 6. Product / UX constraints (no server change)
|
||||
|
||||
**Idea:** Accept that **headless** API calls may fail if the session is stale; show **fallback** copy; or require “open app once per day” for best results.
|
||||
|
||||
| Pros | Cons |
|
||||
|------|------|
|
||||
| No Endorser change | Does not meet “API-driven notification” expectation for inactive users |
|
||||
|
||||
---
|
||||
|
||||
## Client-side mitigations already in play (not sufficient alone)
|
||||
|
||||
- **`configureNativeFetcherIfReady()`** after startup and when **Account** / identity is ready.
|
||||
- **`appStateChange` → `isActive`:** refresh native fetcher when the app returns to foreground (reduces staleness when the user **does** open the app).
|
||||
- **Error logging** of 400 bodies for diagnosis.
|
||||
|
||||
These **do not** guarantee a fresh JWT at **T−5** if the user never opens the app before prefetch.
|
||||
|
||||
---
|
||||
|
||||
## Suggested decision order
|
||||
|
||||
1. **Align on security posture:** Is a **longer TTL** or **scoped long-lived read token** acceptable for Endorser?
|
||||
2. If not, is **refresh token in native** (option 3) or **BFF** (option 4) on the roadmap?
|
||||
3. **Parallel:** UX fallback when API is unavailable (option 6) so the app never silently looks “broken.”
|
||||
|
||||
---
|
||||
|
||||
## References (this repo)
|
||||
|
||||
| Topic | Location |
|
||||
|--------|----------|
|
||||
| Native fetcher + JWT from `getHeaders` | `src/services/notifications/nativeFetcherConfig.ts` |
|
||||
| Android POST + errors | `android/app/src/main/java/app/timesafari/TimeSafariNativeFetcher.java` |
|
||||
| Web `plansLastUpdatedBetween` + `afterId` | `src/libs/endorserServer.ts` (`getStarredProjectsWithChanges`) |
|
||||
| New Activity / dual schedule | `doc/notification-from-api-call.md`, `doc/plugin-feedback-android-dual-schedule-native-fetch-and-timing.md` |
|
||||
|
||||
---
|
||||
|
||||
## Open questions for Endorser (server developer)
|
||||
|
||||
1. What is the **current access token TTL** and can it be **increased** for mobile clients, or **per-scope**?
|
||||
2. Is **refresh token** (or similar) available for **non-interactive** renewal?
|
||||
3. Would a **read-only** scope for `plansLastUpdatedBetween` with a **longer** lifetime be acceptable?
|
||||
4. Is there an existing **server-to-server** or **partner** path that a **BFF** could use instead of user JWT on device?
|
||||
|
||||
---
|
||||
|
||||
*This document is for internal planning and decision; update it when the team chooses an approach.*
|
||||
103
doc/notification-from-api-call.md
Normal file
103
doc/notification-from-api-call.md
Normal file
@@ -0,0 +1,103 @@
|
||||
# New Activity Notification (API-Driven Daily Message)
|
||||
|
||||
**Purpose:** Integrate the daily-notification-plugin’s second feature—the **daily, API-driven message**—into the crowd-funder (TimeSafari) app. The first feature (daily static reminder) is already integrated; this document covers the plan, completed work, and remaining tasks for the API-driven flow.
|
||||
|
||||
**References:**
|
||||
|
||||
- Plugin: `daily-notification-plugin` (INTEGRATION_GUIDE.md, definitions.ts)
|
||||
- Alignment outline: `doc/daily-notification-alignment-outline.md`
|
||||
- Help copy: `HelpNotificationTypesView.vue` (“New Activity Notifications”)
|
||||
|
||||
---
|
||||
|
||||
## Plan Summary
|
||||
|
||||
The API-driven flow:
|
||||
|
||||
1. **Prefetch** – Shortly before the user’s chosen time, the plugin runs a background job that calls the Endorser.ch API (e.g. `plansLastUpdatedBetween`, and optionally offers endpoints) using credentials supplied by the app.
|
||||
2. **Cache** – Fetched content is stored in the plugin’s cache.
|
||||
3. **Notify** – At the chosen time, the user sees a notification whose title/body come from that content (or a fallback).
|
||||
|
||||
The app must:
|
||||
|
||||
- **Configure the native fetcher** with `apiBaseUrl`, `activeDid`, and a JWT so the plugin’s background workers can call the API.
|
||||
- **Implement the native fetcher** (or register an implementation) so the plugin can perform the actual HTTP requests and parse responses into notification content.
|
||||
- **Sync starred plan IDs** to the plugin via `updateStarredPlans` so the fetcher knows which plans to query.
|
||||
- **Expose UI** to enable/disable the “New Activity” notification and choose a time, and call `scheduleDualNotification` / `cancelDualSchedule` accordingly.
|
||||
|
||||
---
|
||||
|
||||
## Tasks Finished
|
||||
|
||||
- **Configure native fetcher on startup and identity**
|
||||
- Added `configureNativeFetcherIfReady()` in `src/services/notifications/nativeFetcherConfig.ts` (reads `activeDid` and `apiServer` from DB, gets JWT via `getHeaders(did)`, calls `DailyNotification.configureNativeFetcher()`).
|
||||
- Called from `main.capacitor.ts` after the 2s delay (with deep link registration).
|
||||
- Called from `AccountViewView.initializeState()` when on native and `activeDid` is set; when New Activity is enabled, also calls `updateStarredPlans(settings.starredPlanHandleIds)`.
|
||||
- **Implement real API calls in Android native fetcher**
|
||||
- `android/app/src/main/java/app/timesafari/TimeSafariNativeFetcher.java` implements `NativeNotificationContentFetcher`: POST to `/api/v2/report/plansLastUpdatedBetween` with `planIds` (from SharedPreferences `daily_notification_timesafari` / `starredPlanIds`) and `afterId`; when `data` is non-empty, builds **one** aggregated `NotificationContent` (title **Starred Project Update** or **Starred Project Updates**, body from `plan.name` with typographic quotes, then `has been updated.` or `+ N more have been updated.`); when `data` is empty, returns an empty list (no “no updates” notification); updates `last_acked_jwt_id` for pagination when content is returned.
|
||||
- Registered in `MainActivity.onCreate()` via `DailyNotificationPlugin.setNativeFetcher(new TimeSafariNativeFetcher(this))`.
|
||||
- **Sync starred plan IDs**
|
||||
- Shared helper `syncStarredPlansToNativePlugin(planIds)` in `src/services/notifications/syncStarredPlansToNativePlugin.ts` (exported from `src/services/notifications/index.ts`) calls `DailyNotification.updateStarredPlans` on native only; ignores `UNIMPLEMENTED`.
|
||||
- When user enables New Activity, `scheduleNewActivityDualNotification()` uses the helper with `settings.starredPlanHandleIds ?? []`.
|
||||
- When Account view loads and New Activity is on, `initializeState()` uses the helper with the same list.
|
||||
- When the user stars or unstars on a project (`ProjectViewView.toggleStar`), after a successful settings save, the helper runs if `notifyingNewActivityTime` is set so prefetch sees the current list without reopening Account.
|
||||
- **Dual schedule config and scheduling**
|
||||
- Added `src/services/notifications/dualScheduleConfig.ts`: `timeToCron()`, `timeToCronFiveMinutesBefore()`, `buildDualScheduleConfig({ notifyTime, title?, body? })` (contentFetch 5 min before, userNotification at chosen time).
|
||||
- When user enables New Activity and picks a time, app calls `DailyNotification.scheduleDualNotification({ config })` with this config.
|
||||
- When user disables New Activity, app calls `DailyNotification.cancelDualSchedule()`.
|
||||
- **UI for New Activity notification**
|
||||
- Unhid the “New Activity Notification” block in `AccountViewView.vue` (toggle + accessibility).
|
||||
- Enable flow: time dialog → save settings → on native, `scheduleNewActivityDualNotification(timeText)` (configure fetcher, updateStarredPlans, scheduleDualNotification).
|
||||
- Disable flow: on native, `cancelDualSchedule()` then save and clear settings.
|
||||
- Added `starredPlanHandleIds` to `AccountSettings` in `interfaces/accountView.ts`.
|
||||
- **Exports**
|
||||
- `src/services/notifications/index.ts` exports `configureNativeFetcherIfReady`, `syncStarredPlansToNativePlugin`, `buildDualScheduleConfig`, `timeToCron`, `timeToCronFiveMinutesBefore`, and `DualScheduleConfigInput`.
|
||||
|
||||
---
|
||||
|
||||
## Checklist of Remaining Tasks
|
||||
|
||||
### iOS
|
||||
|
||||
- **Confirm iOS native fetcher / dual schedule**
|
||||
Plugin exposes `configureNativeFetcher` on iOS. Confirm whether the plugin expects an iOS-specific native fetcher registration (similar to Android’s `setNativeFetcher`) and, if so, register a TimeSafari fetcher implementation for iOS so API-driven notifications work on iPhone.
|
||||
- **Verify dual schedule on iOS**
|
||||
Test `scheduleDualNotification` and `cancelDualSchedule` on iOS; ensure content fetch and user notification fire at the expected times and that foreground/background behavior matches expectations.
|
||||
|
||||
### Testing and hardening
|
||||
|
||||
- **Test full flow on Android**
|
||||
Enable New Activity, set time, wait for prefetch and notification (or use a short rollover for testing). Confirm notification shows with API-derived or fallback content.
|
||||
- **Test full flow on iOS**
|
||||
Same as Android: enable, set time, verify prefetch and notification delivery and content.
|
||||
- **Test with no starred plans**
|
||||
Enable New Activity with empty `starredPlanHandleIds`; confirm no crash; the native fetcher returns no Endorser-derived items when there is nothing to query or no new rows (see `TimeSafariNativeFetcher`).
|
||||
- **Test JWT expiry**
|
||||
Ensure behavior when the token passed to `configureNativeFetcher` has expired (e.g. app in background for a long time); document or implement refresh (e.g. re-call `configureNativeFetcherIfReady` on foreground or when opening Account).
|
||||
|
||||
### Optional enhancements
|
||||
|
||||
- **Offers endpoints**
|
||||
Extend `TimeSafariNativeFetcher` (and any iOS fetcher) to call offers endpoints (e.g. `offers`, `offersToPlansOwnedByMe`) and merge with project-update content for richer notifications.
|
||||
- **Documentation**
|
||||
Add a short “New Activity notifications” section to BUILDING.md or a user-facing help page describing how the feature works and how to troubleshoot (e.g. no notification, wrong content, JWT/API errors).
|
||||
|
||||
---
|
||||
|
||||
## File Reference
|
||||
|
||||
|
||||
| Area | Files |
|
||||
| ---------------------- | ----------------------------------------------------------------------- |
|
||||
| Fetcher config | `src/services/notifications/nativeFetcherConfig.ts` |
|
||||
| Starred list → plugin | `src/services/notifications/syncStarredPlansToNativePlugin.ts` |
|
||||
| Dual schedule config | `src/services/notifications/dualScheduleConfig.ts` |
|
||||
| Notification exports | `src/services/notifications/index.ts` |
|
||||
| Startup | `src/main.capacitor.ts` |
|
||||
| Account UI and flow | `src/views/AccountViewView.vue` |
|
||||
| Project star / unstar | `src/views/ProjectViewView.vue` (`toggleStar`) |
|
||||
| Settings type | `src/interfaces/accountView.ts` |
|
||||
| Android native fetcher | `android/app/src/main/java/app/timesafari/TimeSafariNativeFetcher.java` |
|
||||
| Android registration | `android/app/src/main/java/app/timesafari/MainActivity.java` |
|
||||
|
||||
|
||||
250
doc/notification-new-activity-lay-of-the-land.md
Normal file
250
doc/notification-new-activity-lay-of-the-land.md
Normal file
@@ -0,0 +1,250 @@
|
||||
# Lay of the Land: API-Driven Daily Message (New Activity) and Web-Push Confusion
|
||||
|
||||
**Purpose:** Shareable analysis of the New Activity (API-driven daily message) implementation and the root cause of “always fires / can’t be turned off.” For discussion with teammates.
|
||||
|
||||
**Related:** `doc/notification-from-api-call.md` (plan and progress), teammate note about web-push confusion and possibly removing that logic.
|
||||
|
||||
---
|
||||
|
||||
## 1. Two Separate Notification Features
|
||||
|
||||
There are **two** distinct native notification flows that both go through the same UI component:
|
||||
|
||||
| Feature | Plugin API | Purpose |
|
||||
|--------|------------|--------|
|
||||
| **Daily Reminder** | `scheduleDailyNotification` / `cancelDailyReminder` | Single daily alarm, static title/body (user’s message). |
|
||||
| **New Activity** (API-driven) | `scheduleDualNotification` / `cancelDualSchedule` | Prefetch from API 5 min before, then notify at chosen time with API or fallback content. |
|
||||
|
||||
- **Daily Reminder** is driven from AccountViewView’s “Daily Reminder” toggle; on native it uses `NotificationService.getInstance().scheduleDailyNotification()` / `cancelDailyNotification()` (backed by `NativeNotificationService` and a single `reminderId`: `"daily_timesafari_reminder"`).
|
||||
- **New Activity** is intended to be driven only by `scheduleNewActivityDualNotification()` / `cancelDualSchedule()` in AccountViewView (dual schedule only).
|
||||
|
||||
So: one feature = single schedule (reminder), the other = dual schedule (prefetch + notify). They are different plugin APIs and different lifecycle (enable/disable) handling.
|
||||
|
||||
---
|
||||
|
||||
## 2. Where the Bug Comes From: One Dialog, Two Behaviors
|
||||
|
||||
**New Activity** reuses the same dialog as Daily Reminder: **`PushNotificationPermission.vue`**.
|
||||
|
||||
- When the user turns **New Activity** on from AccountViewView:
|
||||
- AccountViewView opens this dialog with `DAILY_CHECK_TITLE` and a callback that, on success, calls `scheduleNewActivityDualNotification(timeText)` on native.
|
||||
- The dialog does **not** receive `skipSchedule: true` for this flow (only the “edit reminder” flow does).
|
||||
|
||||
So when the user clicks “Turn on Daily Reminder” in the dialog for **New Activity**:
|
||||
|
||||
1. **PushNotificationPermission** (native path) runs `turnOnNativeNotifications()` and always calls:
|
||||
- `service.scheduleDailyNotification({ time, title: "Daily Check-In", body: "Time to check your TimeSafari activity", ... })`
|
||||
- i.e. it schedules the **single** daily reminder (plugin’s `scheduleDailyNotification`), using the same `reminderId` as Daily Reminder (`"daily_timesafari_reminder"`).
|
||||
2. Then the callback runs and AccountViewView calls **`scheduleNewActivityDualNotification(timeText)`**, which calls the plugin’s **`scheduleDualNotification`**.
|
||||
|
||||
Result:
|
||||
|
||||
- **Two schedules** are created when enabling New Activity:
|
||||
- One **single** reminder (wrong for New Activity): static “Daily Check-In” message, same ID as Daily Reminder.
|
||||
- One **dual** schedule (correct): prefetch + notify with API/fallback content.
|
||||
- When the user turns **New Activity** off, AccountViewView only calls **`cancelDualSchedule()`**. It never calls `cancelDailyNotification()` (or equivalent) for the single reminder.
|
||||
- So the **single** reminder stays scheduled and keeps firing at the chosen time. That’s the notification that “always fires” and “can’t be turned off.”
|
||||
|
||||
So the “huge problem with confusion with the web-push” is really: **the same dialog and the same “Turn on” path are used for both Daily Reminder and New Activity, but the dialog always schedules the single daily reminder on native**, while New Activity is supposed to use only the dual schedule. That mixing is what makes the wrong schedule stick and not be cancellable from the New Activity toggle.
|
||||
|
||||
---
|
||||
|
||||
## 3. Key Files and Flows
|
||||
|
||||
- **`src/components/PushNotificationPermission.vue`**
|
||||
- Shared dialog for both “Daily Reminder” and “New Activity” (via `pushType` = `DIRECT_PUSH_TITLE` vs `DAILY_CHECK_TITLE`).
|
||||
- On native it always uses `NotificationService.getInstance().scheduleDailyNotification(...)` (single reminder) and does not branch on “New Activity” to skip scheduling or to call the dual API.
|
||||
- Saves `notifyingNewActivityTime` when `pushType === DAILY_CHECK_TITLE` (lines 834–836). So the dialog both schedules the wrong thing and persists settings for New Activity.
|
||||
|
||||
- **`src/views/AccountViewView.vue`**
|
||||
- **Daily Reminder:** toggle opens same dialog with `DIRECT_PUSH_TITLE`; on native, disable path calls `service.cancelDailyNotification()`.
|
||||
- **New Activity:** toggle opens same dialog with `DAILY_CHECK_TITLE`; on success callback calls `scheduleNewActivityDualNotification(timeText)`; on disable only calls `DailyNotification.cancelDualSchedule()`.
|
||||
- `initializeState()`: on native with `activeDid`, calls `configureNativeFetcherIfReady(activeDid)` and, if New Activity is on, `updateStarredPlans(...)`. It does **not** re-call `scheduleNewActivityDualNotification` on load (so no double dual-schedule from here).
|
||||
|
||||
- **`src/services/notifications/NativeNotificationService.ts`**
|
||||
- Single reminder only: `scheduleDailyNotification` → plugin `scheduleDailyNotification` with `id: this.reminderId` (`"daily_timesafari_reminder"`); `cancelDailyNotification` → `cancelDailyReminder({ reminderId })`. No dual API here.
|
||||
|
||||
- **`src/services/notifications/nativeFetcherConfig.ts`**
|
||||
- Only configures the plugin for API calls (JWT, apiBaseUrl, activeDid). No scheduling.
|
||||
|
||||
- **`src/services/notifications/dualScheduleConfig.ts`**
|
||||
- Builds config for `scheduleDualNotification` (contentFetch 5 min before, userNotification at notify time). Used only from AccountViewView’s `scheduleNewActivityDualNotification`.
|
||||
|
||||
- **`src/main.capacitor.ts`**
|
||||
- Imports the daily-notification plugin; after a 2s delay calls `configureNativeFetcherIfReady()`. No scheduling; only fetcher config.
|
||||
|
||||
So: the “always fires / can’t turn off” behavior is from the **single** reminder created in `PushNotificationPermission` for New Activity and never cancelled when New Activity is turned off. The “confusion with web-push” is the reuse of the same dialog and the same native “schedule single reminder” path for both features.
|
||||
|
||||
---
|
||||
|
||||
## 4. Plugin Usage Summary
|
||||
|
||||
- **Single daily reminder (Daily Reminder):**
|
||||
- Scheduled/cancelled via `NativeNotificationService.scheduleDailyNotification` / `cancelDailyNotification` → plugin `scheduleDailyNotification` / `cancelDailyReminder` with one `reminderId`.
|
||||
- **Dual schedule (New Activity):**
|
||||
- Scheduled/cancelled only in AccountViewView via `DailyNotification.scheduleDualNotification` / `cancelDualSchedule` (and `configureNativeFetcherIfReady` + `updateStarredPlans` as per doc).
|
||||
- **Fetcher config (New Activity):**
|
||||
- `configureNativeFetcherIfReady()` from main.capacitor and from AccountViewView `initializeState` / `scheduleNewActivityDualNotification`; no scheduling by itself.
|
||||
|
||||
---
|
||||
|
||||
## 5. Root Cause (Concise)
|
||||
|
||||
- **Single code path in PushNotificationPermission** for native: it always schedules the **single** daily reminder, regardless of `pushType` (Daily Reminder vs New Activity).
|
||||
- For **New Activity**, that creates an extra, wrong schedule (single reminder) in addition to the correct dual schedule.
|
||||
- **Disable path for New Activity** only calls `cancelDualSchedule()` and never cancels the single reminder, so that reminder keeps firing and appears as “always fires” and “can’t be turned off.”
|
||||
|
||||
---
|
||||
|
||||
## 6. Proper Fix: Options and Detail
|
||||
|
||||
A fix should ensure that (1) enabling New Activity creates only the dual schedule, and (2) disabling New Activity removes every schedule that was created for it. Below are concrete options and implementation notes.
|
||||
|
||||
### 6.1 Option A: Don’t schedule the single reminder when the dialog is for New Activity (recommended)
|
||||
|
||||
**Idea:** On native, when the dialog is opened for **New Activity** (`pushType === DAILY_CHECK_TITLE`), the dialog should **not** call `scheduleDailyNotification`. Only the callback in AccountViewView should run, and it already calls `scheduleNewActivityDualNotification(timeText)`, which uses the dual API only.
|
||||
|
||||
**Where:** `PushNotificationPermission.vue`, inside `turnOnNativeNotifications()`.
|
||||
|
||||
**Implementation sketch:**
|
||||
|
||||
- After requesting permissions and before calling `service.scheduleDailyNotification(...)`, branch on `pushType` and platform:
|
||||
- If native **and** `pushType === this.DAILY_CHECK_TITLE`: skip the `scheduleDailyNotification` call entirely. Still run the rest of the flow (e.g. build `timeText`, save settings if desired, call `callback(true, timeText, ...)`). AccountViewView’s callback will then call `scheduleNewActivityDualNotification(timeText)` and that is the only schedule created for New Activity.
|
||||
- Otherwise (web, or Daily Reminder on native): keep current behavior and call `scheduleDailyNotification` as today.
|
||||
|
||||
**Pros:** Single source of truth for “what is scheduled for New Activity” (dual only). No leftover single reminder to cancel later. Clear separation: dialog collects time + permission; AccountViewView owns native scheduling for New Activity.
|
||||
|
||||
**Cons:** Dialog’s native path now has two behaviors (schedule vs no schedule) depending on `pushType`; needs a quick comment so future changes don’t regress.
|
||||
|
||||
**Note:** The “edit reminder” flow already uses `skipSchedule: true` so the dialog doesn’t schedule; only the parent does. For New Activity enable, we’re doing the same idea: dialog doesn’t schedule on native, parent does.
|
||||
|
||||
### 6.2 Option B: When turning New Activity off, also cancel the single reminder
|
||||
|
||||
**Idea:** Assume the wrong single reminder might already exist (e.g. from before the fix, or from a different code path). When the user turns **New Activity** off, in addition to `cancelDualSchedule()`, call the service’s `cancelDailyNotification()` so the single reminder (same `reminderId` as Daily Reminder) is cancelled too.
|
||||
|
||||
**Where:** `AccountViewView.vue`, inside the disable branch of `showNewActivityNotificationChoice()` (where we currently only call `DailyNotification.cancelDualSchedule()`).
|
||||
|
||||
**Implementation sketch:**
|
||||
|
||||
- On native, when user confirms “turn off New Activity”:
|
||||
1. Call `DailyNotification.cancelDualSchedule()` (existing).
|
||||
2. Call `NotificationService.getInstance().cancelDailyNotification()` (new) so any single reminder that was mistakenly scheduled for this flow is removed.
|
||||
|
||||
**Pros:** Defensive: cleans up the bad schedule even if it was created in the past or by another path. Complements Option A (e.g. A prevents new wrong schedules; B cleans up existing ones).
|
||||
|
||||
**Cons:** That single `reminderId` is shared with **Daily Reminder**. If the user has **Daily Reminder** on and **New Activity** on, then turns only **New Activity** off, we must not cancel the reminder they still want for Daily Reminder. So either:
|
||||
- Only call `cancelDailyNotification()` when we’re sure the single reminder was created for New Activity (e.g. we don’t have a separate “New Activity reminder ID”), which is hard without more state, or
|
||||
- Don’t use Option B alone as the primary fix: use Option A so we never create the single reminder for New Activity, and only add B if we decide we need a one-time cleanup or a safety net (with care not to cancel Daily Reminder’s schedule).
|
||||
|
||||
**Recommendation:** Use Option A as the main fix. Add Option B only if the team agrees we need to cancel the single reminder on “New Activity off” and can do so without affecting Daily Reminder (e.g. by introducing a distinct reminder ID for a “New Activity legacy” reminder and only cancelling that, or by documenting that B is a one-time migration and not long-term behavior).
|
||||
|
||||
### 6.3 Optional cleanup: Separate reminder IDs or dialog responsibilities
|
||||
|
||||
- **Separate reminder IDs:** Today both Daily Reminder and the mistaken New Activity single reminder use `"daily_timesafari_reminder"`. If we ever want to support “both features on” and cancel only one, we’d need a second ID (e.g. one for Daily Reminder, one for New Activity). With Option A in place, New Activity no longer creates a single reminder, so we might not need a second ID unless we add a dedicated “New Activity fallback” single alarm later.
|
||||
- **Dialog responsibilities:** We could narrow the dialog’s role when used for New Activity on native to “collect time + request permission and report success,” and leave all scheduling to AccountViewView. That’s what Option A does without necessarily refactoring the rest of the dialog (e.g. web push, Daily Reminder) in the same change.
|
||||
- **Removing web-push logic for New Activity:** If the team decides to “totally remove” web-push logic that was added for New Activity, that would be a separate change (e.g. ensure New Activity on web either uses a different mechanism or is explicitly unsupported). The lay-of-the-land and this fix section focus on native; web can be scoped in a follow-up.
|
||||
|
||||
---
|
||||
|
||||
## 7. Testing New Activity on a Real Device (iOS or Android)
|
||||
|
||||
Use this section to verify the New Activity flow end-to-end on a physical device after implementing the fix (or to reproduce the current bug).
|
||||
|
||||
### Prerequisites
|
||||
|
||||
- **Build:** Native app built and installed (e.g. `npx cap sync` then build/run from Xcode or Android Studio), or a dev build on device.
|
||||
- **Identity:** User is signed in (active DID set) so `configureNativeFetcherIfReady` and the native fetcher can use a valid JWT.
|
||||
- **Endorser API URL:** New Activity prefetch uses **Account → API Server URL** (the Endorser base URL passed to `configureNativeFetcher`), not the Partner API URL. You can run these tests against **production, test, or local Endorser** (e.g. the test preset `https://test-api.endorser.ch`); use an identity, JWT, and starred plans that exist on **that** server. Changing only **Partner API** URL does not change where `plansLastUpdatedBetween` is called.
|
||||
- **Optional:** One or more starred plans so the API can return activity; with zero starred plans the notification should still show with a sensible fallback (e.g. “No updates in your starred projects”).
|
||||
|
||||
### Enable flow
|
||||
|
||||
1. Open **Account** (Profile).
|
||||
2. In the **Notifications** section, turn **New Activity Notification** on.
|
||||
3. In the dialog, choose a time. For quick testing, set the device clock or pick a time **2–5 minutes from now** (e.g. if it’s 14:00, choose 14:03).
|
||||
4. Tap **Turn on Daily Reminder** (or equivalent), grant notification permission when the OS prompts, and confirm the dialog closes and the toggle shows on with the chosen time.
|
||||
5. **Background the app** (home or switch to another app). The prefetch runs ~5 minutes before the chosen time; the user notification fires at the chosen time.
|
||||
|
||||
### What to verify (after fix)
|
||||
|
||||
- **One notification** at the chosen time, with content from the API or the fallback text (e.g. “Check your starred projects and offers for updates.”). You should **not** see a second, static “Daily Check-In” / “Time to check your TimeSafari activity” notification from the old single-reminder path.
|
||||
- **Before the fix:** You may see two notifications (one static from the mistaken single schedule, one from the dual schedule), and turning New Activity off will only stop the dual one; the static one will keep firing.
|
||||
|
||||
### Disable flow
|
||||
|
||||
1. On **Account**, turn **New Activity Notification** off and confirm in the “turn off” dialog.
|
||||
2. Wait until the next occurrence of the previously chosen time (or use the same “time a few minutes ahead” trick and wait). **No notification** should appear. If one still appears, the single reminder was not cancelled (current bug or Option B not applied correctly).
|
||||
|
||||
### Device-specific notes
|
||||
|
||||
- **Android:** This app has **exact alarm disabled** (no `SCHEDULE_EXACT_ALARM`). Notification permission must be granted; delivery may be inexact or batched by the system. If the app is killed by the OS, behavior may depend on plugin boot/recovery behavior.
|
||||
- **iOS:** Notification permission and background capabilities (e.g. background fetch) may affect prefetch. Test with app in background, not force-quit.
|
||||
- **Time zone:** The chosen time is in the device’s local time. Ensure the device date/time and time zone are correct when testing.
|
||||
|
||||
### Optional test cases
|
||||
|
||||
- **No starred plans:** Enable New Activity with no starred projects; confirm no crash and a sensible fallback message in the notification.
|
||||
- **JWT / API errors:** After leaving the app in background for a long time, the JWT may expire. Re-opening Account (or app) may re-run `configureNativeFetcherIfReady`; document or test whether a new notification still gets valid content or shows fallback.
|
||||
- **Daily Reminder and New Activity both on:** With the fix, turning off only New Activity should not affect the Daily Reminder notification (they use different plugin APIs; Option B must not cancel the single reminder if the user still has Daily Reminder on).
|
||||
|
||||
### Testing: starred project with new activity (Android native fetcher)
|
||||
|
||||
Use this to verify that when a **starred** plan has **new** activity reported by `plansLastUpdatedBetween`, the notification shows API-derived copy (not only the dual-schedule default from `dualScheduleConfig.ts`).
|
||||
|
||||
The steps and expected notification copy below are **Android-specific**: this repo registers `TimeSafariNativeFetcher` only on Android today. Do not assume the same strings or behavior on iOS until native fetcher parity exists; see **`doc/notification-from-api-call.md`** (iOS checklist and remaining tasks).
|
||||
|
||||
**How it works (short):** On Android, `TimeSafariNativeFetcher` POSTs to `/api/v2/report/plansLastUpdatedBetween` with `planIds` from the plugin (`updateStarredPlans`) and `afterId` from stored `last_acked_jwt_id` (or `"0"` initially). When the response `data` array is **non-empty**, the fetcher builds **one** `NotificationContent`: title **Starred Project Update** (one row) or **Starred Project Updates** (two or more rows); body uses each row’s `plan.name` when present (else **Unnamed Project**). For a single update: `[name] has been updated.` For multiple: typographic quotes around the first row’s name, then ` + N more have been updated.` (with `N` = number of additional rows). When `data` is **empty**, the fetcher returns **no** notification items (no “nothing to report” notification). (See `android/app/src/main/java/app/timesafari/TimeSafariNativeFetcher.java`.)
|
||||
|
||||
**Procedure (repeatable on device)**
|
||||
|
||||
1. Sign in on the Endorser environment you mean to test (e.g. test API URL in Account—see **Prerequisites**, Endorser API URL) so `configureNativeFetcherIfReady` can set JWT and `activeDid`.
|
||||
2. Star at least one project you can change (e.g. your own test plan on staging).
|
||||
3. Turn **New Activity Notification** on and pick a time **2–5 minutes ahead** (same quick-test pattern as above).
|
||||
4. Open **Account** once (or finish the enable flow) so `updateStarredPlans({ planIds })` runs with current `starredPlanHandleIds`.
|
||||
5. **Background the app** (home out; do not force-quit). Prefetch runs on the cron **~5 minutes before** the chosen time; the user notification fires at the chosen time.
|
||||
6. **Produce new activity the API will return:** before that prefetch window (i.e. early enough that the scheduled content fetch still sees it), make a real change to the starred plan so `plansLastUpdatedBetween` returns **new** rows after the current `afterId` (e.g. an edit or other update your backend exposes through that report). If you change the plan **after** prefetch already ran with no new rows, you may not get an API-derived notification until the next prefetch cycle (typically the next day at the same T−5 schedule, unless you reschedule).
|
||||
|
||||
**What to verify**
|
||||
|
||||
- **One notification** at the chosen time (no extra static “Daily Check-In” after the fix—see “What to verify (after fix)” above).
|
||||
- **Success path (API returns updates):** Title/body match **Starred Project Update(s)** and the `[name] has been updated.` / `[first name] + N more have been updated.` patterns (names from `plan.name`), not the generic `buildDualScheduleConfig` defaults (**New Activity** / **Check your starred projects and offers for updates.**), which apply when the plugin falls back—e.g. fetch failure—not when the Android fetcher successfully returns Endorser-parsed content.
|
||||
- **Contrast (cursor caught up, no new rows):** After a successful fetch that returned data, `last_acked_jwt_id` advances. Without further plan changes, a later prefetch may return an empty `data` array; the fetcher then supplies **no** Endorser-derived notification (useful to compare against the “has activity” case; the plugin may still show dual-schedule fallback text depending on configuration).
|
||||
|
||||
**Repeatability:** Each successful fetch that returns data moves the `afterId` cursor forward. To see **Starred Project Update** copy again on subsequent tests, make **another** qualifying plan change (or accept heavier setup such as clearing app/plugin storage to reset cursor—usually unnecessary).
|
||||
|
||||
**Debugging:** On Android, filter **logcat** for `TimeSafariNativeFetcher` (e.g. HTTP 200, `Fetched N notification(s)`) to confirm prefetch ran and how many `NotificationContent` items were built.
|
||||
|
||||
**Note:** The in-app **New Activity** screen loads starred changes via the JS stack; the **push** path uses the native fetcher and plugin cache. Validate the notification using **background + prefetch timing**, not only by opening that screen.
|
||||
|
||||
---
|
||||
|
||||
## 8. Plugin Repo Alignment and Attention Items
|
||||
|
||||
Comparison with the **daily-notification-plugin** repo (e.g. `daily-notification-plugin_test` or gitea `master`) to confirm our documentation and usage line up, and to flag anything that needs attention for the New Activity feature.
|
||||
|
||||
### 8.1 What lines up
|
||||
|
||||
- **API surface:** Plugin `definitions.ts` exposes `configureNativeFetcher({ apiBaseUrl, activeDid, jwtToken })`, `scheduleDualNotification(config)`, `cancelDualSchedule()`, `updateStarredPlans({ planIds })`, `scheduleDailyNotification(options)`, and `cancelDailyReminder(reminderId)`. Our app uses these as described in this doc; `buildDualScheduleConfig` produces a `DualScheduleConfiguration` that matches the plugin’s `ContentFetchConfig` / `UserNotificationConfig` / `relationship` shape (cron schedules, title/body, `callbacks: {}`, `fallbackBehavior: "show_default"`, etc.).
|
||||
- **Native fetcher:** Plugin is designed for a host-supplied JWT via `configureNativeFetcher` and a native fetcher implementation (e.g. Android `TimeSafariNativeFetcher`). Our `nativeFetcherConfig.ts` and Android `TimeSafariNativeFetcher.java` follow that model; prefetch runs in the plugin’s background workers and uses the configured credentials.
|
||||
- **Dual vs single:** The plugin clearly separates:
|
||||
- **Single daily path:** `scheduleDailyNotification(options)` (with `id` on Android) and `cancelDailyReminder(reminderId)` (iOS uses `reminder_<reminderId>` for the static-reminder path).
|
||||
- **Dual path:** `scheduleDualNotification(config)` and `cancelDualSchedule()`.
|
||||
So our analysis that “two schedules” are created when the dialog schedules the single reminder and AccountViewView schedules the dual is consistent with the plugin.
|
||||
- **Exact alarm:** The plugin’s Android implementation does **not** require exact alarm: it proceeds with scheduling using inexact/windowed alarms when exact is not granted. The plugin’s `INTEGRATION_GUIDE.md` still shows `SCHEDULE_EXACT_ALARM` in the manifest example; this app has chosen to disable exact alarm, and the plugin supports that. No doc change needed beyond what we already state in section 7.
|
||||
|
||||
### 8.2 Attention items
|
||||
|
||||
- **`cancelDailyReminder` signature:** In the plugin’s `definitions.ts`, `cancelDailyReminder(reminderId: string)`. The app calls it with an object: `cancelDailyReminder({ reminderId })`. On iOS the plugin uses `call.getString("reminderId")`, so the object form works. If the plugin’s TypeScript definition is ever used for strict typing, prefer updating the plugin to accept `{ reminderId: string }` or document that the bridge accepts an object with a `reminderId` key.
|
||||
- **Plugin INTEGRATION_GUIDE vs this app:** The guide describes generic polling, dual scheduling, and optional `SCHEDULE_EXACT_ALARM`. This app uses the dual-schedule + native-fetcher path only (no generic polling), and does not use exact alarm. When onboarding or debugging, treat the guide as the full plugin feature set; our flow is the “legacy dual scheduling” + native fetcher part plus `updateStarredPlans` and `configureNativeFetcher`.
|
||||
- **iOS `scheduleDailyNotification` and stable `id`:** On **Android**, the plugin uses `options.getString("id")` as the stable `scheduleId` for “one per day” semantics and cleanup. On **iOS**, the implementation in the repo was observed to build notification content with an internally generated id (e.g. `daily_<timestamp>`) and not obviously use the app-provided `id` from the call. If the app ever relies on a stable id on iOS for the single reminder (e.g. to cancel or replace only that reminder), it’s worth confirming in the plugin’s iOS code whether the call’s `id` is read and used; if not, consider requesting or contributing a change so iOS also uses the app-provided id for consistency with Android.
|
||||
- **Dual schedule and content fetch:** The plugin’s dual schedule runs the content-fetch job on its cron and then the user notification at the configured time; our config uses a 5-minute gap and `relationship.contentTimeout` / `fallbackBehavior: "show_default"`. The native fetcher is invoked by the plugin’s background layer when the content-fetch schedule fires; we don’t rely on JS `callbacks` in the config (we pass `callbacks: {}`). That matches the “native fetcher does the work” design.
|
||||
|
||||
### 8.3 iOS `UNIMPLEMENTED` on `scheduleDualNotification` (other methods work)
|
||||
|
||||
If iOS logs `scheduleNewActivityDualNotification failed: {"code":"UNIMPLEMENTED"}` while `configureNativeFetcher` succeeds, Capacitor is often rejecting the call in **JavaScript** because `scheduleDualNotification` is missing from `window.Capacitor.PluginHeaders` for `DailyNotification` (stale **Pods / Xcode binary** after upgrading the plugin). **Not** usually a missing Swift handler if `node_modules` already lists the method in `pluginMethods`.
|
||||
|
||||
**Recovery:** `npx cap sync ios`, `cd ios/App && pod install`, Xcode **Clean Build Folder**, rebuild. See **`doc/plugin-feedback-ios-scheduleDualNotification.md`** (troubleshooting section).
|
||||
|
||||
### 8.4 Summary
|
||||
|
||||
The plugin repo aligns with how we use it for New Activity (dual schedule + native fetcher, no generic polling, exact alarm optional). The main follow-ups are: (1) clarify or align `cancelDailyReminder` argument shape in the plugin if needed for typing/tooling, and (2) confirm on iOS whether `scheduleDailyNotification` uses the app-provided `id` for stable single-reminder semantics.
|
||||
203
doc/plan-background-jwt-pool-and-expiry.md
Normal file
203
doc/plan-background-jwt-pool-and-expiry.md
Normal file
@@ -0,0 +1,203 @@
|
||||
# Plan: Background New Activity JWT — extended expiry + token pool
|
||||
|
||||
**Date:** 2026-03-27 14:29 PST
|
||||
**Status:** Draft for implementation
|
||||
**Audience:** TimeSafari / crowd-funder developers
|
||||
**Related:** `doc/endorser-jwt-background-prefetch-options.md`, `android/.../TimeSafariNativeFetcher.java`, `src/services/notifications/nativeFetcherConfig.ts`, `src/libs/crypto/index.ts`
|
||||
|
||||
---
|
||||
|
||||
## 1. Problem statement
|
||||
|
||||
Background prefetch for New Activity calls Endorser with a Bearer JWT configured via `configureNativeFetcher`. The token previously came from `getHeaders()` → `accessToken()`, which used **`exp` ≈ 60 seconds** (`src/libs/crypto/index.ts`). Prefetch runs **minutes later** in WorkManager **without JavaScript**, so the JWT can be **expired** before the POST (`JWT_VERIFY_FAILED`).
|
||||
|
||||
**Goals:**
|
||||
|
||||
1. Use JWTs whose **`exp`** covers the gap between **last app-side configure** and **prefetch** (and ideally days without opening the app).
|
||||
2. Optionally support a **pool** of distinct JWT strings so Endorser can enforce **duplicate-JWT** / **one-time-use** rules without breaking daily prefetch. **Pool size** should follow **`expiryDays + buffer`** (one distinct token per day over the JWT lifetime, plus headroom for retries / edge cases); **implementation uses `BACKGROUND_JWT_POOL_SIZE = 100`** until policy changes.
|
||||
3. Keep pool size and expiry policy **easy to change** (constants / remote config later).
|
||||
|
||||
---
|
||||
|
||||
## 2. Guiding principles
|
||||
|
||||
| Principle | Implication |
|
||||
|-----------|-------------|
|
||||
| **Background has no JS** | Token selection and HTTP must run in **native** (or plugin) code using **persisted** data. |
|
||||
| **Single source of truth for signing** | Continue using **`createEndorserJwtForDid`** (same keys as today); do not fork crypto in Java/Kotlin. |
|
||||
| **Configurable pool size** | One constant `BACKGROUND_JWT_POOL_SIZE`; **currently 100**. Size should satisfy **`≥ expiryDays + buffer`** (see below). |
|
||||
| **Phased delivery** | Ship **extended expiry** first; add **pool** when server duplicate rules require it or in the same release if coordinated. |
|
||||
|
||||
### 2.1 Pool size rationale (`expiryDays + buffer`)
|
||||
|
||||
For **one New Activity prefetch per day**, each day should use a **distinct** JWT string if the server rejects reuse. Over the JWT lifetime (aligned with **`exp`**), you need at least **one token per day** the pool might be used without regeneration.
|
||||
|
||||
**Rule of thumb:**
|
||||
|
||||
```text
|
||||
BACKGROUND_JWT_POOL_SIZE ≥ ceil(BACKGROUND_JWT_EXPIRY_DAYS) + BACKGROUND_JWT_POOL_BUFFER
|
||||
```
|
||||
|
||||
- **`BACKGROUND_JWT_EXPIRY_DAYS`** — human-facing match to `exp` (e.g. **90**); convert to `BACKGROUND_JWT_EXPIRY_SECONDS` for the payload.
|
||||
- **`BACKGROUND_JWT_POOL_BUFFER`** — extra slots for **same-day retries**, manual tests, or stricter duplicate rules (e.g. **10**).
|
||||
|
||||
**Example:** 90‑day `exp` + buffer 10 ⇒ **minimum 100** logical slots. **This plan keeps `BACKGROUND_JWT_POOL_SIZE = 100`** as the shipped default so it matches that example; if `expiryDays` or buffer change later, **bump the constant** so the inequality still holds.
|
||||
|
||||
---
|
||||
|
||||
## 3. Phases
|
||||
|
||||
### Phase A — Extended expiry only (minimum viable)
|
||||
|
||||
**Scope**
|
||||
|
||||
- Introduce a dedicated mint path for **background / native fetcher** use (name TBD, e.g. `accessTokenForBackgroundNotifications(did)`), producing **one** JWT per configure call with:
|
||||
- `iss`: DID (unchanged)
|
||||
- `iat`: now
|
||||
- `exp`: now + **`BACKGROUND_JWT_EXPIRY_SECONDS`** (derived from **`BACKGROUND_JWT_EXPIRY_DAYS`**; see §2.1 / Phase B constants — **confirm** with Endorser policy)
|
||||
- Optional: `jti` or nonce for uniqueness if needed for logging/debug
|
||||
|
||||
- **`configureNativeFetcherIfReady`** should pass this token (or keep using a thin wrapper) instead of reusing the **60s** `accessToken()` when configuring native fetcher **only** — **do not** change interactive `getHeaders()` / passkey caching behavior for normal API calls unless product asks for it.
|
||||
|
||||
**Files (likely)**
|
||||
|
||||
- `src/libs/crypto/index.ts` — new function or parameters; keep `accessToken()` default at 60s for existing callers.
|
||||
- `src/services/notifications/nativeFetcherConfig.ts` — obtain background JWT via the new mint path, not `getHeaders()`’s generic path, **or** add a dedicated branch that calls the new mint after resolving `did`.
|
||||
|
||||
**Native**
|
||||
|
||||
- **`TimeSafariNativeFetcher`**: still one `jwtToken` field; no pool yet. Ensure `configure()` is called whenever TS refreshes (startup, resume, Account — already partially covered).
|
||||
|
||||
**Exit criteria**
|
||||
|
||||
- Logcat: prefetch POST returns **200** (or non-expired 4xx) when user has not opened the app for several **minutes** after configure.
|
||||
- Endorser accepts **`exp`** far enough in the future (coordinate TTL policy).
|
||||
|
||||
---
|
||||
|
||||
### Phase B — Token pool (size 100; driven by `expiryDays + buffer`)
|
||||
|
||||
**Why**
|
||||
|
||||
- Endorser may **reject duplicate JWT strings** (same bearer used twice). One long-lived token could fail on **day 2** if the server marks each JWT as consumed.
|
||||
- A **pool** of **N** distinct JWTs (different payload, e.g. unique `jti` per token) gives **N** independent strings with the same long **`exp`**. **N** should follow **§2.1** (`expiryDays + buffer`); **100** is the initial **`BACKGROUND_JWT_POOL_SIZE`** (satisfies e.g. 90 + 10).
|
||||
|
||||
**Scope**
|
||||
|
||||
1. **Constants** (single place, e.g. `src/constants/backgroundJwt.ts` or next to native fetcher config):
|
||||
|
||||
```text
|
||||
BACKGROUND_JWT_EXPIRY_DAYS = 90 // align with Endorser; drives exp
|
||||
BACKGROUND_JWT_EXPIRY_SECONDS = 90 * 24 * 60 * 60 // derived
|
||||
BACKGROUND_JWT_POOL_BUFFER = 10 // retries / headroom; tune with server team
|
||||
BACKGROUND_JWT_POOL_SIZE = 100 // must be >= expiryDays + buffer; adjust if policy changes
|
||||
```
|
||||
|
||||
2. **Mint in TS** (uses `createEndorserJwtForDid`):
|
||||
|
||||
- Loop `i = 0 .. POOL_SIZE - 1`
|
||||
- Payload: `{ iss, iat, exp, jti: `${did}#bg#${i}` or uuid }` — **confirm** `jti` format with Endorser if required.
|
||||
|
||||
3. **Persistence** — native code must read the pool **without JS**:
|
||||
|
||||
- **Option B1 (preferred):** Implement in **`@timesafari/daily-notification-plugin`** (not in the app): extend **`configureNativeFetcher`** to accept an optional JWT pool, persist it for native read. **Handoff spec:** `doc/plugin-feedback-daily-notification-configureNativeFetcher-jwt-pool.md` — copy or reference that file in the plugin repo PR.
|
||||
- **Option B2 (app-only, no plugin release):** Write JSON to **Capacitor Preferences** or **encrypted storage** from TS; **TimeSafariNativeFetcher** reads the same store on Android (requires knowing Capacitor’s Android `SharedPreferences` name/key convention or a tiny **bridge** in `MainActivity`). Use only if plugin work is deferred.
|
||||
|
||||
4. **Selection policy in `TimeSafariNativeFetcher`** (before each POST):
|
||||
|
||||
- **By calendar day:** `index = (epochDay + offset) % POOL_SIZE` (stable per day).
|
||||
- Or **sequential:** persist `lastUsedIndex` in prefs and increment (wrap). **Decision:** document chosen policy; day-based is easier to reason about for “one token per day.”
|
||||
|
||||
5. **configureNativeFetcherIfReady** (and any “reset notifications on startup” hook):
|
||||
|
||||
- Regenerate full pool when user opens app (per product decision), then call configure with pool + **current** `apiBaseUrl` / `did`.
|
||||
|
||||
6. **iOS:** When iOS native fetcher exists, mirror Android behavior.
|
||||
|
||||
**Exit criteria**
|
||||
|
||||
- Prefetch succeeds on **consecutive days** with duplicate-JWT enforcement enabled on a **staging** Endorser.
|
||||
- Pool **refreshes** on startup without breaking dual schedule.
|
||||
|
||||
---
|
||||
|
||||
## 4. Detailed tasks (checklist)
|
||||
|
||||
### Crypto & TypeScript
|
||||
|
||||
- [ ] Add `BACKGROUND_JWT_EXPIRY_DAYS`, `BACKGROUND_JWT_EXPIRY_SECONDS`, `BACKGROUND_JWT_POOL_BUFFER`, and `BACKGROUND_JWT_POOL_SIZE` (exported constants), with a **comment** that `POOL_SIZE >= expiryDays + buffer` (see §2.1).
|
||||
- [ ] Implement `mintBackgroundJwtPool(did: string): Promise<string[]>` (or split single + pool).
|
||||
- [ ] Ensure each JWT has **unique** `jti` (or equivalent) for duplicate detection.
|
||||
- [ ] **Do not** break existing `accessToken()` 60s behavior for unrelated features.
|
||||
- [ ] Wire `configureNativeFetcherIfReady` to pass **single extended token** (Phase A) then **pool** (Phase B).
|
||||
- [ ] On **logout / identity clear**, clear persisted pool and call plugin clear if needed.
|
||||
|
||||
### Android
|
||||
|
||||
- [ ] **Phase A:** No structural change if `configure()` still receives one string; verify non-null `jwtToken` after configure.
|
||||
- [ ] **Phase B:** Parse pool from persisted JSON; implement `selectTokenForRequest()`; use selected token in `Authorization` header instead of sole `jwtToken` field (keep `configure` for `apiBaseUrl` / `did`).
|
||||
- [ ] Unit or instrumentation tests optional: selection index deterministic.
|
||||
|
||||
### Plugin (Option B1 — **daily-notification-plugin** repo)
|
||||
|
||||
- [ ] Follow **`doc/plugin-feedback-daily-notification-configureNativeFetcher-jwt-pool.md`** (API shape, Android/iOS, versioning).
|
||||
- [ ] Release new plugin version; bump dependency in this app.
|
||||
|
||||
### Product & server
|
||||
|
||||
- [ ] Endorser: confirm **max `exp`**, **duplicate JWT** semantics, recommended **`jti`** format.
|
||||
- [ ] Document operational limit: if user never opens app for **longer than `exp` allows** (or longer than **pool × daily use** without refresh), prefetch may fail until next open — align with `doc/endorser-jwt-background-prefetch-options.md`.
|
||||
|
||||
---
|
||||
|
||||
## 5. Security notes
|
||||
|
||||
- Longer-lived JWTs and **many** tokens increase impact if device is compromised. Mitigations: **encrypted prefs** where possible, **no logging** of full JWTs, **revocation** story with Endorser (key rotation, deny list).
|
||||
- Pool regeneration on **login** should replace old pools.
|
||||
|
||||
---
|
||||
|
||||
## 6. Testing plan
|
||||
|
||||
| Test | Expected |
|
||||
|------|----------|
|
||||
| Configure → wait **> 5 min** → prefetch | **200** from `plansLastUpdatedBetween` (Phase A) |
|
||||
| Two consecutive **days** with duplicate-JWT staging | **200** both days (Phase B) |
|
||||
| Logout | Pool cleared; no stale bearer |
|
||||
| Lower `BACKGROUND_JWT_POOL_SIZE` in dev only (below `expiryDays + buffer`) | Expect possible reuse / server duplicate errors — use to reproduce failures |
|
||||
|
||||
---
|
||||
|
||||
## 7. Rollout / staging
|
||||
|
||||
1. Implement Phase A behind feature flag **optional** (or direct if low risk).
|
||||
2. Verify on **test-api.endorser.ch** with server team.
|
||||
3. Phase B behind flag or same release once server duplicate rules are understood.
|
||||
|
||||
---
|
||||
|
||||
## 8. Where plugin documentation lives
|
||||
|
||||
| Document | Purpose |
|
||||
|----------|---------|
|
||||
| **`doc/plan-background-jwt-pool-and-expiry.md`** (this file) | End-to-end app plan: crypto, pool sizing, native host, rollout. |
|
||||
| **`doc/plugin-feedback-daily-notification-configureNativeFetcher-jwt-pool.md`** | **Plugin-only** handoff: extend `configureNativeFetcher`, persist pool, Android/iOS notes — intended for PRs in **daily-notification-plugin** (or Cursor on that repo). |
|
||||
|
||||
Keeping them **separate** avoids mixing consumer app tasks with plugin API contract; the plan **links** to the plugin feedback doc for Option B1.
|
||||
|
||||
---
|
||||
|
||||
## 9. References
|
||||
|
||||
| Topic | Location |
|
||||
|--------|----------|
|
||||
| Current 60s `accessToken` | `src/libs/crypto/index.ts` |
|
||||
| `createEndorserJwtForDid` | `src/libs/endorserServer.ts` |
|
||||
| Native configure | `src/services/notifications/nativeFetcherConfig.ts` |
|
||||
| Android HTTP | `android/.../TimeSafariNativeFetcher.java` |
|
||||
| Options doc (TTL, refresh, BFF) | `doc/endorser-jwt-background-prefetch-options.md` |
|
||||
| Plugin: `configureNativeFetcher` + JWT pool | `doc/plugin-feedback-daily-notification-configureNativeFetcher-jwt-pool.md` |
|
||||
|
||||
---
|
||||
|
||||
*Update this plan when Phase A/B ship or when Endorser policy changes.*
|
||||
@@ -0,0 +1,126 @@
|
||||
# Plugin feedback: Android dual schedule — native fetcher not used; fetch timing wrong
|
||||
|
||||
**Date:** 2026-03-24 21:56 PST
|
||||
**Target repo:** `@timesafari/daily-notification-plugin` (daily-notification-plugin)
|
||||
**Consuming app:** crowd-funder-for-time-pwa (TimeSafari)
|
||||
**Platform:** Android (Kotlin / Java)
|
||||
**Related:** New Activity notifications (`scheduleDualNotification` / `cancelDualSchedule`)
|
||||
|
||||
---
|
||||
|
||||
## Summary
|
||||
|
||||
On Android, the **dual (New Activity) schedule** path is **not** implementing the intended contract:
|
||||
|
||||
1. **Prefetch does not call `NativeNotificationContentFetcher`.**
|
||||
`ScheduleHelper.scheduleDualNotification` delegates fetch to `FetchWorker` (HTTP GET to optional `url`, or **mock JSON** when `url` is absent). The host app’s `TimeSafariNativeFetcher` is **never** invoked. Logcat shows `DNP-FETCH: Starting content fetch from: null, notificationTime=0` and **no** `TimeSafariNativeFetcher` `fetchContent` lines.
|
||||
|
||||
2. **Fetch is not scheduled at `contentFetch.schedule` (e.g. T−5 minutes).**
|
||||
`FetchWorker.enqueueFetch` enqueues **immediate** `OneTimeWorkRequest` work (no `setInitialDelay` aligned to the fetch cron). The **notify** alarm is scheduled correctly for `dual_notify_*`, but there is **no** corresponding alarm/work at the **fetch** cron time. A `dual_fetch_*` row may exist in the DB with `nextRunAt`, but the **actual** fetch runs at **enable/setup time**, not at T−5.
|
||||
|
||||
3. **Cache vs `DualScheduleHelper` / `contentTimeout`.**
|
||||
`DualScheduleHelper.resolveDualContentBlocking` only uses `contentCache` when the latest fetch is within `relationship.contentTimeout` (e.g. 5 minutes). If fetch runs **once at setup** and notify fires **~9+ minutes later**, cache is **stale** → `useCache=false` → default title/body from `userNotification`, even when mock payload was stored.
|
||||
|
||||
**Recommended direction (plugin):**
|
||||
|
||||
- For dual schedule when **no HTTP `url`** is configured (or when a flag indicates native mode), run **`NativeNotificationContentFetcher.fetchContent(FetchContext)`** (same path as `DailyNotificationFetchWorker` uses), persist results into the same `contentCache` / pipeline `DualScheduleHelper` expects.
|
||||
- **Schedule** that work (or an alarm that enqueues it) **at** `calculateNextRunTime(contentFetch.schedule)` — i.e. **before** the notify alarm, typically **5 minutes** earlier per app cron (see consuming app `timeToCronFiveMinutesBefore`).
|
||||
- Optionally align **one** scheduling mechanism: either exact alarm for fetch + notify, or WorkManager with **initial delay** to the next fetch instant (and reschedule after run).
|
||||
|
||||
---
|
||||
|
||||
## Symptoms (consuming app + logcat)
|
||||
|
||||
- Notification shows **default** copy from `userNotification` (`title` / `body` from `buildDualScheduleConfig`), not API-derived or native “No updates” copy.
|
||||
- Logcat: `DNP-DUAL: Resolved dual content: useCache=false` at notify time.
|
||||
- Logcat: `DNP-FETCH: Starting content fetch from: null, notificationTime=0` followed by `Content fetch completed successfully` **at schedule/setup time**, not at T−5.
|
||||
- **No** `TimeSafariNativeFetcher` `fetchContent START` / `POST …/plansLastUpdatedBetween` during prefetch window (host registers `NativeNotificationContentFetcher` and logs on configure + fetch).
|
||||
- **No** activity at the **prefetch cron** time (e.g. 19:05 for notify at 19:10); only **notify** fires at T.
|
||||
|
||||
---
|
||||
|
||||
## What the consuming app sends (contract)
|
||||
|
||||
**File:** `src/services/notifications/dualScheduleConfig.ts`
|
||||
|
||||
- `contentFetch.enabled: true`
|
||||
- `contentFetch.schedule`: cron **5 minutes before** `userNotification.schedule` (e.g. `"25 19 * * *"` for notify `"30 19 * * *"`).
|
||||
- **No** `contentFetch.url` — intended to use **native** Endorser API via `configureNativeFetcher` + `NativeNotificationContentFetcher`.
|
||||
- `relationship.autoLink: true`, `relationship.contentTimeout: 5 * 60 * 1000`, `fallbackBehavior: "show_default"`.
|
||||
|
||||
**Host app:** `android/.../TimeSafariNativeFetcher.java` implements `NativeNotificationContentFetcher` and calls `POST /api/v2/report/plansLastUpdatedBetween` with starred plan IDs from `updateStarredPlans`.
|
||||
|
||||
---
|
||||
|
||||
## Root cause (plugin code — paths to review)
|
||||
|
||||
These paths are from a local clone of **daily-notification-plugin**; line numbers may drift.
|
||||
|
||||
### 1. `FetchWorker` is URL/mock-only; does not call native fetcher
|
||||
|
||||
`android/src/main/java/org/timesafari/dailynotification/FetchWorker.kt`
|
||||
|
||||
- `enqueueFetch` passes `config.url` into `InputData`; `doWork` logs `Starting content fetch from: $url`.
|
||||
- `fetchContent(url, …)` when `url` is null/blank returns **`generateMockContent()`** — never calls `DailyNotificationPlugin.getNativeFetcherStatic().fetchContent(...)`.
|
||||
|
||||
### 2. `scheduleDualNotification` runs fetch work immediately, not at fetch cron
|
||||
|
||||
`android/src/main/java/org/timesafari/dailynotification/DailyNotificationPlugin.kt` — `object ScheduleHelper`, `suspend fun scheduleDualNotification(...)`
|
||||
|
||||
- Calls `scheduleFetch(context, contentFetchConfig)` which resolves to `FetchWorker.scheduleFetchForDual` → `enqueueFetch` **without** delay tied to `contentFetchConfig.schedule`.
|
||||
- Schedules **notify** via `NotifyReceiver.scheduleExactNotification` for `dual_notify_*` at `calculateNextRunTime(userNotificationConfig.schedule)`.
|
||||
- Persists `dual_fetch_*` with `nextRunAt = calculateNextRunTime(contentFetchConfig.schedule)` but **no** matching alarm/work is scheduled for that instant in the current flow (as observed).
|
||||
|
||||
### 3. Native fetcher exists elsewhere
|
||||
|
||||
`android/src/main/java/org/timesafari/dailynotification/DailyNotificationFetchWorker.java`
|
||||
|
||||
- Contains logic to call `NativeNotificationContentFetcher.fetchContent(FetchContext)` (with timeout). Dual schedule **does not** enqueue this worker for the TimeSafari `contentFetch` payload.
|
||||
|
||||
### 4. `DualScheduleHelper` behavior is consistent with “wrong fetch time”
|
||||
|
||||
`android/src/main/java/org/timesafari/dailynotification/DualScheduleHelper.kt`
|
||||
|
||||
- Uses latest `contentCache` only if `(now - fetchedAt) <= contentTimeoutMs`. If fetch ran at setup and notify is **later** than `contentTimeout`, **cache is ignored** → `useCache=false` in logs.
|
||||
|
||||
---
|
||||
|
||||
## Acceptance criteria (plugin)
|
||||
|
||||
After a fix, on a device with:
|
||||
|
||||
- `configureNativeFetcher` + `updateStarredPlans` called (host app),
|
||||
- `scheduleDualNotification` with `contentFetch.enabled: true`, no `url`, cron 5 min before notify,
|
||||
|
||||
then:
|
||||
|
||||
1. **At or before** the notify fire time, **within** `contentTimeout`, the cache used by `DualScheduleHelper` reflects **native** fetch results when the API returns data (or empty), not only mock JSON.
|
||||
2. Logcat **includes** host tag `TimeSafariNativeFetcher` with `fetchContent START` (or equivalent) **when** prefetch runs, **or** plugin logs an explicit `NativeNotificationContentFetcher` invocation.
|
||||
3. Prefetch **does not** run only at **INITIAL_SETUP**; it runs at the **next** occurrence of `contentFetch.schedule` (and reschedules for the following day after success, same as notify rollover).
|
||||
4. **Optional:** If `url` is set, preserve HTTP GET behavior; if `url` is absent and native fetcher is registered, use native path.
|
||||
|
||||
---
|
||||
|
||||
## References in consuming app
|
||||
|
||||
| Topic | Location |
|
||||
|--------|----------|
|
||||
| Dual config builder | `src/services/notifications/dualScheduleConfig.ts` |
|
||||
| `scheduleDualNotification` call | `src/views/AccountViewView.vue` (`scheduleNewActivityDualNotification`, `editNewActivityNotification`) |
|
||||
| Native fetcher | `android/app/src/main/java/app/timesafari/TimeSafariNativeFetcher.java` |
|
||||
| Registration | `MainActivity` / plugin init (host registers `DailyNotificationPlugin.setNativeFetcher`) |
|
||||
|
||||
---
|
||||
|
||||
## Notes for Cursor / implementers
|
||||
|
||||
- **Do not** assume `contentFetch.url` is present; TimeSafari intentionally omits it for native API.
|
||||
- **Reuse** the same `FetchContext` / timeout semantics as `DailyNotificationFetchWorker` where possible to avoid two divergent native fetch implementations.
|
||||
- After changing timing, **verify** `WorkManager` unique work name `fetch_dual` / `cancelDualSchedule` still cancel only dual fetch and do not break daily reminder.
|
||||
|
||||
---
|
||||
|
||||
## Related docs in this repo
|
||||
|
||||
- `doc/notification-from-api-call.md` — integration plan for API-driven New Activity.
|
||||
- `doc/plugin-feedback-android-scheduleDualNotification-contentFetch-json.md` — optional `timeout` / `retry*` JSON parsing (already addressed on the plugin side).
|
||||
@@ -0,0 +1,99 @@
|
||||
# Plugin feedback: Android `parseUserNotificationConfig` — optional fields vs `getBoolean` / `getString`
|
||||
|
||||
**Date:** 2026-03-20 21:11 PST
|
||||
**Target repo:** `@timesafari/daily-notification-plugin` (daily-notification-plugin)
|
||||
**Consuming app:** crowd-funder-for-time-pwa (TimeSafari)
|
||||
**Platform:** Android (Kotlin)
|
||||
**Related:** Same class of issue as [plugin-feedback-android-scheduleDualNotification-contentFetch-json.md](./plugin-feedback-android-scheduleDualNotification-contentFetch-json.md) (`contentFetch` / `parseContentFetchConfig`).
|
||||
|
||||
---
|
||||
|
||||
## Summary
|
||||
|
||||
`DailyNotificationPlugin.parseUserNotificationConfig()` uses **`JSObject` / `JSONObject` strict getters** for fields that the published TypeScript **`UserNotificationConfig`** marks as **optional** (`sound?`, `vibration?`, `priority?`, `title?`, `body?`). If a key is omitted, Android throws **`JSONException`** (e.g. *No value for vibration*), and `scheduleDualNotification` fails before scheduling.
|
||||
|
||||
**Recommended direction (plugin):** Align Kotlin parsing with `dist/esm/definitions.d.ts` by using **optional reads + defaults**, consistent with the fix already applied for `parseContentFetchConfig` (e.g. `optIntOrNull`, or Capacitor/JSON equivalents for booleans and strings).
|
||||
|
||||
**Recommended direction (app / already done in TimeSafari):** Send explicit `sound`, `vibration`, and `priority` (and title/body) in `buildDualScheduleConfig()` so **older plugin builds** that still use strict getters continue to work.
|
||||
|
||||
**Does it make sense to change both sides?** **Yes** — same reasoning as for `contentFetch`: the plugin should match its public contract; the app can stay explicit for compatibility and clarity.
|
||||
|
||||
---
|
||||
|
||||
## Symptoms (consuming app)
|
||||
|
||||
- In-app toast: *“Could not schedule New Activity notification. Please try again.”* (generic catch after `scheduleDualNotification` rejects.)
|
||||
- Logcat:
|
||||
|
||||
```text
|
||||
E DNP-PLUGIN: Schedule dual notification error
|
||||
E DNP-PLUGIN: org.json.JSONException: No value for vibration
|
||||
E DNP-PLUGIN: at org.json.JSONObject.getBoolean(JSONObject.java:419)
|
||||
E DNP-PLUGIN: at org.timesafari.dailynotification.DailyNotificationPlugin.parseUserNotificationConfig(DailyNotificationPlugin.kt:2428)
|
||||
E DNP-PLUGIN: at org.timesafari.dailynotification.DailyNotificationPlugin.scheduleDualNotification(DailyNotificationPlugin.kt:1392)
|
||||
```
|
||||
|
||||
(First failure observed after `contentFetch` timeouts were fixed was **`vibration`**; the same pattern can affect **`sound`** or **`priority`** if those keys are omitted.)
|
||||
|
||||
---
|
||||
|
||||
## Root cause
|
||||
|
||||
### Published TypeScript contract (`UserNotificationConfig`)
|
||||
|
||||
From `definitions.d.ts` (representative):
|
||||
|
||||
- `title?`, `body?`, `sound?`, `vibration?`, `priority?` — all optional.
|
||||
|
||||
### Current Android implementation (strict)
|
||||
|
||||
In `DailyNotificationPlugin.kt`, `parseUserNotificationConfig` (line numbers approximate; search for `parseUserNotificationConfig`):
|
||||
|
||||
```kotlin
|
||||
private fun parseUserNotificationConfig(configJson: JSObject): UserNotificationConfig {
|
||||
return UserNotificationConfig(
|
||||
enabled = configJson.getBoolean("enabled") ?: true,
|
||||
schedule = configJson.getString("schedule") ?: "0 9 * * *",
|
||||
title = configJson.getString("title"),
|
||||
body = configJson.getString("body"),
|
||||
sound = configJson.getBoolean("sound"),
|
||||
vibration = configJson.getBoolean("vibration"),
|
||||
priority = configJson.getString("priority")
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
- **`getBoolean("vibration")`** (and **`getBoolean("sound")`**) throw if the key is **missing** — optional in TS, required at runtime on Android.
|
||||
- **`getString("title")`**, **`getString("body")`**, **`getString("priority")`** likewise throw if missing (depending on `JSObject` / `JSONObject` behavior for absent keys).
|
||||
|
||||
So minimal or TS-faithful payloads omit `vibration` → immediate `JSONException`.
|
||||
|
||||
---
|
||||
|
||||
## Plugin-side recommendations
|
||||
|
||||
1. **Treat `UserNotificationConfig` optional fields as optional on Android**, mirroring `definitions.d.ts`:
|
||||
- **`vibration`:** e.g. `optBoolean` / nullable + default **`true`** (or `false` if that matches product default — document the default).
|
||||
- **`sound`:** same pattern; default **`true`** is typical for notifications.
|
||||
- **`priority`:** optional string with default **`"normal"`** (or map from TS union).
|
||||
- **`title` / `body`:** if TS allows omission, use optional reads + defaults consistent with dual-schedule UX (or reject with a clear `call.reject` message instead of a raw `JSONException`).
|
||||
|
||||
2. **Reuse the same helper style** as `parseContentFetchConfig` after the timeout fix (`optIntOrNull`, etc.) so one codebase convention applies to all dual-schedule JSON parsing.
|
||||
|
||||
3. **Tests:** Unit or integration test that calls `scheduleDualNotification` with a **minimal** `userNotification` object (only what TS strictly requires, if anything) and asserts scheduling succeeds on Android.
|
||||
|
||||
4. **iOS parity:** If iOS already accepts omitted `vibration` / `sound`, Android should match; if not, align both platforms to the same `UserNotificationConfig` rules.
|
||||
|
||||
---
|
||||
|
||||
## App-side note (TimeSafari)
|
||||
|
||||
`src/services/notifications/dualScheduleConfig.ts` — `buildDualScheduleConfig()` now includes **`vibration: true`** (with `sound: true`) so current native code paths succeed. Keeping this explicit is still recommended even after the plugin is fixed.
|
||||
|
||||
---
|
||||
|
||||
## References
|
||||
|
||||
- Plugin: `android/.../DailyNotificationPlugin.kt` — `parseUserNotificationConfig`
|
||||
- TS: `dist/esm/definitions.d.ts` — `UserNotificationConfig`, `DualScheduleConfiguration`
|
||||
- App: `src/services/notifications/dualScheduleConfig.ts` — `buildDualScheduleConfig`
|
||||
@@ -0,0 +1,117 @@
|
||||
# Plugin feedback: Android `scheduleDualNotification` — `JSONException: No value for timeout`
|
||||
|
||||
**Date:** 2026-03-20 18:21 PST
|
||||
**Target repo:** `@timesafari/daily-notification-plugin` (daily-notification-plugin)
|
||||
**Consuming app:** crowd-funder-for-time-pwa (TimeSafari)
|
||||
**Platform:** Android (Kotlin)
|
||||
**Plugin version observed:** 2.1.2 (from app `node_modules`)
|
||||
|
||||
---
|
||||
|
||||
## Summary
|
||||
|
||||
Scheduling the **New Activity** dual notification on Android fails with a native `JSONException` because `DailyNotificationPlugin.parseContentFetchConfig()` uses **`JSONObject.getInt()`** for `timeout`, `retryAttempts`, and `retryDelay`. Those keys are **absent** from the app’s `contentFetch` object built by `buildDualScheduleConfig()`. The plugin’s own TypeScript `ContentFetchConfig` marks those fields as **optional**, so the Android parser is stricter than the published contract.
|
||||
|
||||
**Recommended direction:**
|
||||
|
||||
1. **Plugin (primary):** Parse optional numeric fields with defaults (e.g. `optInt` / nullable + defaults) so payloads that omit them do not crash and match `definitions.d.ts`.
|
||||
2. **App (secondary / compatibility):** Include explicit `timeout`, `retryAttempts`, and `retryDelay` on `contentFetch` so older plugin versions that still use `getInt` continue to work.
|
||||
|
||||
**Does it make sense to change both sides?** **Yes.** Fixing the plugin aligns behavior with the documented API and protects any consumer that omits those fields. Fixing the app is still valuable for **older shipped plugin builds** and makes network behavior explicit. Together you get backward compatibility, clearer intent, and no silent reliance on undocumented defaults.
|
||||
|
||||
---
|
||||
|
||||
## Symptoms (consuming app)
|
||||
|
||||
- In-app toast: *“Could not schedule New Activity notification. Please try again.”* (generic error path after `scheduleDualNotification` rejects.)
|
||||
- Logcat (filtered on DNP / plugin tags):
|
||||
|
||||
```text
|
||||
E DNP-PLUGIN: Schedule dual notification error
|
||||
E DNP-PLUGIN: org.json.JSONException: No value for timeout
|
||||
E DNP-PLUGIN: at org.json.JSONObject.getInt(JSONObject.java:487)
|
||||
E DNP-PLUGIN: at org.timesafari.dailynotification.DailyNotificationPlugin.parseContentFetchConfig(DailyNotificationPlugin.kt:2403)
|
||||
E DNP-PLUGIN: at org.timesafari.dailynotification.DailyNotificationPlugin.scheduleDualNotification(DailyNotificationPlugin.kt:1391)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Root cause
|
||||
|
||||
### Call path
|
||||
|
||||
`scheduleDualNotification` reads `config.contentFetch` and passes it to `parseContentFetchConfig`:
|
||||
|
||||
- File: `android/.../DailyNotificationPlugin.kt`
|
||||
- `scheduleDualNotification` ~1391: `parseContentFetchConfig(contentFetchObj)`
|
||||
- `parseContentFetchConfig` ~2397–2411: uses `getInt` for three keys.
|
||||
|
||||
### Strict Android parsing
|
||||
|
||||
Illustrative (exact line numbers may shift between releases):
|
||||
|
||||
```kotlin
|
||||
// parseContentFetchConfig — timeout / retry fields are required via getInt()
|
||||
timeout = configJson.getInt("timeout"),
|
||||
retryAttempts = configJson.getInt("retryAttempts"),
|
||||
retryDelay = configJson.getInt("retryDelay"),
|
||||
```
|
||||
|
||||
`getInt` throws if the key is missing → first missing key in practice is `timeout` → `JSONException: No value for timeout`.
|
||||
|
||||
### App payload today (consuming app)
|
||||
|
||||
File: `src/services/notifications/dualScheduleConfig.ts` — `buildDualScheduleConfig()` sets `contentFetch` to:
|
||||
|
||||
- `enabled`, `schedule`, `callbacks` only (no `timeout`, `retryAttempts`, `retryDelay`, no `url`).
|
||||
|
||||
That matches the **TypeScript** contract in the plugin’s `dist/esm/definitions.d.ts`, where `timeout`, `retryAttempts`, and `retryDelay` are **optional** on `ContentFetchConfig`.
|
||||
|
||||
### Contract mismatch
|
||||
|
||||
| Layer | `timeout` / `retryAttempts` / `retryDelay` |
|
||||
|--------|--------------------------------------------|
|
||||
| TS `ContentFetchConfig` | Optional (`?`) |
|
||||
| Android `parseContentFetchConfig` | Required (`getInt` — throws if absent) |
|
||||
|
||||
The consuming app followed the TS API; Android rejected it at runtime.
|
||||
|
||||
---
|
||||
|
||||
## Plugin-side recommendations
|
||||
|
||||
1. **Use optional reads with defaults** for `timeout`, `retryAttempts`, and `retryDelay` (and any similar fields), e.g. Kotlin/Capacitor equivalents of `optInt` or `getInteger` with fallbacks documented in `ContentFetchConfig`.
|
||||
2. **Document defaults** in the plugin README or API docs if they are applied on native when omitted.
|
||||
3. **Consider tests** that call `scheduleDualNotification` with a minimal `contentFetch` (only `enabled`, `schedule`, `callbacks`) and assert scheduling succeeds on Android.
|
||||
4. **Optional:** If `url` is also read in a way that assumes presence, align with TS (`url?`) the same way.
|
||||
|
||||
---
|
||||
|
||||
## App-side recommendations (later; crowd-funder-for-time-pwa)
|
||||
|
||||
When you implement the app fix:
|
||||
|
||||
- Extend `contentFetch` in `buildDualScheduleConfig()` (`src/services/notifications/dualScheduleConfig.ts`) to include explicit integers, for example aligned with existing app/network conventions (the app’s `capacitor.config.ts` already uses a `timeout` value in one place — reuse or document chosen values).
|
||||
- Ensure **both** code paths that build dual config stay in sync (e.g. `AccountViewView.vue` uses `buildDualScheduleConfig` for New Activity scheduling and for `updateDualScheduleConfig` fallback).
|
||||
|
||||
This unblocks users on **current** plugin versions that still require those keys.
|
||||
|
||||
---
|
||||
|
||||
## References (paths in consuming app workspace)
|
||||
|
||||
- App config builder: `src/services/notifications/dualScheduleConfig.ts`
|
||||
- Native scheduling entry: `node_modules/@timesafari/daily-notification-plugin/android/.../DailyNotificationPlugin.kt` (`scheduleDualNotification`, `parseContentFetchConfig`)
|
||||
|
||||
---
|
||||
|
||||
## Answer: change both plugin and app?
|
||||
|
||||
**Yes, it makes sense to change both**, for different reasons:
|
||||
|
||||
| Side | Why |
|
||||
|------|-----|
|
||||
| **Plugin** | Fixes the real bug: native behavior must match the published optional TS fields; avoids breaking any client that sends a minimal `contentFetch`. |
|
||||
| **App** | Defense in depth and support for **already-shipped** plugin binaries that will not get the Kotlin fix until users update the app. Explicit values also document intended fetch/retry behavior in one place. |
|
||||
|
||||
If you only fix the plugin, new app releases still need users to update the **native** binary. If you only fix the app, any other consumer of the plugin or future minimal payloads can hit the same crash until the plugin is fixed.
|
||||
@@ -0,0 +1,95 @@
|
||||
# Plugin feedback: `configureNativeFetcher` — optional JWT pool for background API calls
|
||||
|
||||
**Date:** 2026-03-27 PST
|
||||
**Target repo:** `@timesafari/daily-notification-plugin` (daily-notification-plugin)
|
||||
**Consuming app:** crowd-funder-for-time-pwa (TimeSafari)
|
||||
**Related app plan:** `doc/plan-background-jwt-pool-and-expiry.md` (Phase B, Option B1)
|
||||
|
||||
---
|
||||
|
||||
## Summary
|
||||
|
||||
The host app’s **`NativeNotificationContentFetcher`** (`TimeSafariNativeFetcher` on Android) calls Endorser with a Bearer JWT set via **`configureNativeFetcher`**. For **background** prefetch, the token must stay valid until WorkManager runs (often **minutes later**); Endorser may also reject **duplicate** JWT strings across days.
|
||||
|
||||
The **app** will mint a **pool** of distinct JWTs (see app plan) and needs the plugin to **accept and persist** that pool so native code can select a token **without JavaScript** at prefetch time.
|
||||
|
||||
**Requested change (plugin):** extend **`configureNativeFetcher`** to accept an optional **JWT pool** alongside the existing **`jwtToken`**, persist it in the same storage the host already relies on (e.g. SharedPreferences / app group), and document how **`NativeNotificationContentFetcher`** implementations should read it.
|
||||
|
||||
---
|
||||
|
||||
## Motivation
|
||||
|
||||
| Issue | Why plugin support helps |
|
||||
|-------|---------------------------|
|
||||
| Single short-lived `jwtToken` | Expires before background fetch |
|
||||
| Server duplicate-JWT rules | Need many distinct bearer strings over time |
|
||||
| No JS in WorkManager | Pool must be readable **only** from native |
|
||||
|
||||
---
|
||||
|
||||
## Proposed API (TypeScript / Capacitor)
|
||||
|
||||
**Extend** existing `configureNativeFetcher` options (names indicative — align with plugin naming conventions):
|
||||
|
||||
```ts
|
||||
configureNativeFetcher(options: {
|
||||
apiBaseUrl: string;
|
||||
activeDid: string;
|
||||
/** Primary token; keep for backward compatibility and Phase A (single long-lived JWT). */
|
||||
jwtToken: string;
|
||||
/**
|
||||
* Optional. Distinct JWT strings for background use (e.g. one per day slot).
|
||||
* If omitted, behavior matches today (single jwtToken only).
|
||||
*/
|
||||
jwtTokens?: string[];
|
||||
});
|
||||
```
|
||||
|
||||
**Alternatives** (if size limits matter for bridge payload):
|
||||
|
||||
- `jwtTokenPoolJson: string` — JSON array string of JWT strings (single string across the bridge).
|
||||
|
||||
**Validation (plugin):**
|
||||
|
||||
- If `jwtTokens` present: length **≤** a sane cap (host will use ~100; plugin may enforce max e.g. 128).
|
||||
- Empty array: treat as “no pool” (same as omitting).
|
||||
|
||||
---
|
||||
|
||||
## Android
|
||||
|
||||
1. **Parse** new fields in `DailyNotificationPlugin.configureNativeFetcher` (or equivalent).
|
||||
2. **Persist** pool under the same prefs namespace used for other TimeSafari / dual-schedule data, or a **documented** key prefix (e.g. `jwt_token_pool` as JSON array string).
|
||||
3. **Document** for host implementers: `NativeNotificationContentFetcher` should:
|
||||
- Prefer **pool entry** for `fetchContent` when pool is non-empty (selection policy is **host** responsibility — e.g. day index % length), **or**
|
||||
- Expose a small helper the host fetcher calls to resolve “current” bearer.
|
||||
4. **Clear** pool when `configureNativeFetcher` is called with a new identity / empty pool / logout path (coordinate with host).
|
||||
5. **Backward compatibility:** if only `jwtToken` is sent, behavior **unchanged** from current release.
|
||||
|
||||
---
|
||||
|
||||
## iOS
|
||||
|
||||
When `configureNativeFetcher` exists on iOS, mirror Android: accept optional pool, persist, document read path for native fetcher.
|
||||
|
||||
---
|
||||
|
||||
## Versioning & release
|
||||
|
||||
- Bump **plugin semver** (minor: new optional fields).
|
||||
- Publish package; consuming app bumps **`@timesafari/daily-notification-plugin`** and updates `nativeFetcherConfig.ts` to pass `jwtTokens` when Phase B ships.
|
||||
|
||||
---
|
||||
|
||||
## References (host app)
|
||||
|
||||
| Topic | Location |
|
||||
|--------|----------|
|
||||
| End-to-end plan (Phase A/B, pool sizing) | `doc/plan-background-jwt-pool-and-expiry.md` |
|
||||
| Android fetcher | `android/.../TimeSafariNativeFetcher.java` |
|
||||
| Current configure call | `src/services/notifications/nativeFetcherConfig.ts` |
|
||||
| JWT options (expired token context) | `doc/endorser-jwt-background-prefetch-options.md` |
|
||||
|
||||
---
|
||||
|
||||
*This document is intended to be copied or linked from PRs in **daily-notification-plugin**; keep app-specific details in the app plan.*
|
||||
140
doc/plugin-feedback-ios-scheduleDualNotification.md
Normal file
140
doc/plugin-feedback-ios-scheduleDualNotification.md
Normal file
@@ -0,0 +1,140 @@
|
||||
# Plugin Feedback: Implement scheduleDualNotification on iOS
|
||||
|
||||
**Target repo:** daily-notification-plugin (iOS native layer)
|
||||
**Purpose:** Document for implementing or fixing `scheduleDualNotification` on iOS so the consuming app (TimeSafari / crowd-funder) can enable “New Activity” notifications.
|
||||
**Consuming app doc:** `doc/notification-new-activity-lay-of-the-land.md`
|
||||
|
||||
---
|
||||
|
||||
## Troubleshooting: `UNIMPLEMENTED` on iOS (Capacitor 6)
|
||||
|
||||
If **`configureNativeFetcher`** (or other DailyNotification methods) work but **`scheduleDualNotification`** still fails with **`{"code":"UNIMPLEMENTED"}`** and you **do not** see a native log line like `To Native -> DailyNotification scheduleDualNotification`, the failure is often **not** missing Swift code—it is **Capacitor’s JavaScript layer** rejecting the call because the method is **not listed** in `window.Capacitor.PluginHeaders` for `DailyNotification`. Those headers are built at runtime from the **compiled** plugin’s `pluginMethods` list (`CAPBridgedPlugin`).
|
||||
|
||||
**Fix in the consuming app (usual cause: stale Pods / binary):**
|
||||
|
||||
1. Ensure `node_modules/@timesafari/daily-notification-plugin` includes `scheduleDualNotification` in `DailyNotificationPlugin.swift`’s `pluginMethods` (v2.1.0+).
|
||||
2. From the project root: `npx cap sync ios`
|
||||
3. `cd ios/App && pod install` (or delete `Pods` + `Podfile.lock` and `pod install` if upgrading the plugin).
|
||||
4. Xcode: **Product → Clean Build Folder**, then rebuild and run on device/simulator.
|
||||
|
||||
**Verify:** Safari → Develop → attach to the app WebView → Console: inspect `window.Capacitor.PluginHeaders` and confirm the `DailyNotification` entry’s `methods` array includes `{ name: "scheduleDualNotification", ... }`.
|
||||
|
||||
If a full clean rebuild still doesn't fix it, clear Xcode's **system** DerivedData (quit Xcode, run `rm -rf ~/Library/Developer/Xcode/DerivedData/*TimeSafari*`, reopen and rebuild). On launch the app logs `[Capacitor] DNP PluginHeaders methods: [...]`; if that list omits `scheduleDualNotification`, the native binary is still stale.
|
||||
|
||||
If the method **is** present in headers but scheduling still fails, debug the Swift implementation (reject message, BG tasks, etc.).
|
||||
|
||||
### Misleading `UNIMPLEMENTED` before `scheduleDualNotification`
|
||||
|
||||
Capacitor’s `registerPlugin` proxy returns a **callable stub for every property name**. So `if (DailyNotification?.updateStarredPlans)` is **always truthy** even when iOS does not expose `updateStarredPlans` in `pluginMethods`. Calling that stub throws **`UNIMPLEMENTED`** in JS **before** any `To Native -> DailyNotification scheduleDualNotification` line appears—so logs look like “dual schedule is unimplemented” when the real failure was **`updateStarredPlans`**.
|
||||
|
||||
**Consuming-app fix:** treat `updateStarredPlans` as optional: catch `UNIMPLEMENTED` and continue, or only call after verifying the method name exists on `PluginHeaders` for `DailyNotification`. If the plugin adds `updateStarredPlans` natively later, starred-plan filtering will start working without app changes.
|
||||
|
||||
---
|
||||
|
||||
## Current behavior
|
||||
|
||||
- The **consuming app** calls `DailyNotification.scheduleDualNotification({ config })` from TypeScript when the user turns on “New Activity Notification” and picks a time (native iOS).
|
||||
- On **iOS**, the plugin rejects with **`code: "UNIMPLEMENTED"`** (observed in Xcode: `[AccountViewView] scheduleNewActivityDualNotification failed: {"code":"UNIMPLEMENTED"}`).
|
||||
- On **Android**, the same call is expected to work (dual schedule: content fetch + user notification).
|
||||
|
||||
The app has already:
|
||||
|
||||
- Called `configureNativeFetcher({ apiBaseUrl, activeDid, jwtToken })` so the plugin can use the native fetcher for API-driven content.
|
||||
- Called `updateStarredPlans({ planIds })` so the fetcher knows which plans to query.
|
||||
- Built a `config` object that matches the plugin’s `DualScheduleConfiguration` (see below).
|
||||
|
||||
So the missing piece on iOS is a **working implementation** of `scheduleDualNotification` that accepts this config and schedules the dual flow (content fetch at one time, user notification at a later time).
|
||||
|
||||
---
|
||||
|
||||
## Call from the consuming app
|
||||
|
||||
```ts
|
||||
await DailyNotification.scheduleDualNotification({ config });
|
||||
```
|
||||
|
||||
`config` is built by the app’s `buildDualScheduleConfig({ notifyTime })` and has the following shape.
|
||||
|
||||
---
|
||||
|
||||
## Config shape the app sends
|
||||
|
||||
The app sends a single `config` object that matches the plugin’s `DualScheduleConfiguration` (see `definitions.ts`). Example for `notifyTime: "18:30"` (6:30 PM):
|
||||
|
||||
```json
|
||||
{
|
||||
"contentFetch": {
|
||||
"enabled": true,
|
||||
"schedule": "25 18 * * *",
|
||||
"callbacks": {}
|
||||
},
|
||||
"userNotification": {
|
||||
"enabled": true,
|
||||
"schedule": "30 18 * * *",
|
||||
"title": "New Activity",
|
||||
"body": "Check your starred projects and offers for updates.",
|
||||
"sound": true,
|
||||
"priority": "normal"
|
||||
},
|
||||
"relationship": {
|
||||
"autoLink": true,
|
||||
"contentTimeout": 300000,
|
||||
"fallbackBehavior": "show_default"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
- **Cron format:** `"minute hour * * *"` (daily at that local time).
|
||||
- **contentFetch.schedule:** 5 minutes **before** the user’s chosen time (e.g. 18:25 for notify at 18:30).
|
||||
- **userNotification.schedule:** The user’s chosen time (e.g. 18:30).
|
||||
- **contentFetch.callbacks:** The app sends `{}`; the actual fetch is done by the **native fetcher** (already configured via `configureNativeFetcher`). The plugin should run the content-fetch job at the contentFetch cron and use the native fetcher to get content; at userNotification time it should show a notification using that content or the fallback title/body.
|
||||
- **relationship.contentTimeout:** Milliseconds to wait for content before showing the notification (app uses 5 minutes = 300000).
|
||||
- **relationship.fallbackBehavior:** `"show_default"` means if content isn’t ready in time, show the notification with the default title/body from `userNotification`.
|
||||
|
||||
The app does **not** send `contentFetch.url` or `contentFetch.timesafariConfig`; it relies on the native fetcher and `configureNativeFetcher` / `updateStarredPlans` for API behavior.
|
||||
|
||||
---
|
||||
|
||||
## Expected plugin behavior (iOS)
|
||||
|
||||
1. **Accept** the `config` argument (object with `contentFetch`, `userNotification`, and optional `relationship`).
|
||||
2. **Parse** the cron expressions for `contentFetch.schedule` and `userNotification.schedule` (e.g. using a shared cron parser or the same approach as Android).
|
||||
3. **Schedule** two things:
|
||||
- **Content fetch:** At the time given by `contentFetch.schedule`, run the **native notification content fetcher** (the one configured via `configureNativeFetcher`). Store the result in the plugin’s cache (or equivalent) for use when the user notification fires.
|
||||
- **User notification:** At the time given by `userNotification.schedule`, show a local notification. Use cached content from the fetch if available and within `relationship.contentTimeout`; otherwise use `userNotification.title` and `userNotification.body` (per `relationship.fallbackBehavior: "show_default"`).
|
||||
4. **Do not** reject with `UNIMPLEMENTED`; resolve the promise once scheduling has succeeded (or reject with a descriptive error if scheduling fails).
|
||||
5. **cancelDualSchedule()** should cancel both the content-fetch schedule and the user-notification schedule so the user can turn off New Activity from the app.
|
||||
|
||||
Alignment with **Android** (if implemented there) is desirable: same config shape, same semantics (prefetch then notify, fallback to default title/body). The plugin’s **definitions.ts** already defines `DualScheduleConfiguration`, `ContentFetchConfig`, `UserNotificationConfig`, and the `scheduleDualNotification` / `cancelDualSchedule` API.
|
||||
|
||||
---
|
||||
|
||||
## Where to look in the plugin (iOS)
|
||||
|
||||
- **Plugin entry:** `ios/Plugin/DailyNotificationPlugin.swift` (or equivalent)—find the handler for `scheduleDualNotification` (e.g. method that receives `call.getObject("config")`).
|
||||
- **Android reference:** `android/` implementation of `scheduleDualNotification` and how it schedules WorkManager/alarms for content fetch and for the user notification.
|
||||
- **Definitions:** `src/definitions.ts` — `DualScheduleConfiguration`, `scheduleDualNotification`, `cancelDualSchedule`.
|
||||
- **Native fetcher:** The app configures the native fetcher before calling `scheduleDualNotification`; the iOS plugin should invoke that same fetcher when the content-fetch job runs (BGAppRefreshTask or equivalent), not a URL from the config.
|
||||
|
||||
---
|
||||
|
||||
## Acceptance criteria
|
||||
|
||||
- [ ] On iOS, calling `DailyNotification.scheduleDualNotification({ config })` with the config shape above **does not** reject with `code: "UNIMPLEMENTED"`.
|
||||
- [ ] The content-fetch job is scheduled at `contentFetch.schedule` and uses the configured native fetcher to fetch content.
|
||||
- [ ] The user notification is scheduled at `userNotification.schedule` and shows with API-derived content when available, or with `userNotification.title` / `userNotification.body` as fallback.
|
||||
- [ ] Calling `DailyNotification.cancelDualSchedule()` cancels both schedules on iOS.
|
||||
- [ ] Behavior is consistent with Android where applicable (same config, same lifecycle).
|
||||
|
||||
---
|
||||
|
||||
## Relationship to consuming app
|
||||
|
||||
The consuming app will continue to call:
|
||||
|
||||
1. `configureNativeFetcher(...)` on startup and when enabling New Activity.
|
||||
2. `updateStarredPlans({ planIds })` when enabling or when Account view loads with New Activity on.
|
||||
3. `scheduleDualNotification({ config })` when the user turns on New Activity and picks a time.
|
||||
4. `cancelDualSchedule()` when the user turns off New Activity.
|
||||
|
||||
No change to the app’s config shape or call order is planned; the fix is entirely on the plugin iOS side to implement or correct `scheduleDualNotification` (and ensure `cancelDualSchedule` clears the dual schedule).
|
||||
@@ -0,0 +1,96 @@
|
||||
# Plugin fix: Android compile error — duplicate `scheduleId` in `handleDisplayNotification`
|
||||
|
||||
**Date:** 2026-03-20
|
||||
**Target repo:** `@timesafari/daily-notification-plugin` (daily-notification-plugin)
|
||||
**Consuming app:** crowd-funder-for-time-pwa (TimeSafari)
|
||||
**Platform:** Android (Java)
|
||||
|
||||
---
|
||||
|
||||
## Summary
|
||||
|
||||
The Android module fails to compile with **two** `javac` errors: `variable scheduleId is already defined in method handleDisplayNotification(String)`. The method already declares `String scheduleId` at the start of the `try` block; two nested blocks incorrectly **redeclare** `String scheduleId`, which Java forbids in the same method scope. Remove the redundant declarations and reuse the existing variable (or assign without `String` if you ever need to refresh it).
|
||||
|
||||
---
|
||||
|
||||
## Problem
|
||||
|
||||
- **File:** `android/src/main/java/org/timesafari/dailynotification/DailyNotificationWorker.java`
|
||||
- **Method:** `private Result handleDisplayNotification(String notificationId)`
|
||||
|
||||
**Compiler output (representative):**
|
||||
|
||||
```text
|
||||
DailyNotificationWorker.java:162: error: variable scheduleId is already defined in method handleDisplayNotification(String)
|
||||
String scheduleId = inputData.getString("schedule_id");
|
||||
^
|
||||
DailyNotificationWorker.java:193: error: variable scheduleId is already defined in method handleDisplayNotification(String)
|
||||
String scheduleId = inputData.getString("schedule_id");
|
||||
^
|
||||
```
|
||||
|
||||
**Root cause:** At the top of the `try` block, the code already has:
|
||||
|
||||
```java
|
||||
Data inputData = getInputData();
|
||||
String scheduleId = inputData.getString("schedule_id");
|
||||
```
|
||||
|
||||
Later, inside:
|
||||
|
||||
1. The `if (isStaticReminder) { ... }` branch — a line like `String scheduleId = inputData.getString("schedule_id");` (around line 162).
|
||||
2. The `else { ... }` branch — the same pattern (around line 193).
|
||||
|
||||
In Java, a local variable name cannot be declared again in nested blocks that share the enclosing method’s scope for that name. These inner `String scheduleId` lines are **illegal** and break `:timesafari-daily-notification-plugin:compileDebugJavaWithJavac`.
|
||||
|
||||
**Functional note:** Both inner reads use the same key (`"schedule_id"`) as the outer declaration, so they add **no** new information; the fix is to **delete** those inner declarations and keep using `scheduleId` from the first assignment.
|
||||
|
||||
---
|
||||
|
||||
## Required change
|
||||
|
||||
**Option A (recommended):** Delete the two redundant lines entirely:
|
||||
|
||||
- Remove the inner `String scheduleId = inputData.getString("schedule_id");` in the **static reminder** branch (post-reboot/rollover comment block).
|
||||
- Remove the inner `String scheduleId = inputData.getString("schedule_id");` in the **regular notification** branch (rollover/notify_* comment block).
|
||||
|
||||
All subsequent uses of `scheduleId` in those branches should continue to refer to the variable declared immediately after `getInputData()`.
|
||||
|
||||
**Option B (only if you must re-read input later):** Replace redeclaration with assignment:
|
||||
|
||||
```java
|
||||
scheduleId = inputData.getString("schedule_id");
|
||||
```
|
||||
|
||||
Do **not** prefix with `String` again inside the same method.
|
||||
|
||||
---
|
||||
|
||||
## Verification
|
||||
|
||||
1. **Compile:** From the plugin repo, run the Android Java compile for the library (or assemble debug). Expect **zero** errors for `DailyNotificationWorker.java`.
|
||||
2. **Consuming app:** Bump/publish the plugin version, update `package.json` in TimeSafari, `npm install`, `npx cap sync android`, then run the usual Android debug build (e.g. `./scripts/build-android.sh --test` or `assembleDebug`). The task `:timesafari-daily-notification-plugin:compileDebugJavaWithJavac` must succeed.
|
||||
3. **Behavior:** No intended behavior change: `schedule_id` is still read once per worker run from `getInputData()` and used for dual-prefix checks, static reminder DB fallback, and canonical content by `schedule_id` in the non-static path.
|
||||
|
||||
---
|
||||
|
||||
## Context (how this was found)
|
||||
|
||||
- Observed when running `npm run build:android:test:run` on crowd-funder-for-time-pwa; Vite/TypeScript succeeded; Gradle failed on the plugin’s Java sources under `node_modules/.../DailyNotificationWorker.java`.
|
||||
- Line numbers in published packages may drift slightly; search for `handleDisplayNotification` and duplicate `String scheduleId` inside that method.
|
||||
|
||||
---
|
||||
|
||||
## Cursor prompt (paste into plugin repo)
|
||||
|
||||
You can paste the block below into Cursor in the **daily-notification-plugin** workspace:
|
||||
|
||||
```text
|
||||
Fix Android compile errors in DailyNotificationWorker.java: in handleDisplayNotification(String notificationId), scheduleId is declared once after getInputData(). Remove the two illegal inner redeclarations "String scheduleId = inputData.getString(\"schedule_id\");" (static reminder branch and else branch). Reuse the outer scheduleId variable. Do not shadow or redeclare String scheduleId in the same method. Verify compileDebugJavaWithJavac passes.
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## After the fix
|
||||
|
||||
Release a new plugin version and update the consuming app’s dependency so `node_modules` is not hand-edited (edits there are lost on `npm install`).
|
||||
@@ -86,7 +86,7 @@ PODS:
|
||||
- SQLCipher/common (4.9.0)
|
||||
- SQLCipher/standard (4.9.0):
|
||||
- SQLCipher/common
|
||||
- TimesafariDailyNotificationPlugin (2.0.0):
|
||||
- TimesafariDailyNotificationPlugin (2.1.1):
|
||||
- Capacitor
|
||||
- ZIPFoundation (0.9.19)
|
||||
|
||||
@@ -172,7 +172,7 @@ SPEC CHECKSUMS:
|
||||
nanopb: 438bc412db1928dac798aa6fd75726007be04262
|
||||
PromisesObjC: f5707f49cb48b9636751c5b2e7d227e43fba9f47
|
||||
SQLCipher: 31878d8ebd27e5c96db0b7cb695c96e9f8ad77da
|
||||
TimesafariDailyNotificationPlugin: 3c12e8c39fc27f689f56cf4e57230a8c28611fcc
|
||||
TimesafariDailyNotificationPlugin: ab9860e6ab9db8019f64f3c08f115a0c4ffd32d9
|
||||
ZIPFoundation: b8c29ea7ae353b309bc810586181fd073cb3312c
|
||||
|
||||
PODFILE CHECKSUM: 6d92bfa46c6c2d31d19b8c0c38f56a8ae9fd222f
|
||||
|
||||
1830
package-lock.json
generated
1830
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "timesafari",
|
||||
"version": "1.3.8-beta",
|
||||
"version": "1.4.1-beta",
|
||||
"description": "Gift Economies Application",
|
||||
"author": {
|
||||
"name": "Gift Economies Team"
|
||||
|
||||
@@ -204,7 +204,7 @@ run_android() {
|
||||
safe_execute "Launching app" "adb -s $device_id shell am start -n app.timesafari.app/app.timesafari.MainActivity"
|
||||
else
|
||||
log_info "Launching emulator and installing app"
|
||||
safe_execute "Launching app" "npx cap run android"
|
||||
safe_execute "Launching app" "npx cap run android --no-sync"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
@@ -645,11 +645,13 @@ if [ "$BUILD_AAB" = true ]; then
|
||||
fi
|
||||
|
||||
# Step 11: Auto-run app if requested
|
||||
# cap run runs sync by default, which would overwrite capacitor.plugins.json again;
|
||||
# we already synced and ran restore-local-plugins.js above, so skip sync here.
|
||||
if [ "$AUTO_RUN" = true ]; then
|
||||
log_step "Auto-running Android app..."
|
||||
safe_execute "Launching app" "npx cap run android" || {
|
||||
safe_execute "Launching app" "npx cap run android --no-sync" || {
|
||||
log_error "Failed to launch Android app"
|
||||
log_info "You can manually run with: npx cap run android"
|
||||
log_info "You can manually run with: npx cap run android --no-sync"
|
||||
exit 9
|
||||
}
|
||||
log_success "Android app launched successfully!"
|
||||
|
||||
19
src/App.vue
19
src/App.vue
@@ -360,6 +360,7 @@
|
||||
|
||||
<script lang="ts">
|
||||
import { Vue, Component } from "vue-facing-decorator";
|
||||
import { Capacitor } from "@capacitor/core";
|
||||
|
||||
import { NotificationIface } from "./constants/app";
|
||||
import { PlatformServiceMixin } from "@/utils/PlatformServiceMixin";
|
||||
@@ -382,6 +383,24 @@ export default class App extends Vue {
|
||||
async turnOffNotifications(
|
||||
notification: NotificationIface,
|
||||
): Promise<boolean> {
|
||||
// On native (iOS/Android) we don't use web push; the callback handles cancel + state in the view.
|
||||
// The callback is the one passed for this specific modal (New Activity or Daily Reminder), so we only turn off that one.
|
||||
if (Capacitor.isNativePlatform()) {
|
||||
if (notification.callback) {
|
||||
await notification.callback(true);
|
||||
}
|
||||
this.$notify(
|
||||
{
|
||||
group: "alert",
|
||||
type: "info",
|
||||
title: "Finished",
|
||||
text: "Notifications are off.",
|
||||
},
|
||||
5000,
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
let subscription: PushSubscriptionJSON | null = null;
|
||||
let allGoingOff = false;
|
||||
|
||||
|
||||
@@ -67,7 +67,7 @@
|
||||
class="block w-full text-center text-md font-bold uppercase bg-blue-600 text-white mt-2 px-2 py-2 rounded-md"
|
||||
@click="handleTurnOnNotifications"
|
||||
>
|
||||
Turn on Daily Reminder
|
||||
{{ isDailyCheck ? "Turn on New Activity Notifications" : "Turn on Daily Reminder" }}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
@@ -95,6 +95,7 @@ import {
|
||||
NOTIFY_PUSH_PERMISSION_ERROR,
|
||||
NOTIFY_PUSH_SETUP_UNDERWAY,
|
||||
NOTIFY_PUSH_SUCCESS,
|
||||
NOTIFY_PUSH_SUCCESS_NEW_ACTIVITY,
|
||||
NOTIFY_PUSH_SETUP_ERROR,
|
||||
NOTIFY_PUSH_SUBSCRIPTION_ERROR,
|
||||
PUSH_NOTIFICATION_TIMEOUT_SHORT,
|
||||
@@ -758,17 +759,35 @@ export default class PushNotificationPermission extends Vue {
|
||||
time24h,
|
||||
);
|
||||
|
||||
// Determine title and body based on pushType
|
||||
const title =
|
||||
this.pushType === this.DAILY_CHECK_TITLE
|
||||
? "Daily Check-In"
|
||||
: "Daily Reminder";
|
||||
const body =
|
||||
this.pushType === this.DIRECT_PUSH_TITLE
|
||||
? this.messageInput || this.notificationMessagePlaceholder
|
||||
: "Time to check your TimeSafari activity";
|
||||
// Option A: For New Activity we do not schedule the single daily reminder here.
|
||||
// AccountViewView's callback will call scheduleNewActivityDualNotification(timeText),
|
||||
// which uses the dual schedule (prefetch + notify) only. This keeps the two notification
|
||||
// types separate and avoids a second, uncancellable reminder.
|
||||
if (this.pushType === this.DAILY_CHECK_TITLE) {
|
||||
logger.info(
|
||||
"[PushNotificationPermission] New Activity: skipping single reminder schedule; parent will schedule dual notification",
|
||||
);
|
||||
const timeText = this.notificationTimeText;
|
||||
await this.$saveSettings({ notifyingNewActivityTime: timeText });
|
||||
logger.debug(
|
||||
"[PushNotificationPermission] Settings saved: notifyingNewActivityTime",
|
||||
);
|
||||
this.$notify(
|
||||
{
|
||||
group: "alert",
|
||||
type: "success",
|
||||
title: NOTIFY_PUSH_SUCCESS_NEW_ACTIVITY.title,
|
||||
text: NOTIFY_PUSH_SUCCESS_NEW_ACTIVITY.message,
|
||||
},
|
||||
PUSH_NOTIFICATION_TIMEOUT_LONG,
|
||||
);
|
||||
this.callback(true, timeText, this.messageInput);
|
||||
return;
|
||||
}
|
||||
|
||||
// Schedule notification
|
||||
// Daily Reminder: schedule the single daily notification (native only).
|
||||
const title = "Daily Reminder";
|
||||
const body = this.messageInput || this.notificationMessagePlaceholder;
|
||||
logger.info(
|
||||
"[PushNotificationPermission] Scheduling native notification:",
|
||||
{
|
||||
|
||||
15
src/constants/backgroundJwt.ts
Normal file
15
src/constants/backgroundJwt.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* JWT lifetime for native New Activity background prefetch (`configureNativeFetcher`).
|
||||
* See doc/plan-background-jwt-pool-and-expiry.md. Confirm max `exp` with Endorser before raising.
|
||||
*/
|
||||
export const BACKGROUND_JWT_EXPIRY_DAYS = 90;
|
||||
|
||||
export const BACKGROUND_JWT_EXPIRY_SECONDS =
|
||||
BACKGROUND_JWT_EXPIRY_DAYS * 24 * 60 * 60;
|
||||
|
||||
/** Headroom for retries / tests; pool size should be ≥ expiryDays + buffer. */
|
||||
export const BACKGROUND_JWT_POOL_BUFFER = 10;
|
||||
|
||||
/** Distinct JWT strings minted per configure (duplicate-JWT / daily slot). */
|
||||
export const BACKGROUND_JWT_POOL_SIZE =
|
||||
BACKGROUND_JWT_EXPIRY_DAYS + BACKGROUND_JWT_POOL_BUFFER;
|
||||
@@ -1640,12 +1640,18 @@ export const NOTIFY_PUSH_SETUP_UNDERWAY = {
|
||||
"Setting up notifications for interesting activity, which takes about 10 seconds. If you don't see a final confirmation, check the 'Troubleshoot' page.",
|
||||
};
|
||||
|
||||
// Used in: PushNotificationPermission.vue (turnOnNotifications method - success)
|
||||
// Used in: PushNotificationPermission.vue (turnOnNotifications method - success, Daily Reminder)
|
||||
export const NOTIFY_PUSH_SUCCESS = {
|
||||
title: "Notifications On",
|
||||
message: "Daily Reminder notifications are now enabled.",
|
||||
};
|
||||
|
||||
// Used in: PushNotificationPermission.vue (turnOnNotifications method - success, New Activity only)
|
||||
export const NOTIFY_PUSH_SUCCESS_NEW_ACTIVITY = {
|
||||
title: "Notifications On",
|
||||
message: "New Activity notifications are now enabled.",
|
||||
};
|
||||
|
||||
// Used in: PushNotificationPermission.vue (turnOnNotifications method - general error)
|
||||
export const NOTIFY_PUSH_SETUP_ERROR = {
|
||||
title: "Error Setting Notification Permissions",
|
||||
|
||||
@@ -258,13 +258,14 @@ export async function logToDb(
|
||||
|
||||
try {
|
||||
const platform = PlatformServiceFactory.getInstance();
|
||||
const timestamp = new Date().toISOString();
|
||||
const todayKey = new Date().toDateString();
|
||||
|
||||
try {
|
||||
memoryLogs.push(`${new Date().toISOString()} ${message}`);
|
||||
memoryLogs.push(`${timestamp} ${message}`);
|
||||
// Insert using actual schema: date, message (no level column)
|
||||
await platform.dbExec("INSERT INTO logs (date, message) VALUES (?, ?)", [
|
||||
todayKey, // Use date string to match schema
|
||||
timestamp,
|
||||
`[${level.toUpperCase()}] ${message}`, // Include level in message
|
||||
]);
|
||||
|
||||
@@ -273,7 +274,7 @@ export async function logToDb(
|
||||
if (!lastCleanupDate || lastCleanupDate !== todayKey) {
|
||||
const sevenDaysAgo = new Date(
|
||||
new Date().getTime() - 7 * 24 * 60 * 60 * 1000,
|
||||
).toDateString(); // Use date string to match schema
|
||||
).toISOString();
|
||||
memoryLogs = memoryLogs.filter(
|
||||
(log) => log.split(" ")[0] > sevenDaysAgo,
|
||||
);
|
||||
|
||||
@@ -33,6 +33,7 @@ export interface AccountSettings {
|
||||
notifyingNewActivityTime?: string;
|
||||
notifyingReminderMessage?: string;
|
||||
notifyingReminderTime?: string;
|
||||
starredPlanHandleIds?: string[];
|
||||
reminderFastRolloverForTesting?: boolean;
|
||||
partnerApiServer?: string;
|
||||
profileImageUrl?: string;
|
||||
|
||||
@@ -4,6 +4,10 @@ import { entropyToMnemonic } from "ethereum-cryptography/bip39";
|
||||
import { wordlist } from "ethereum-cryptography/bip39/wordlists/english";
|
||||
import { HDNode } from "@ethersproject/hdnode";
|
||||
|
||||
import {
|
||||
BACKGROUND_JWT_EXPIRY_SECONDS,
|
||||
BACKGROUND_JWT_POOL_SIZE,
|
||||
} from "@/constants/backgroundJwt";
|
||||
import {
|
||||
CONTACT_IMPORT_CONFIRM_URL_PATH_TIME_SAFARI,
|
||||
createEndorserJwtForDid,
|
||||
@@ -104,6 +108,45 @@ export const accessToken = async (did?: string) => {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* JWT for native New Activity prefetch (`configureNativeFetcher` / WorkManager).
|
||||
* Uses a long `exp` (`BACKGROUND_JWT_EXPIRY_SECONDS`); do not use for ordinary
|
||||
* in-app API calls — use `getHeaders` / `accessToken` instead.
|
||||
*/
|
||||
export const accessTokenForBackgroundNotifications = async (
|
||||
did?: string,
|
||||
): Promise<string> => {
|
||||
if (!did) {
|
||||
return "";
|
||||
}
|
||||
const nowEpoch = Math.floor(Date.now() / 1000);
|
||||
const endEpoch = nowEpoch + BACKGROUND_JWT_EXPIRY_SECONDS;
|
||||
const tokenPayload = { exp: endEpoch, iat: nowEpoch, iss: did };
|
||||
return createEndorserJwtForDid(did, tokenPayload);
|
||||
};
|
||||
|
||||
/**
|
||||
* Mint {@link BACKGROUND_JWT_POOL_SIZE} distinct JWTs for native background prefetch
|
||||
* (`configureNativeFetcher` `jwtTokens`). Unique `jti` per slot; same `exp` for all.
|
||||
*/
|
||||
export async function mintBackgroundJwtTokenPool(
|
||||
did: string,
|
||||
): Promise<string[]> {
|
||||
const nowEpoch = Math.floor(Date.now() / 1000);
|
||||
const endEpoch = nowEpoch + BACKGROUND_JWT_EXPIRY_SECONDS;
|
||||
const tokens: string[] = [];
|
||||
for (let i = 0; i < BACKGROUND_JWT_POOL_SIZE; i++) {
|
||||
const tokenPayload = {
|
||||
exp: endEpoch,
|
||||
iat: nowEpoch,
|
||||
iss: did,
|
||||
jti: `${did}#bg#${i}`,
|
||||
};
|
||||
tokens.push(await createEndorserJwtForDid(did, tokenPayload));
|
||||
}
|
||||
return tokens;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract JWT from various URL formats
|
||||
* @param jwtUrlText The URL containing the JWT
|
||||
|
||||
@@ -43,10 +43,37 @@ import "./utils/safeAreaInset";
|
||||
|
||||
// Load Daily Notification plugin at startup so native performRecovery() runs at launch (rollover recovery)
|
||||
import "@timesafari/daily-notification-plugin";
|
||||
import { configureNativeFetcherIfReady } from "@/services/notifications";
|
||||
|
||||
logger.log("[Capacitor] 🚀 Starting initialization");
|
||||
logger.log("[Capacitor] Platform:", process.env.VITE_PLATFORM);
|
||||
|
||||
// Diagnostic: log DailyNotification methods from native PluginHeaders (helps debug UNIMPLEMENTED)
|
||||
type CapacitorWindow = {
|
||||
Capacitor?: {
|
||||
PluginHeaders?: Array<{ name: string; methods?: Array<{ name: string }> }>;
|
||||
};
|
||||
};
|
||||
const cap =
|
||||
typeof window !== "undefined"
|
||||
? (window as unknown as CapacitorWindow).Capacitor
|
||||
: undefined;
|
||||
if (cap?.PluginHeaders) {
|
||||
const dn = cap.PluginHeaders.find((h) => h.name === "DailyNotification");
|
||||
const methodNames = dn?.methods?.map((m) => m.name) ?? null;
|
||||
logger.log(
|
||||
"[Capacitor] DNP PluginHeaders methods:",
|
||||
methodNames ?? "DailyNotification NOT IN HEADERS",
|
||||
);
|
||||
if (methodNames && !methodNames.includes("scheduleDualNotification")) {
|
||||
logger.warn(
|
||||
"[Capacitor] scheduleDualNotification missing from PluginHeaders – native plugin may be stale; try clearing Xcode DerivedData and rebuilding",
|
||||
);
|
||||
}
|
||||
} else {
|
||||
logger.warn("[Capacitor] Capacitor.PluginHeaders not present");
|
||||
}
|
||||
|
||||
const app = initializeApp();
|
||||
|
||||
// Initialize API error handling for unhandled promise rejections
|
||||
@@ -432,11 +459,14 @@ if (
|
||||
if (isActive) {
|
||||
logger.debug("[Main] 📱 App became active, checking for shared image");
|
||||
await checkForSharedImageAndNavigate();
|
||||
// Refresh JWT for background New Activity prefetch (WorkManager cannot run JS;
|
||||
// short-lived tokens would expire between configure and T−5 fetch without this).
|
||||
await configureNativeFetcherIfReady();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Register deeplink listener after app is mounted
|
||||
// Register deeplink listener and configure native notification fetcher after app is mounted
|
||||
setTimeout(async () => {
|
||||
try {
|
||||
logger.info(
|
||||
@@ -444,6 +474,8 @@ setTimeout(async () => {
|
||||
);
|
||||
await registerDeepLinkListener();
|
||||
logger.info(`[Main] 🎉 Deep link system fully initialized!`);
|
||||
// Configure native fetcher for API-driven daily notifications (activeDid + JWT)
|
||||
await configureNativeFetcherIfReady();
|
||||
} catch (error) {
|
||||
logger.error(`[Main] ❌ Deep link system initialization failed:`, error);
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
|
||||
import { Capacitor } from "@capacitor/core";
|
||||
import { DailyNotification } from "@/plugins/DailyNotificationPlugin";
|
||||
import { REMINDER_ID_DAILY_REMINDER } from "./reminderIds";
|
||||
|
||||
/**
|
||||
* Extended type for DailyNotification that includes the actual Swift implementation
|
||||
@@ -44,10 +45,10 @@ export class NativeNotificationService implements NotificationServiceInterface {
|
||||
private readonly platformName = "native";
|
||||
|
||||
/**
|
||||
* Stable schedule/reminder ID used for schedule, cancel, and getStatus.
|
||||
* Same value on iOS and Android (plugin v1.1.2+ fixes Android reschedule with custom id).
|
||||
* Stable schedule/reminder ID for the Daily Reminder feature only.
|
||||
* New Activity uses the dual schedule (scheduleDualNotification) and does not use this ID.
|
||||
*/
|
||||
private readonly reminderId = "daily_timesafari_reminder";
|
||||
private readonly reminderId = REMINDER_ID_DAILY_REMINDER;
|
||||
|
||||
/**
|
||||
* Ensures only one scheduleDailyNotification runs at a time (no rapid successive plugin calls).
|
||||
|
||||
84
src/services/notifications/dualScheduleConfig.ts
Normal file
84
src/services/notifications/dualScheduleConfig.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
/**
|
||||
* Builds DualScheduleConfiguration for the Daily Notification plugin.
|
||||
* Used for API-driven "New Activity" notifications (prefetch + notify).
|
||||
*/
|
||||
|
||||
import type { DualScheduleConfiguration } from "@timesafari/daily-notification-plugin";
|
||||
|
||||
/** Matches `plugins.DailyNotification.networkConfig` in capacitor.config.ts */
|
||||
const CONTENT_FETCH_NETWORK = {
|
||||
timeout: 30_000,
|
||||
retryAttempts: 3,
|
||||
retryDelay: 1_000,
|
||||
} as const;
|
||||
|
||||
/**
|
||||
* Convert "HH:mm" (24h) to cron expression "minute hour * * *" (daily at that time).
|
||||
*/
|
||||
export function timeToCron(timeHHmm: string): string {
|
||||
const [h, m] = timeHHmm.split(":").map(Number);
|
||||
const hour = Math.max(0, Math.min(23, h ?? 0));
|
||||
const minute = Math.max(0, Math.min(59, m ?? 0));
|
||||
return `${minute} ${hour} * * *`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cron for 5 minutes before the given "HH:mm" (so prefetch runs before the notification).
|
||||
*/
|
||||
export function timeToCronFiveMinutesBefore(timeHHmm: string): string {
|
||||
const [h, m] = timeHHmm.split(":").map(Number);
|
||||
let hour = Math.max(0, Math.min(23, h ?? 0));
|
||||
let minute = Math.max(0, Math.min(59, m ?? 0));
|
||||
minute -= 5;
|
||||
if (minute < 0) {
|
||||
minute += 60;
|
||||
hour -= 1;
|
||||
if (hour < 0) hour += 24;
|
||||
}
|
||||
return `${minute} ${hour} * * *`;
|
||||
}
|
||||
|
||||
export interface DualScheduleConfigInput {
|
||||
/** Time in HH:mm (24h) for the user notification */
|
||||
notifyTime: string;
|
||||
/** Optional title; default "New Activity" */
|
||||
title?: string;
|
||||
/** Optional body; default describes API-driven content */
|
||||
body?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build plugin DualScheduleConfiguration for scheduleDualNotification().
|
||||
* contentFetch runs 5 minutes before notifyTime; userNotification at notifyTime.
|
||||
*/
|
||||
export function buildDualScheduleConfig(
|
||||
input: DualScheduleConfigInput,
|
||||
): DualScheduleConfiguration {
|
||||
const notifyTime = input.notifyTime || "09:00";
|
||||
const fetchCron = timeToCronFiveMinutesBefore(notifyTime);
|
||||
const notifyCron = timeToCron(notifyTime);
|
||||
return {
|
||||
contentFetch: {
|
||||
enabled: true,
|
||||
schedule: fetchCron,
|
||||
timeout: CONTENT_FETCH_NETWORK.timeout,
|
||||
retryAttempts: CONTENT_FETCH_NETWORK.retryAttempts,
|
||||
retryDelay: CONTENT_FETCH_NETWORK.retryDelay,
|
||||
callbacks: {},
|
||||
},
|
||||
userNotification: {
|
||||
enabled: true,
|
||||
schedule: notifyCron,
|
||||
title: input.title ?? "New Activity",
|
||||
body: input.body ?? "Check your starred projects and offers for updates.",
|
||||
sound: true,
|
||||
vibration: true,
|
||||
priority: "normal",
|
||||
},
|
||||
relationship: {
|
||||
autoLink: true,
|
||||
contentTimeout: 5 * 60 * 1000, // 5 minutes
|
||||
fallbackBehavior: "skip", // was "show_default"
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -17,6 +17,20 @@ export { NotificationService } from "./NotificationService";
|
||||
export { NativeNotificationService } from "./NativeNotificationService";
|
||||
export { WebPushNotificationService } from "./WebPushNotificationService";
|
||||
|
||||
export { configureNativeFetcherIfReady } from "./nativeFetcherConfig";
|
||||
export { syncStarredPlansToNativePlugin } from "./syncStarredPlansToNativePlugin";
|
||||
export {
|
||||
buildDualScheduleConfig,
|
||||
timeToCron,
|
||||
timeToCronFiveMinutesBefore,
|
||||
} from "./dualScheduleConfig";
|
||||
export type { DualScheduleConfigInput } from "./dualScheduleConfig";
|
||||
|
||||
export {
|
||||
REMINDER_ID_DAILY_REMINDER,
|
||||
REMINDER_ID_NEW_ACTIVITY,
|
||||
} from "./reminderIds";
|
||||
|
||||
export type {
|
||||
NotificationServiceInterface,
|
||||
DailyNotificationOptions,
|
||||
|
||||
98
src/services/notifications/nativeFetcherConfig.ts
Normal file
98
src/services/notifications/nativeFetcherConfig.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
/**
|
||||
* Native fetcher configuration for API-driven daily notifications.
|
||||
* Calls the Daily Notification plugin's configureNativeFetcher with
|
||||
* apiBaseUrl, activeDid, and a JWT so background workers can call the Endorser API.
|
||||
*
|
||||
* @see daily-notification-plugin docs/integration/INTEGRATION_GUIDE.md
|
||||
*/
|
||||
|
||||
import { Capacitor } from "@capacitor/core";
|
||||
import { DailyNotification } from "@/plugins/DailyNotificationPlugin";
|
||||
import { mintBackgroundJwtTokenPool } from "@/libs/crypto";
|
||||
import { PlatformServiceFactory } from "@/services/PlatformServiceFactory";
|
||||
import { logger } from "@/utils/logger";
|
||||
import { DEFAULT_ENDORSER_API_SERVER } from "@/constants/app";
|
||||
|
||||
/**
|
||||
* Configure the native notification content fetcher with API credentials.
|
||||
* Call when the app has an active identity (e.g. after login, app startup, or identity change).
|
||||
* No-op on web; requires native platform and an active DID.
|
||||
*
|
||||
* @param activeDid - Optional. If not provided, reads from active_identity table.
|
||||
* @param apiServer - Optional. If not provided, reads from settings for the active DID.
|
||||
* @returns true if configuration was attempted and succeeded, false otherwise.
|
||||
*/
|
||||
export async function configureNativeFetcherIfReady(
|
||||
activeDid?: string,
|
||||
apiServer?: string,
|
||||
): Promise<boolean> {
|
||||
if (!Capacitor.isNativePlatform()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const platform = Capacitor.getPlatform();
|
||||
if (platform !== "ios" && platform !== "android") {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
const service = PlatformServiceFactory.getInstance();
|
||||
let did = activeDid;
|
||||
let apiBaseUrl = apiServer;
|
||||
|
||||
if (!did) {
|
||||
const row = await service.dbGetOneRow(
|
||||
"SELECT activeDid FROM active_identity WHERE id = 1",
|
||||
);
|
||||
if (!row || !row[0]) {
|
||||
logger.debug(
|
||||
"[nativeFetcherConfig] No active DID; skipping native fetcher config",
|
||||
);
|
||||
return false;
|
||||
}
|
||||
did = String(row[0]);
|
||||
}
|
||||
|
||||
if (!apiBaseUrl) {
|
||||
const settingsRow = await service.dbGetOneRow(
|
||||
"SELECT apiServer FROM settings WHERE id = 1 OR accountDid = ? LIMIT 1",
|
||||
[did],
|
||||
);
|
||||
apiBaseUrl = settingsRow?.[0]
|
||||
? String(settingsRow[0])
|
||||
: DEFAULT_ENDORSER_API_SERVER;
|
||||
}
|
||||
|
||||
const jwtTokens = await mintBackgroundJwtTokenPool(did);
|
||||
const jwtToken = jwtTokens[0] ?? "";
|
||||
if (!jwtToken) {
|
||||
logger.warn(
|
||||
"[nativeFetcherConfig] No JWT for native fetcher; API-driven notifications may fail",
|
||||
);
|
||||
}
|
||||
|
||||
if (!DailyNotification?.configureNativeFetcher) {
|
||||
logger.warn(
|
||||
"[nativeFetcherConfig] Plugin configureNativeFetcher not available",
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
await DailyNotification.configureNativeFetcher({
|
||||
apiBaseUrl:
|
||||
apiBaseUrl?.trim().replace(/\/$/, "") ?? DEFAULT_ENDORSER_API_SERVER,
|
||||
activeDid: did,
|
||||
jwtToken,
|
||||
jwtTokens,
|
||||
});
|
||||
logger.info(
|
||||
"[nativeFetcherConfig] Native fetcher configured (JWT pool size=" +
|
||||
jwtTokens.length +
|
||||
")",
|
||||
);
|
||||
return true;
|
||||
} catch (error) {
|
||||
logger.error("[nativeFetcherConfig] configureNativeFetcher failed:", error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
13
src/services/notifications/reminderIds.ts
Normal file
13
src/services/notifications/reminderIds.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* Stable reminder/schedule IDs for native daily notifications.
|
||||
* Keeps Daily Reminder and New Activity distinct so we can support both on
|
||||
* and cancel only one. New Activity uses the dual schedule (scheduleDualNotification)
|
||||
* only; this ID is for reference/future use (e.g. if we ever add a single-reminder
|
||||
* fallback for New Activity).
|
||||
*/
|
||||
|
||||
/** ID for the single daily reminder (Daily Reminder feature). Used by NativeNotificationService. */
|
||||
export const REMINDER_ID_DAILY_REMINDER = "daily_timesafari_reminder";
|
||||
|
||||
/** ID for New Activity. Not used for scheduling (we use dual schedule only); kept for clarity and future use. */
|
||||
export const REMINDER_ID_NEW_ACTIVITY = "new_activity_timesafari";
|
||||
30
src/services/notifications/syncStarredPlansToNativePlugin.ts
Normal file
30
src/services/notifications/syncStarredPlansToNativePlugin.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { Capacitor } from "@capacitor/core";
|
||||
|
||||
import { DailyNotification } from "@/plugins/DailyNotificationPlugin";
|
||||
import { logger } from "@/utils/logger";
|
||||
|
||||
/**
|
||||
* Pushes starred plan handle IDs to the native Daily Notification plugin so
|
||||
* Android TimeSafariNativeFetcher uses the current list for prefetch
|
||||
* (plansLastUpdatedBetween planIds).
|
||||
*
|
||||
* No-op on web. Ignores UNIMPLEMENTED when the plugin omits the method on some builds.
|
||||
*/
|
||||
export async function syncStarredPlansToNativePlugin(
|
||||
planIds: string[],
|
||||
): Promise<void> {
|
||||
if (!Capacitor.isNativePlatform()) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await DailyNotification.updateStarredPlans({ planIds });
|
||||
} catch (e: unknown) {
|
||||
if ((e as { code?: string })?.code === "UNIMPLEMENTED") {
|
||||
return;
|
||||
}
|
||||
logger.warn(
|
||||
"[syncStarredPlansToNativePlugin] updateStarredPlans failed",
|
||||
e,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -164,10 +164,10 @@ async function logToDatabase(
|
||||
|
||||
try {
|
||||
const platform = PlatformServiceFactory.getInstance();
|
||||
const todayKey = new Date().toDateString();
|
||||
const timestamp = new Date().toISOString();
|
||||
|
||||
await platform.dbExec("INSERT INTO logs (date, message) VALUES (?, ?)", [
|
||||
todayKey,
|
||||
timestamp,
|
||||
`[${level.toUpperCase()}] ${message}`,
|
||||
]);
|
||||
} catch (error) {
|
||||
|
||||
@@ -139,41 +139,61 @@
|
||||
class="w-full text-md bg-gradient-to-b from-blue-400 to-blue-700 shadow-[inset_0_-1px_0_0_rgba(0,0,0,0.5)] text-white px-4 py-2 rounded-md"
|
||||
@click="editReminderNotification"
|
||||
>
|
||||
Edit Notification Details…
|
||||
Edit Daily Reminder…
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div v-if="false" class="mt-4 flex items-center justify-between">
|
||||
<div class="flex items-center justify-between mt-4 mb-2">
|
||||
<!-- label -->
|
||||
<div>
|
||||
New Activity Notification
|
||||
<font-awesome
|
||||
icon="question-circle"
|
||||
<button
|
||||
class="text-slate-400 fa-fw cursor-pointer"
|
||||
aria-label="Learn more about New Activity notifications"
|
||||
@click.stop="showNewActivityNotificationInfo"
|
||||
/>
|
||||
>
|
||||
<font-awesome icon="question-circle" aria-hidden="true" />
|
||||
</button>
|
||||
</div>
|
||||
<!-- toggle -->
|
||||
<div
|
||||
class="relative ml-2 cursor-pointer"
|
||||
@click="showNewActivityNotificationChoice()"
|
||||
role="switch"
|
||||
:aria-checked="notifyingNewActivity"
|
||||
aria-label="Toggle New Activity notifications"
|
||||
tabindex="0"
|
||||
@click.stop.prevent="showNewActivityNotificationChoice()"
|
||||
>
|
||||
<!-- input -->
|
||||
<input
|
||||
v-model="notifyingNewActivity"
|
||||
:checked="notifyingNewActivity"
|
||||
type="checkbox"
|
||||
class="sr-only"
|
||||
readonly
|
||||
@click.stop.prevent
|
||||
@change.stop.prevent
|
||||
/>
|
||||
<!-- line -->
|
||||
<div class="block bg-slate-500 w-14 h-8 rounded-full"></div>
|
||||
<!-- dot -->
|
||||
<div
|
||||
class="dot absolute left-1 top-1 bg-slate-400 w-6 h-6 rounded-full transition"
|
||||
></div>
|
||||
</div>
|
||||
</div>
|
||||
<div v-if="notifyingNewActivityTime" class="w-full text-right">
|
||||
{{ notifyingNewActivityTime.replace(" ", " ") }}
|
||||
<div v-if="notifyingNewActivity" class="w-full">
|
||||
<div
|
||||
class="text-sm text-slate-500 mb-2 bg-white rounded px-3 py-2 border border-slate-200"
|
||||
>
|
||||
<div>
|
||||
<b>Time:</b> {{ notifyingNewActivityTime.replace(" ", " ") }}
|
||||
</div>
|
||||
</div>
|
||||
<div class="mt-2 text-center">
|
||||
<button
|
||||
class="w-full text-md bg-gradient-to-b from-blue-400 to-blue-700 shadow-[inset_0_-1px_0_0_rgba(0,0,0,0.5)] text-white px-4 py-2 rounded-md"
|
||||
@click="editNewActivityNotification"
|
||||
>
|
||||
Edit New Activity Notification…
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mt-2 text-center">
|
||||
<router-link class="text-sm text-blue-500" to="/help-notifications">
|
||||
@@ -807,7 +827,13 @@ import { createNotifyHelpers, TIMEOUTS } from "@/utils/notify";
|
||||
import { ACCOUNT_VIEW_CONSTANTS } from "@/constants/accountView";
|
||||
import { showSeedPhraseReminder } from "@/utils/seedPhraseReminder";
|
||||
import { AccountSettings, isApiError } from "@/interfaces/accountView";
|
||||
import { NotificationService } from "@/services/notifications";
|
||||
import {
|
||||
NotificationService,
|
||||
configureNativeFetcherIfReady,
|
||||
buildDualScheduleConfig,
|
||||
syncStarredPlansToNativePlugin,
|
||||
} from "@/services/notifications";
|
||||
import { DailyNotification } from "@/plugins/DailyNotificationPlugin";
|
||||
// Profile data interface (inlined from ProfileService)
|
||||
interface ProfileData {
|
||||
description: string;
|
||||
@@ -1100,6 +1126,14 @@ export default class AccountViewView extends Vue {
|
||||
this.warnIfTestServer = !!settings.warnIfTestServer;
|
||||
this.webPushServer = settings.webPushServer || this.webPushServer;
|
||||
this.webPushServerInput = settings.webPushServer || this.webPushServerInput;
|
||||
|
||||
if (Capacitor.isNativePlatform() && this.activeDid) {
|
||||
void configureNativeFetcherIfReady(this.activeDid);
|
||||
if (this.notifyingNewActivity) {
|
||||
const planIds = settings?.starredPlanHandleIds ?? [];
|
||||
void syncStarredPlansToNativePlugin(planIds);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// call fn, copy text to the clipboard, then redo fn after 2 seconds
|
||||
@@ -1193,11 +1227,24 @@ export default class AccountViewView extends Vue {
|
||||
});
|
||||
this.notifyingNewActivity = true;
|
||||
this.notifyingNewActivityTime = timeText;
|
||||
if (Capacitor.isNativePlatform()) {
|
||||
await this.scheduleNewActivityDualNotification(timeText);
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
this.notify.notificationOff(DAILY_CHECK_TITLE, async (success) => {
|
||||
if (success) {
|
||||
if (
|
||||
Capacitor.isNativePlatform() &&
|
||||
DailyNotification?.cancelDualSchedule
|
||||
) {
|
||||
try {
|
||||
await DailyNotification.cancelDualSchedule();
|
||||
} catch (e) {
|
||||
logger.error("[AccountViewView] cancelDualSchedule failed:", e);
|
||||
}
|
||||
}
|
||||
await this.$saveSettings({
|
||||
notifyingNewActivityTime: "",
|
||||
});
|
||||
@@ -1208,6 +1255,100 @@ export default class AccountViewView extends Vue {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure native fetcher, sync starred plans, and schedule API-driven dual notification.
|
||||
*/
|
||||
async scheduleNewActivityDualNotification(notifyTime: string): Promise<void> {
|
||||
const plugin = DailyNotification as unknown as {
|
||||
scheduleDualNotification?: (opts: { config: unknown }) => Promise<void>;
|
||||
};
|
||||
if (!plugin.scheduleDualNotification) {
|
||||
logger.warn(
|
||||
"[AccountViewView] scheduleDualNotification not available on this device",
|
||||
);
|
||||
this.notify.error(
|
||||
"New Activity scheduling is not available on this device. Please update the app.",
|
||||
TIMEOUTS.STANDARD,
|
||||
);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const time24h = this.parseTimeTo24Hour(notifyTime);
|
||||
await configureNativeFetcherIfReady(this.activeDid);
|
||||
const settings = await this.$accountSettings();
|
||||
const planIds = settings?.starredPlanHandleIds ?? [];
|
||||
await syncStarredPlansToNativePlugin(planIds);
|
||||
const config = buildDualScheduleConfig({ notifyTime: time24h });
|
||||
// Diagnostic: log what Capacitor sees at call time (helps debug UNIMPLEMENTED)
|
||||
const cap = (typeof window !== "undefined" &&
|
||||
(
|
||||
window as unknown as {
|
||||
Capacitor?: {
|
||||
PluginHeaders?: Array<{
|
||||
name: string;
|
||||
methods?: Array<{ name: string }>;
|
||||
}>;
|
||||
};
|
||||
}
|
||||
).Capacitor) as
|
||||
| {
|
||||
PluginHeaders?: Array<{
|
||||
name: string;
|
||||
methods?: Array<{ name: string }>;
|
||||
}>;
|
||||
}
|
||||
| undefined;
|
||||
const dnHeader = cap?.PluginHeaders?.find(
|
||||
(h) => h.name === "DailyNotification",
|
||||
);
|
||||
const methodsAtCall = dnHeader?.methods?.map((m) => m.name) ?? null;
|
||||
logger.warn(
|
||||
"[AccountViewView] Before scheduleDualNotification, PluginHeaders methods:",
|
||||
methodsAtCall ?? "DailyNotification not in headers",
|
||||
);
|
||||
if (
|
||||
methodsAtCall &&
|
||||
!methodsAtCall.includes("scheduleDualNotification")
|
||||
) {
|
||||
logger.warn(
|
||||
"[AccountViewView] scheduleDualNotification missing from PluginHeaders at call time – bridge may be stale for this run",
|
||||
);
|
||||
}
|
||||
await plugin.scheduleDualNotification!({ config });
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
"[AccountViewView] scheduleNewActivityDualNotification failed:",
|
||||
error,
|
||||
);
|
||||
const err = error as {
|
||||
code?: string;
|
||||
errorMessage?: string;
|
||||
message?: string;
|
||||
};
|
||||
const code = err?.code;
|
||||
const msg = err?.errorMessage ?? err?.message ?? "";
|
||||
if (code === "UNIMPLEMENTED") {
|
||||
this.notify.error(
|
||||
"New Activity scheduling is not yet available on this device. Please update the app when support is added.",
|
||||
TIMEOUTS.STANDARD,
|
||||
);
|
||||
} else if (
|
||||
msg.includes("BGTaskSchedulerErrorDomain") ||
|
||||
msg.includes("error 1")
|
||||
) {
|
||||
this.notify.error(
|
||||
"New Activity scheduling needs a real device and Background App Refresh enabled. It does not work in Simulator.",
|
||||
TIMEOUTS.STANDARD,
|
||||
);
|
||||
} else {
|
||||
this.notify.error(
|
||||
"Could not schedule New Activity notification. Please try again.",
|
||||
TIMEOUTS.STANDARD,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async showReminderNotificationInfo(): Promise<void> {
|
||||
this.notify.confirm(
|
||||
ACCOUNT_VIEW_CONSTANTS.NOTIFICATIONS.REMINDER_INFO,
|
||||
@@ -1405,6 +1546,91 @@ export default class AccountViewView extends Vue {
|
||||
}, 150);
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit existing New Activity notification time.
|
||||
* Opens the dialog with current time; on success updates dual schedule via
|
||||
* updateDualScheduleConfig when available (plugin v2.1.0+), else scheduleDualNotification.
|
||||
*/
|
||||
async editNewActivityNotification(): Promise<void> {
|
||||
const dialog = this.$refs
|
||||
.pushNotificationPermission as PushNotificationPermission;
|
||||
|
||||
dialog.open(
|
||||
DAILY_CHECK_TITLE,
|
||||
async (success: boolean, timeText: string) => {
|
||||
if (!success) return;
|
||||
if (Capacitor.isNativePlatform()) {
|
||||
const time24h = this.parseTimeTo24Hour(timeText);
|
||||
const config = buildDualScheduleConfig({ notifyTime: time24h });
|
||||
const plugin = DailyNotification as unknown as {
|
||||
updateDualScheduleConfig?: (opts: {
|
||||
config: unknown;
|
||||
}) => Promise<void>;
|
||||
scheduleDualNotification?: (opts: {
|
||||
config: unknown;
|
||||
}) => Promise<void>;
|
||||
};
|
||||
try {
|
||||
if (plugin.updateDualScheduleConfig) {
|
||||
await plugin.updateDualScheduleConfig({ config });
|
||||
} else {
|
||||
await this.scheduleNewActivityDualNotification(timeText);
|
||||
}
|
||||
} catch (e) {
|
||||
logger.warn(
|
||||
"[AccountViewView] updateDualScheduleConfig failed, falling back to scheduleDualNotification:",
|
||||
e,
|
||||
);
|
||||
try {
|
||||
await this.scheduleNewActivityDualNotification(timeText);
|
||||
} catch (fallbackError) {
|
||||
logger.error(
|
||||
"[AccountViewView] editNewActivityNotification schedule failed:",
|
||||
fallbackError,
|
||||
);
|
||||
this.notify.error(
|
||||
"Could not update New Activity time. Please try again.",
|
||||
TIMEOUTS.STANDARD,
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
await this.$saveSettings({ notifyingNewActivityTime: timeText });
|
||||
this.notifyingNewActivityTime = timeText;
|
||||
this.notify.success(
|
||||
"New Activity notification time updated.",
|
||||
TIMEOUTS.STANDARD,
|
||||
);
|
||||
},
|
||||
{ skipSchedule: true },
|
||||
);
|
||||
|
||||
// Pre-populate the dialog with current New Activity time
|
||||
setTimeout(() => {
|
||||
const timeMatch = this.notifyingNewActivityTime.match(
|
||||
/(\d+):(\d+)\s*(AM|PM)/i,
|
||||
);
|
||||
if (timeMatch) {
|
||||
let hour = parseInt(timeMatch[1], 10);
|
||||
const minute = timeMatch[2];
|
||||
const isAm = timeMatch[3].toUpperCase() === "AM";
|
||||
if (hour === 12) {
|
||||
hour = 12;
|
||||
} else if (hour > 12) {
|
||||
hour = hour - 12;
|
||||
}
|
||||
const dialogComponent =
|
||||
dialog as unknown as PushNotificationPermissionRef;
|
||||
if (dialogComponent) {
|
||||
dialogComponent.hourInput = hour.toString();
|
||||
dialogComponent.minuteInput = minute;
|
||||
dialogComponent.hourAm = isAm;
|
||||
}
|
||||
}
|
||||
}, 150);
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle dev-only 10-minute rollover for daily reminder. Saves the setting and,
|
||||
* if reminder is already on, reschedules so the plugin uses the new interval.
|
||||
|
||||
@@ -647,6 +647,7 @@ import * as serverUtil from "../libs/endorserServer";
|
||||
import { retrieveAccountDids } from "../libs/util";
|
||||
import { copyToClipboard } from "../services/ClipboardService";
|
||||
import { logger } from "../utils/logger";
|
||||
import { syncStarredPlansToNativePlugin } from "@/services/notifications";
|
||||
import { PlatformServiceMixin } from "@/utils/PlatformServiceMixin";
|
||||
import { createNotifyHelpers, TIMEOUTS } from "@/utils/notify";
|
||||
|
||||
@@ -1545,6 +1546,9 @@ export default class ProjectViewView extends Vue {
|
||||
);
|
||||
if (result) {
|
||||
this.isStarred = true;
|
||||
if (settings.notifyingNewActivityTime) {
|
||||
void syncStarredPlansToNativePlugin(newStarredIds);
|
||||
}
|
||||
} else {
|
||||
// eslint-disable-next-line no-console
|
||||
logger.error("Got a bad result from SQL update to star a project.");
|
||||
@@ -1567,6 +1571,9 @@ export default class ProjectViewView extends Vue {
|
||||
);
|
||||
if (result) {
|
||||
this.isStarred = false;
|
||||
if (settings.notifyingNewActivityTime) {
|
||||
void syncStarredPlansToNativePlugin(updatedIds);
|
||||
}
|
||||
} else {
|
||||
// eslint-disable-next-line no-console
|
||||
logger.error("Got a bad result from SQL update to unstar a project.");
|
||||
|
||||
Reference in New Issue
Block a user