I'm using Firebase Cloud Messaging in my Dart/Flutter app. The code below updates a user doc with a retrieved android notification token.
FirebaseMessaging().getToken().then((token) {
_usersRef.doc(user.id).update({
"notificationAndroidToken": token,
});
});
I then read the notificationAndroidToken in my cloud functions. Works great except I had wrongly assumed the token would be invalid if the user had shut off their notifications (e.g., empty or null). What's the best way for determining if notifications are enabled for my app in cloud functions? I'm finding some hits in my searches but many are outdate or for a different dev environment.
The firebase_messaging package does not provide any method for checking if notifications are enabled by calling NotificationManagerCompat.areNotificationsEnabled on the platform. The awesome_notifications package provides such a isNotificationAllowed method.
Related
I'm making a mobile app using flutter and firebase.
To use Cloud Messaging of firebase, I need to send server key of firebase by putting in headers and sending with http post method.
headers: {
'Authorization': server key
}
However, it is not a recommended way to use server key. Firebase recommends to download json file of service account and use that file to get JWT token from firebase. Otherwise, I have to use Admin SDK and it's difficult for us because we don't have a backend server.
Therefore, my question is that are there any good ways to store json files in client side or local environment or firebase or Azure and can read the file only when we need it.
Plus, if you know the better way to send FCM from client side, please let me know. Thank you so much for reading it.
I have to use Admin SDK and it's difficult for us because we don't
have a backend server.
You can very well use the Admin SDK with Cloud Functions for Firebase, the "serverless framework that lets you run backend code". It is very common to use Cloud Functions to send message requests to the FCM backend, which then routes messages to client apps running on users' devices.
There is actually an official example that demonstrates how to send an FCM notification from a Realtime Database triggered Function. You can adapt it to be triggered by any other available trigger mechanism.
I set up App Check for iOS and Android on Flutter. iOS devices are working fine and able to access my RT Database and Cloud Functions. However, Android devices are blocked. I used the SHA-256 certificate fingerprint generated on Google Play Console. Also, Firebase says my Android app is registered. So, everything looks ok, but it's not.
I don't know what to check at this point because there is no error on my side. The only thing I can see is unverified: invalid requests from App Check request metrics.
This is my Flutter code to activate App Check:
void main() async {
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
await FirebaseAppCheck.instance.activate();
runApp(MyApp());
}
This is a log info from Cloud Functions:
Callable request verification passed {"verifications"{"auth":"MISSING","app":"MISSING"}}
I have solved my problem and I have recently received my first "App Check Verified Request" successfully at my Firebase Cloud Firestore App Check request metrics.
I only added "Play Integrity" until now, because the Firebase documentation (https://firebase.google.com/docs/app-check/android/safetynet-provider) uses the following statement: "Note: The SafetyNet Attestation API is deprecated and has been replaced by the Play Integrity API. Consider enabling App Check with Play Integrity instead. See the deprecation timeline for more information."
To solve the problem, it will be enough to apply the following:
Firebase Console -> Project Settings -> App Check -> Apps -> Add SafetyNet as additional Attestation providers.
I have now Play Integrity and SafetyNet both together. Now App Check Requests are working also for "Real device", "Release build" and "Closed Testing" scenario.
I suggest using "Consider enabling App Check with both Play Integrity and SafetyNet." instead of "Consider enabling App Check with Play Integrity instead." on Firebase Documentation.
Thanks for adding such a great service that improves security in any case.
So just leaving a comment in case this helps.
I had the same issue but made this work after using the SHA-256 under "App Signing key certificate" (It wasn't working until I used Upload key certificate). Hope this helps in any way.
I have a flutter social network app with firebase
I want to send notification to user when they get follow,like,comment or any activity
I know firebase cloud massaging do this but how
I want the exact code for this or some thing near with it
You can archive in 2 ways but in both ways you required user's fcm_token or device_token and firebase Server key.
Use this API - https://fcm.googleapis.com/fcm/send to send push notifications to specific users.
If you have own server
Manage via own server (secure & most preferred)
Crate your own API for send notifications to specific user. From your backend get that user's fcm-token & fire actual push API from your server.(firebase server key not save on mobile platform)
Do not include the server key anywhere in your client code. Also, make sure to use only server keys to authorize your app server.
If you don't have own server.
You need to add firebase sever_key in front-end means app code. (Less preferable)
Just call actual firebase push API from your app code.
Note: in both ways you required receiver fcm_token or device_token. Because you need to pass firebase server_key & this token in that API.
Reference links
1.official latest documentation for API link1
Another reference link2
API official doc - link3
I am new to Smart Home world and I am trying my hand to implement smart home solution with Google App Engine and Firestore for my cloud service. I have successfully integrated my devices and Smart home solution and able to update the device state using Google assistant and Google Home. After much read through got HomeGraph ReportState also implemented via REST, which is executed during Execute and when the device state changes outside Google home (User changes the fan speed of the Fan).
Now the issue is, when the Execute API is executed, the Firestore database is updated by the API and homegraph report state is invoked and hence data is in sync between them (Both Query responses match). However when the home graph report state is called outside Smarthome system, to update user made changes, the data is out of sync. Home Graph has the latest updates, but Firestore has the last updated state done by Google Assistant.
How to sync HomeGraph and Smart home cloud services, with HomeGraph has the accurate data.
There are two ways you can ensure the data is synchronized between both Home Graph and your service. One method is to modify the non-Google Home state changes so that they still go through your service. This can be a new intermediate endpoint that you call which will update your database before updating Home Graph, ensuring that you have the state changes.
Alternatively, you can use the Home Graph Query API call, which will return the states of devices from your service according to Home Graph. You will need to call this manually, as there is no way to subscribe to changes.
Im new to Flutter.
I wondered why I need to use Cloud Functions to send notifications to the other device in Flutter.
If one device simply know the token of the partner device, I think that it can specify the token and send a notification directly from the client side.
Does this question relate to this answer?
How to send push Notification using FCM and Flutter(One to Another Device)?
Thank you.
You certainly could send the message directly from your client app, but you would then have a huge security problem. The Admin SDK requires a service account to initialize, and you would have to package that service account in your app so it could call the FCM API.
Distributing your service account is strongly discouraged, as it now allows everyone to do everything to your project that the service account is allowed to do. This could be anything and everything.
Instead, people put messaging code on secure backends where the service account can't be seen by others. Cloud Functions is a popular option for this, but you can use whatever backend you want.