Firebase Cloud Messaging in flutter function works but didn't receive notification

521

It appears that my token was going as null in fcm function rectified it.

Share:
521
Pushpendra Pal
Author by

Pushpendra Pal

Updated on December 13, 2022

Comments

  • Pushpendra Pal
    Pushpendra Pal over 1 year

    I want to send fcm notication to device but the problem is it's hitting api success fully but the push notification is not send to app.

     Future<bool> callOnFcmApiSendPushNotifications(
          Future<String> userToken, Message message) async {
    
        print(message.message);
        print(userToken);
        final postUrl = 'https://fcm.googleapis.com/fcm/send';
        final data = {
          "notification": {"body": message.message, "title": "New Notification"},
          "priority": "high",
          "data": {
            "click_action": "FLUTTER_NOTIFICATION_CLICK",
            "id": "1",
            "status": "done"
          },
          "to": "$userToken"
        };
    
        final headers = {
          'content-type': 'application/json',
          'Authorization':
              'key=Server key'
        };
    
        print('hit till response');
    
        final response = await Dio()
            .post(postUrl, data: data, options: Options(headers: headers));
        print(response.statusCode);
        if (response.statusCode == 200) {
          // on success do sth
          print('test ok push CFM');
          return true;
        } else {
          print(' CFM error');
          // on failure do sth
          return false;
        }
      }
    
      static Future<String> getToken(userId) async {
        final Firestore _db = Firestore.instance;
    
        var token;
        await _db
            .collection('users')
            .document(userId)
            .collection('tokens')
            .getDocuments()
            .then((snapshot) {
          snapshot.documents.forEach((doc) {
            token = doc.documentID;
          });
        });
    
        return token;
      }
    

    my message sending code

    Future<void> addMessageToDb(
          Message message, User sender, User receiver) async {
        var map = message.toMap();
    
        await firestore
            .collection("messages")
            .document(message.senderId)
            .collection(message.receiverId)
            .add(map);
    
        var token = getToken(message.receiverId);
        print('token setted for fcm');
        callOnFcmApiSendPushNotifications(token, message);
        print('fcm worked');
    
        return await firestore
            .collection("messages")
            .document(message.receiverId)
            .collection(message.senderId)
            .add(map);
      }
    

    my console after message is sent :

    I/flutter (14894): hari
    I/flutter (14894): token setted for fcm
    I/flutter (14894): let's see
    I/flutter (14894): Instance of 'Future<String>'
    I/flutter (14894): hit till response
    I/flutter (14894): fcm worked
    I/flutter (14894): 200
    I/flutter (14894): test ok push CFM
    

    as in my console i can see all the print statement part fcm function but i do not get any notification in my app. Help appreciated