pulyaevskiy / firebase-functions-interop

Firebase Functions Interop Library for Dart.
BSD 3-Clause "New" or "Revised" License
191 stars 52 forks source link

Integrating FCM or PubSub Push #51

Open Skquark opened 5 years ago

Skquark commented 5 years ago

I'm facing problems using the Dart fcm_push or the pubsub push in gcloud. I installed the gcloud, googleapis and googleapis_auth dependencies, and set up the ServiceAccountCredentials to get the AuthClient, and everything seems to be done right, however I get this uncaught exception in the log: ReferenceError: XMLHttpRequest is not defined at /user_code/build/node/main.dart.js:9351:7 I just can't get past that, no matter what I tried. Is there a trick I'm missing, or a better way to push notifications to subscribers or user token ids using this interop? Here is my pubsub.yaml:

dependencies:
  firebase_functions_interop: ^1.0.0
  fcm_push: ^1.3.1
  gcloud: ^0.6.0+4
  googleapis: ^0.53.0
  cron: ^0.2.2

dev_dependencies:
  build_runner: ^1.0.0
  build_node_compilers: ^0.2.0
  googleapis_auth: '>=0.2.3 <0.3.0'

and my relevant imports:

import 'package:fcm_push/fcm_push.dart' as fcm;
import 'package:googleapis_auth/auth_io.dart' as auth;
import 'package:http/http.dart' as http;
import 'package:gcloud/pubsub.dart' as pubsub; 

and my code to init the client:

auth.AuthClient client;
pubsub.PubSub pubSub;
fcm.FCM cloudMessaging;

  var credentials = auth.ServiceAccountCredentials.fromJson(r'''
{
  "type": "service_account",
  "project_id": "....",
  "private_key_id": "....",
//Taken from key credentials json download
}
''');
  var scopes = pubsub.PubSub.SCOPES;
  print("Init 1 - auth client Metadata");
  client = await auth.clientViaServiceAccount(credentials, scopes);
  //client = await auth.clientViaMetadataServer();
  print("Init 2 - Getting pubSub");
  pubSub = new pubsub.PubSub(client, "functions");

Everything builds/deploys correctly, except in the log with the client auth, or try to do a cloudMessaging.send() I get that pesky XMLHttpRequest is not defined error that I can't find anything about fixing. Has anyone else been able to send push notifications from their Firebase Functions trigger? Seems like it would be a common use case, any help would be appreciated, thanks..

Skquark commented 5 years ago

I'm still trying to figure this out, I really don't want to redo all my code in the JavaScript. There's gotta be a way to send Firebase Cloud Messaging from here, that's my whole purpose of needing to run a Firebase Functions server to send notifications. Any ideas?

ThinkDigitalSoftware commented 5 years ago

It does seem like messaging is missing from the admin class here

pulyaevskiy commented 5 years ago

I'm not sure I understand the problem here. Do you have a cloud function which should send a PubSub message?

If you're using googleapis packages I'll likely need to implement Node.js specific googleapis_auth client, because it looks like only supports dart:io and web browser HTTP clients.

Of course, it would be nice to add FCM support to firebase_admin_interop so that we can use Firebase SDKs directly.

ThinkDigitalSoftware commented 5 years ago

Yeah, that's what I'm waiting for myself

Skquark commented 5 years ago

I had hoped the native dart plugins would have been compatible, but I'm realizing the interop trick having to translate to node.js calls.. It would be nice if it was a builtin function for sure. I don't know if pubsub is easier to impliment or Cloud Messaging, I could go with either for pushing notification to a channel group or individual Firebase UIDs. Whatever's easier to hack together, it's that missing piece to get my app functional. If I can help, lemme know, but this part of code gets daunting... Thanks.

kaciula commented 2 years ago

Did anyone here manage to send a push message from dart in a Firebase function? I really hope I do not have to redo all my code using Javascript.