Repository navigation
Description
Description
I'm experiencing an issue with Appwrite Messaging (using FCM) where a user receives two notifications simultaneously on the same device. In the Appwrite console, the targets appear correct and only one notification is scheduled. However, when the scheduler runs, duplicate notifications are triggered.
Different cases
-
Case 1:
- Run the scheduler without calling
setRemindersForNewSession. - Result: The bug occurs and the user receives duplicate notifications.
- Run the scheduler without calling
-
Case 2:
- Run the scheduler (triggered via cron) and then call
setRemindersForNewSession. - Result: The user receives only one notification.
- Run the scheduler (triggered via cron) and then call
-
Case 3:
- Call
setRemindersForNewSessionbefore running the scheduler. - Result: The duplicate notification issue persists (or at least the bug is not fixed).
- Call
Note: When the app is opened,
setRemindersForNewSessionis automatically called. It appears that the timing of calling this function influences whether the bug occurs.
Expected Behavior
The user should receive only one notification per scheduled event.
Actual Behavior
The user receives two notifications simultaneously, despite only one notification being scheduled in the console.
Environment
- Library:
dart_appwrite - Service: Appwrite Messaging
- Language: Dart
Code
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:dart_appwrite/dart_appwrite.dart';
import 'package:dart_appwrite/models.dart';
import 'package:starter_template/core/extensions.dart';
import 'package:starter_template/core/response_model.dart';
// This Appwrite function will be executed every time your function is triggered
Future<dynamic> main(final context) async {
final client = Client()
.setEndpoint(Platform.environment['APPWRITE_FUNCTION_API_ENDPOINT'] ?? '')
.setProject(Platform.environment['APPWRITE_FUNCTION_PROJECT_ID'] ?? '')
.setKey(context.req.headers['x-appwrite-key'] ?? '');
final users = Users(client);
final messaging = Messaging(client);
final trigger = context.req.headers['x-appwrite-trigger'] ?? '';
context.log('trigger: $trigger');
if (context.req.method == 'POST' &&
context.req.body != null &&
context.req.path == "/setRemindersForNewSession") {
return await handleRemindersOnNewSession(context, users, messaging);
} else if (trigger == 'schedule') {
return await _scheduleUsersDailyReminders(context, users, messaging);
}
}
Future<dynamic> _scheduleUsersDailyReminders(
context, Users users, Messaging messaging) async {
try {
context.log('load all users');
final UserList userList = await users.list();
context.log('fetched users!');
context.log('users: ${jsonEncode(userList.users.map((e) => e.toMap().toString(),).toList())}');
for (User user in userList.users) {
try {
await setRemindersForUser(context, user, messaging);
} catch (e) {
context.log('error setting reminders for user ${user.$id}: $e');
}
}
return context.res.text('Task completed!');
} catch (e) {
context.log(e.toString());
return context.res.text(e.toString());
}
}
// message id containing user id and date (only year, month, day)
String _generateMessageId(User user, DateTime next9PMUtc) =>
'${user.$id}-${next9PMUtc.year}-${next9PMUtc.month}-${next9PMUtc.day}';
dynamic handleRemindersOnNewSession(
context, Users users, Messaging messaging) async {
final userId = context.req.bodyJson['userId'];
if (userId == null || userId.isEmpty) {
return context.res.text('userId is empty', 400);
}
late User user;
try {
user = await users.get(userId: userId);
} on Exception catch (e) {
return context.res.text(e.toString(), 400);
}
final result = await setRemindersForUser(context, user, messaging);
return result.when(
success: (_) {
return context.res.text('success', 200);
},
failed: (exception) {
return context.res.text(exception.toString(), 400);
},
);
}
Future<ResponseModel> setRemindersForUser(
context, User user, Messaging messaging) async {
context.log('processing user ${user.name}');
// Retrieve user's timezone offset
final timezoneOffset = user.prefs.data['timezone'];
if (timezoneOffset != null) {
context.log('retrieved timezone offset: $timezoneOffset');
final isOffsetNegative = timezoneOffset.startsWith('-');
final pureOffsetDuration = timezoneOffset.replaceAll('-', '').split('.').first;
context.log('Pure offset duration: $pureOffsetDuration');
final offsetHour = int.parse(pureOffsetDuration.split(':')[0]);
final offsetMin = int.parse(pureOffsetDuration.split(':')[1]);
final offsetSec = int.parse(pureOffsetDuration.split(':')[2]);
final offsetDuration = Duration(
hours: offsetHour,
minutes: offsetMin,
seconds: offsetSec,
);
context.log('Offset duration: $offsetDuration');
final now = DateTime.now().toUtc();
context.log('Current UTC time: $now');
final userTime = isOffsetNegative
? now.subtract(offsetDuration)
: now.add(offsetDuration);
context.log('Current user time: $userTime');
DateTime next9PM = DateTime(userTime.year, userTime.month, userTime.day, 21);
if (userTime.isAfter(next9PM)) {
next9PM = next9PM.add(Duration(days: 1));
}
context.log('Next 9 PM user time: $next9PM');
// Convert next9PM to UTC
final next9PMUtc = next9PM.subtract(offsetDuration);
final messageId = _generateMessageId(user, next9PMUtc);
late Function({
required String messageId,
required String title,
required String body,
List<String>? topics,
List<String>? users,
List<String>? targets,
Map? data,
String? action,
String? image,
String? icon,
String? sound,
String? color,
String? tag,
bool? draft,
String? scheduledAt
}) scheduler;
try {
context.log('check existing message');
await messaging.getMessage(messageId: messageId);
// If no error, the message exists - update it
scheduler = messaging.updatePush;
context.log('found existing message');
} catch (e) {
// If an error is thrown, the message does not exist - create it
context.log('no existing message found');
scheduler = messaging.createPush;
context.log('should create new message');
context.log(e.toString());
}
context.log('scheduling push notification');
final userPushTargets = user.targets.where((element) => element.providerType == "push").toList();
context.log('user push targets: ${jsonEncode(userPushTargets.map((e) => e.toMap(),).toList())}');
try {
final content = dynamicNotifications.random;
final result = await scheduler(
messageId: messageId,
title: content.$1,
body: content.$2,
scheduledAt: next9PMUtc.toIso8601String(),
targets: userPushTargets.map((e) => e.$id,).toList(),
);
context.log('scheduled push notification!: $messageId - ${result.toMap()}');
return ResponseModel.success(null);
} catch (e) {
context.log(e.toString());
return ResponseModel.failed(message: e.toString());
}
}
return ResponseModel.failed(message: 'Timezone offset not found');
}
List<(String, String)> dynamicNotifications = [
// List of dynamic notifications...
];Any help investigating or fixing this behavior would be greatly appreciated. Please let me know if you need further details or logs!
Activity
fix(messaging): replace a session's push target when its device token…
HarshMN2345 commentedon Sep 23, 2026
This shouldn’t be a problem anymore.