Krishnaa Krishna
Krishnaa Krishna

Reputation: 49

Invalid key 401 error while trying to notification using fcm api

I am trying to send a notification to another user using fcm api and I have a function to get the access token. while using the access token I am getting errors like invalid key. My all json data are correct. What will be the issue how to solve.

Future sendPushNotification(String token, String messageText) async { final serviceAccountJson = { "type": "service_account", "project_id": "chat-app-test-xxxx", "private_key_id": "cb1b048b78df3fcd76202131402329993fca2a7b", "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCov7hgDCOf5aVT\npCWCqqnUPtPfzRiUT2opS38YmLMnVsctKxx78kkr4tXK09lgGSKE8SesDLDE7TTD\nJzPD88TBmBqTg98Cc3Gg+o1kTDZTIb3lmV4G3k5VRobYny7qYpyGhbWOFpTszbx8\nP5bKsesHu+u+39upqwkNz89j3+ZKkirVIf50FFIpJeE7Q4/7hyjJpXHgSfoOEHnT\nl1vAuPxFWARVnr916lWI2RAbj+gDWu2lYmD6to4c0HhHOdJ+PvvzpkQ9CouqMDerPFeE\nhGt2J2YO/hYI7MgskmFhSdIvcxWwtqdi8TaBDXBlxhmiPQzAjLlMolxbDyyGMC0q\n7cav3QyB4eeQ5NaT6c3wHRYs3wKBgQDBSm2GueB+YATS8Rrt9CJ+LCbue5pZA27y\nYdymrh6eWTDA4KJRN1E8j7q94POzJJdkOepYCGqYt3aA6RL1Z6A1ABIOGydtSmcf\n6osDoHrVjM4kaL6eHUYQnPRGG8DnsVheOG6RO681GOnfkqraLwUUTH59M6l53CoV\nxUkfD4Qs/wKBgAQLZky8lk17Axe6NWfpk0eMVbY42rNLvcjztgaugaLSka8vIFWb\nrCK1Wxfe4gJK3YBhR+ba5AAmczt0Ehlh1BgFSoGG85shvdWgqIZ8MYOOT5wtEgZm\nWxgmyBvvY9Ndy3dIlKngXk9/jMy43nhiaTARjgJjti8XFRMELifkPtx9AoGAHUTr\n+O7Es95OXLLt5QKHBpwg+RP2d2wxrLps80CCmm/fFhXt/mZmAsmw+XfK72GnGyI5\n/kKoAaAmbbbdjBO5Cs2L7l7mRYJywz589nuinhvIS3MhCkhch798rfEd8PfF7d6V\nv6BOE7hGEfjfrlJYbo52yPa9vxo7tTg+rllnJnkCgYBM0zKGjFd9/zwjuU5ctKcd\nXaNdbtoOIz6edveUKNiOLs+qSSkQxeNiFbxVkR7bXrcp+UD1+q962ZTIqqx1XPv8\nvzHu0YGhKRNs+E4qLc0UB4vDoUTezcnEGnSc78SU2Yu/0hDPkjzVSnM3eCn9GSPu\ncDMXYOZizGwKH3ld/339Vw==\n-----END PRIVATE KEY-----\n", "client_email": "[email protected]", "client_id": "115351056556643696250", "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://oauth2.googleapis.com/token", "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/chat-app-service%40chat-app-test-a303f.iam.gserviceaccount.com", "universe_domain": "googleapis.com" }; List scopes = [ "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/firebase.database", "https://www.googleapis.com/auth/firebase.messaging", ]; http.Client client = await auth.clientViaServiceAccount( auth.ServiceAccountCredentials.fromJson(serviceAccountJson), scopes);

auth.AccessCredentials credentials =
    await auth.obtainAccessCredentialsViaServiceAccount(
        auth.ServiceAccountCredentials.fromJson(serviceAccountJson),
        scopes,
        client);

client.close();

log("Access Token: ${credentials.accessToken.data}");

final Map<String, dynamic> notificationPayload = {
  'to': token,
  'notification': {
    'title': 'New Message',
    'body': messageText,
    'sound': 'default',
  },
  'data': {
    'click_action': 'FLUTTER_NOTIFICATION_CLICK',
    'message': messageText,
  },
};

inspect(credentials);

try {
  var response = await http.post(
    Uri.parse('https://fcm.googleapis.com/fcm/send'),
    headers: {
      'Content-Type': 'application/json',
      'Authorization': 'key=${credentials.accessToken.data}',
    },
    body: jsonEncode(notificationPayload),
  );

  log('The response ${response.body}');
} catch (e) {
  log('Error sending push notification: $e');
}

}

Upvotes: 0

Views: 103

Answers (0)

Related Questions