Skip to content
Back

[Not Solved] Push Notifications With Appwrite using FCM Provider

  • 1
  • 3
  • Android
  • Flutter
  • Messaging
  • Cloud
Arif Qayoom
29 Aug, 2024, 09:27

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

TL;DR
Issue with sending push notifications via Appwrite using FCM provider despite various attempts. Confusion around targetId and anonymous user limitations. Solution not provided in the thread.
Arif Qayoom
29 Aug, 2024, 09:28

( 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(); }

)

Arif Qayoom
29 Aug, 2024, 09:29

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'); } }

Arif Qayoom
29 Aug, 2024, 09:29

Future<void> _initializeFCM() async { FirebaseMessaging messaging = FirebaseMessaging.instance;

TypeScript
// 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();

TypeScript
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'), ), ), ); } }

29 Aug, 2024, 11:06

targetId: 'your-target-id', user have this target?

29 Aug, 2024, 12:38

How can I achieve this can You explain Sir I Am little bit confused about this

29 Aug, 2024, 12:50

in "targetId" parameter you must pass the targetId associate to user, for example, user.targets.where((target) => target.providerType == 'push').first.$id

29 Aug, 2024, 12:51

'your-target-id' isn't a valid target id

29 Aug, 2024, 12:52

BTW, if you surround your code with three backticks (), it'll format in Discord as code

1
29 Aug, 2024, 12:54

😃 I was not able to send the message coz the code was long, Btw instead please provide a solution so I can continue😂❤️

29 Aug, 2024, 12:56

I will try this & update you in a time Thanks❤️

29 Aug, 2024, 14:05

Again Not working

29 Aug, 2024, 14:42

how did you do it?

29 Aug, 2024, 14:43

you must use this for example:

29 Aug, 2024, 14:43

target id, and the path on getAccount is user.targets[].$id

29 Aug, 2024, 14:44

or this,

29 Aug, 2024, 14:45

These are examples to help you understand the data to use

29 Aug, 2024, 15:48

But I used Anonymous login... for my app

29 Aug, 2024, 16:24

i think you can't send message to an anonymous user

29 Aug, 2024, 16:26

targetId contains the data for send the message, and it is a required property on createSubscriber

29 Aug, 2024, 16:52

Oo may b! that's why I am not able to do this stuff!

2
2 Sep, 2024, 15:25

[Not Solved] Push Notifications With Appwrite using FCM Provider

4 Sep, 2024, 15:07

I think you can't push the notification to the anonymous user as it need FCM token which is being generated by firebase after you signup.

4 Sep, 2024, 16:52

I would assume it would be possible, as long as the (anonymous) session is active?

3
4 Sep, 2024, 17:13

If this is possible than I would love to hear the possible solution of this

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more