I have the following server code in python stored in firebase cloud functions
import firebase_admin
import google.cloud.firestore as t
from firebase_admin import credentials, firestore
from firebase_admin import messaging
from firebase_functions import firestore_fn
cred = credentials.Certificate(my credentials stuff to give it permission to access database as admin)
firebase_admin.initialize_app(cred)
db = firestore.client()
@firestore_fn.on_document_created(document=r"Notifications/{id}")
def send_notification(event: firestore_fn.Event[firestore_fn.DocumentSnapshot | None]) -> None:
sent_to_device = "my-device-unique-id"
# notification = messaging.Notification(
# title="You have a new follower!",
# body="Someone is now following you.",
# )
doc_ref = db.collection("Users")
doc_ref.add({"first": "Ada", "last": "Lovelace", "born": 1815})
msgs = [
messaging.Message(token=sent_to_device, data={'data': 'hello'})
]
messaging.send_each(msgs)
return
so what it does is to listen to a collection in my database in firebase cloud. If a user follows someone else, a document is created in notifications. it will store the device number from gettoken(). When a notification document is created, when a user follows other user, it calls the server function. However, I do not get the notification in my android device.
I tested the server function by adding a temporary document to other collection in my database, by using the db variable, and it does work. The function does responds to when a user follows another user.
It does work from the firebase console. I can send a fcm to my device and it does get send.
here is my android manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<application
android:label="appname"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<activity
android:name="com.ryanheise.audioservice.AudioServiceActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize"
android:exported="true"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service
android:name="com.ryanheise.audioservice.AudioService"
android:foregroundServiceType="mediaPlayback"
android:exported="true">
<intent-filter>
<action android:name="android.media.browse.MediaBrowserService" />
</intent-filter>
</service>
<service
android:name="com.google.firebase.messaging.FirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<receiver
android:name="com.ryanheise.audioservice.MediaButtonReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
</manifest>
I am trying to send a notification by running my application from the web, not from another device. Could that be the problem?
I also have read that I need to ask user for notification permission, which I already did in Flutter
Timer.run(() {
showModalBottomSheet(
context: context,
isDismissible: true,
builder: (context) {
return AlertDialog(
title: Text('Notifications'),
actions: [
FutureBuilder(
future: Permission.notification.request(),
builder:
(context, AsyncSnapshot<PermissionStatus> snapshot) {
return Row(
children: [
ElevatedButton(
onPressed: () => snapshot.data!.isGranted,
child: Text('Confirm'))
],
);
})
I have near 0 experience with python so maybe my syntax is wrong?