Permissions Denied with Firebase Messaging for notifications

I've checked my service permissions and theye are fine, the users have read/write permissions for EventCards so long as they are in the userID field. I've enabled the cloud messaging API.

Here is the error:

W/Firestore( 7028): (24.7.0) [Firestore]: Listen for Query(target=Query(Users/6jmDcBDb0aNy5XJ9gVwu9tFbyhm2 order by __name__);limitType=LIMIT_TO_FIRST) failed: Status{code=PERMISSION_DENIED, description=Missing or insufficient permissions., cause=null}
E/flutter ( 7028): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: [cloud_firestore/permission-denied] The caller does not have permission to execute the specified operation.
E/flutter ( 7028): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:652:7)
E/flutter ( 7028): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:310:18)
E/flutter ( 7028): <asynchronous suspension>
E/flutter ( 7028): #2      MethodChannel.invokeMapMethod (package:flutter/src/services/platform_channel.dart:510:43)
E/flutter ( 7028): <asynchronous suspension>
E/flutter ( 7028): #3      MethodChannelDocumentReference.get (package:cloud_firestore_platform_interface/src/method_channel/method_channel_document_reference.dart:69:42)
E/flutter ( 7028): <asynchronous suspension>
E/flutter ( 7028): #4      _JsonDocumentReference.get (package:cloud_firestore/src/document_reference.dart:148:7)
E/flutter ( 7028): <asynchronous suspension>
E/flutter ( 7028): 

Here is the code that actions it:

class _EventCardState extends State<EventCard> {
  bool _isCancelled = false;

  Future<void> _cancelEvent(BuildContext context) async {
    final currentUserUid = FirebaseAuth.instance.currentUser!.uid;
    final eventRef = FirebaseFirestore.instance.collection('Events').doc(widget.documentId);
    final eventSnapshot = await eventRef.get();
    final userIds = List<String>.from(eventSnapshot.data()?['userID'] ?? []);
    final cancelledUserIds = List<String>.from(eventSnapshot.data()?['cancelledUserIDs'] ?? []);
    final userIDs = List<String>.from(eventSnapshot.data()?['userID'] ?? []);
// Create a notification payload
    final notificationPayload = {
      'title': 'Event Cancelled',
      'body': 'The event "${widget.titleEvent}" has been cancelled.',
    };

    if (userIds.contains(currentUserUid)) {
      setState(() {
        _isCancelled = !_isCancelled;
      });

      if (_isCancelled) {
        cancelledUserIds.add(currentUserUid);
      } else {
        cancelledUserIds.remove(currentUserUid);
      }

      await eventRef.update({'cancelledUserIDs': cancelledUserIds});

      if (cancelledUserIds.length == userIds.length &&
          cancelledUserIds.toSet().containsAll(userIds.toSet())) {
        await showDialog(
          context: context,
          builder: (context) => AlertDialog(
            title: Text('${widget.titleEvent} has been cancelled'),
          ),
        );

        for (String userID in userIDs) {
          FirebaseFirestore.instance
              .collection('Users')
              .doc(userID)
              .get()
              .then((userDocument) {
            if (userDocument.exists) {
              final fcmToken = userDocument.data()!['fcmToken'];
              FirebaseMessaging.instance.sendMessage(to: fcmToken, data: notificationPayload);
            }
          });
        }

        await eventRef.delete();
      } else {
        await showDialog(
          context: context,
          builder: (context) => AlertDialog(
            title: Text(
                'You have voted to ${_isCancelled ? 'cancel ' : 'uncancel '}${widget.titleEvent}'),
          ),
        );

        await eventRef.update({'cancelledUserIDs': cancelledUserIds});
      }
    }
  }

I've stopped and re-run the app, updated the dependencies, and checked permissions. The only thing I'm not sure of is my fcmToken handling.

EDIT:

See rules below

rules_version = '2'; ​ service cloud.firestore {   match /databases/{database}/documents {

match /Events/{docId} { 
  allow read, write: if request.auth != null;
}   } }


Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation