
I will share code in Parts Due to limits & I am not able to create any subscriber & Getting errors

( import 'package:flutter/material.dart'; import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:appwrite/appwrite.dart'; import 'package:appwrite/models.dart';
void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler); runApp(MyApp()); }
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async { await Firebase.initializeApp(); print("Handling a background message: ${message.messageId}"); }
class MyApp extends StatefulWidget { @override _MyAppState createState() => _MyAppState(); }
)

class _MyAppState extends State<MyApp> { late Client client; late Messaging messaging; String? userId;
@override void initState() { super.initState(); _initializeAppwrite(); _initializeFCM(); _subscribeToTopic('66cc43930014666fb982'); }
void _initializeAppwrite() { client = Client() ..setEndpoint('https://cloud.appwrite.io/v1') ..setProject('643588ac5b13906fc980'); messaging = Messaging(client); }
Future<void> _loginAnonymously() async { try { final account = Account(client); final response = await account.createAnonymousSession(); userId = response.$id; print('User ID: $userId'); } catch (e) { print('Error logging in anonymously: $e'); } }

Future<void> _initializeFCM() async { FirebaseMessaging messaging = FirebaseMessaging.instance;
// Request permission for iOS
NotificationSettings settings = await messaging.requestPermission(
alert: true,
badge: true,
sound: true,
);
if (settings.authorizationStatus == AuthorizationStatus.authorized) {
print('User granted permission');
} else if (settings.authorizationStatus == AuthorizationStatus.provisional) {
print('User granted provisional permission');
} else {
print('User declined or has not accepted permission');
}
// Get the token for this device
String? token = await messaging.getToken();
print("FCM Token: $token");
// Listen for messages
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('Received a message while in the foreground!');
print('Message data: ${message.data}');
if (message.notification != null) {
print('Message also contained a notification: ${message.notification}');
}
});
}
Future<void> _subscribeToTopic(String topicId) async { await _loginAnonymously();
if (userId != null) {
try {
await messaging.createSubscriber(
topicId: topicId,
subscriberId: userId!,
targetId: 'your-target-id',
);
print('Subscribed to topic: $topicId');
} catch (e) {
print('Error subscribing to topic: $e');
}
} else {
print('Failed to obtain user ID');
}
}
@override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: Text('Appwrite Messaging with FCM'), ), body: Center( child: Text('Push Notifications Setup Complete'), ), ), ); } }
Recommended threads
- my database attribute stuck in processin...
when i created attributes in collection 3 of those attributes become "processing", and they are not updating, the worst thing is that i cant even delete them s...
- Is Quick Start for function creation wor...
I am trying to create a Node.js function using the Quick Start feature. It fails and tells me that it could not locate the package.json file. Isn't Quick Start ...
- Forever Processing Issue
I encountered an issue when creating attributes in the collections . if you create an attribute of type string for example and choose a size of 200 or 250 or a...
