Reputation: 962
This is the index.js code
const admin = require('firebase-admin');
const functions = require('firebase-functions');
admin.initializeApp();
exports.notifFunc = functions.database.ref("/collegeData/{collegeId}/notices").onCreate((snapshot, context) => {
console.log(snapshot.val());
return admin.messaging().sendToTopic('notices', {notification: {title: "Title", body: "Does this
work?", clickAction: "FLUTTER_NOTIFICATION_CLICK"}});
});
It deploys without any error with the following output.
=== Deploying to '*************'...
i deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint
functions@ lint C:\Users\kenre\Documents\GitHub\Android Project\************\functions
eslint .
+ functions: Finished running predeploy script.
i functions: ensuring required API cloudfunctions.googleapis.com is enabled...
+ functions: required API cloudfunctions.googleapis.com is enabled
i functions: preparing functions directory for uploading...
i functions: packaged functions (37.51 KB) for uploading
+ functions: functions folder uploaded successfully
i functions: updating Node.js 8 (Deprecated) function notifFunc(us-central1)...
+ functions[notifFunc(us-central1)]: Successful update operation.
! functions: Warning: Node.js 8 functions are deprecated and will stop running on 2021-03-15. Please upgrade to Node.js 10 or greater by adding an entry like this to your package.json:
{
"engines": {
"node": "12"
}
}
The Firebase CLI will stop deploying Node.js 8 functions in new versions beginning 2020-12-15, and deploys from all CLI versions will halt on 2021-02-15. For additional information, see: https://firebase.google.com/support/faq#functions-runtime
+ Deploy complete!
Project Console: https://console.firebase.google.com/project/*************/overview
My app successfully creates/posts a new key-value pair entry in the specified path ("collegeData/{collegeId}/notices") using the Rest API for Firebase Realtime Database. But the log never appears (waited 4 hours before writing this) and the notification is not sent either. Is there any reason for this? I realise it asks me to upgrade to node version 12 but I am refraining to do so to avoid using a credit card.
Upvotes: 0
Views: 135
Reputation: 962
After trying various random things, I seem to have stumbled upon the solution. Although the firebase docs say it listens for writes in all entries under the specified path it actually needs the path to be specified as collegeData/{collegeId}/notices/{noticeId} to listen for new notices that are posted with new ID keys.
Upvotes: 1
Reputation: 1771
If the function is triggered successfully there will be a log in the console Function Execution Started
. So if you never get the log, it means the function isn't being triggered.
In your case I would maybe try a simple function to test if a function is even callable without a credit card linked, although I think if you called it and it wanted a payment method that would should up in the log. This probably isn't the issue, but it could be.
Function logs appear about 20-60 seconds after a function executes. Your function looks fine to me and should be called if the ref is correct. Upload a picture of your database so we can see there are no mistakes, and test a simple function to ensure any function works first before a onCreate, try a http callable function from flutter:
exports.areMyFunctionConfigured = functions.https.onCall(async(data, context) => {
console.log('they work');
});
in Flutter:
import 'package:cloud_functions/cloud_functions.dart';
final HttpsCallable areMyFunctionConfigured =
CloudFunctions.instance.getHttpsCallable(functionName: 'areMyFunctionConfigured');
_checkIfFunctionWork() async {
dynamic resp = await areMyFunctionConfigured.call();
}
Upvotes: 0