I'm using firebase to send notifications to my app(iOS and Android).
I'm not keen on using the data fields to send my notifications because for iOS didReceiveRemoteNotification doesn't get called when user has force quit the app.
I've tried setting "sound" to all sorts of things including default and null but I'm not getting any sounds/vibrations on either devices.
Thanks.
There was just a minor issue. I was placing the sound in root of json payload. While, it had to be inside the notification object. Firebase should really have a number of example payloads in their documentation.
You can refer firebase document, table 2a
Should put sound filed into notification payload, then it should work
Related
I want to build a notification system in my Android app where I can send a notification to someone upon an event. For example, my app is about a social system where it uses Firestore database (Firebase) to save its users (which means I have a 'users' collection in there), and I have an 'Add as Friend' feature.
Everything works just fine, but I would like to add a feature where when user1 adds user2 as a friend, I would like to send user2 a notification about that.
I have searched in google and saw this post: https://www.geeksforgeeks.org/how-to-push-notification-in-android-using-firebase-cloud-messaging/
Now, from what I understand, this wouldn't help me much cause it only sends the notification from FCM by me (and not to a specific user).
The problem is, I don't really know what to search for (or what to type in google) in order to find what I'm looking for.
Some ideas I thought:
to make a collection in firestore called 'Notifications' where it holds usernames as documentIDs
when user1 sends a friend request to user2, I would add data (like title/message) to 'Notifications/user2/'
But I'm kind of stuck here, I would like to know ideas or if you guys have threads on how do I implement this, I really don't know what should I look for.
Thank you very much and sorry for the long post.
Firebase creates a userID for every user and a usertoken which is a temporary identifier for the device which is connected to this userID. You need to save the usertoken when there is a new one being created in a database(firestore) along with the user with whom you identify this person.
When someone adds this person as a friend you can add a notification in the database that there is a request from person1 to person2.
This change in the database can trigger a cloud function that reacts to changes in the database.
The triggered cloud function sends an FCM to the person which is being added as a friend. This FCM can either be a pure notification or you send data and let your app handle how this data is being handled(saved in a local database or displayed as a custom notification).
Keywords are: FCM, Notification, Firestore, cloud functions, and firebase token
Edit (Answer to 1. comment): Firebase userID and usertoken are created on the device. In the tutorial you implement FCM(I didn't read it complete) and a class called FirebaseMessagingService, from which you can inherit. This class is called, when a message is sent to the device and contains a method called onNewToken. This message is called, when the Firebase Server creates a token for your device and sends it to the device. In this method you might want to send the new token to your database and save it as an identifier for the user of this device. The Tokens are used by firebase to identify devices, to which messages are being sent(You can read about FCM in detail)
Cloud functions are something you can deploy along your firebase project, which get triggered when an event occurs. For example a http call, or a specific change in your database. Cloud functions are written in JS and need to be deployed for your firebase project. (Cloud functions are not for free and you need to pay for each call, but you have some thousand uses for free each month.)
You send a FCM to a specific person by creating a firebase message which is directed to a specific token, tokens or user groups. To identify the person to which you want to send a message you need to have the token for this user, that is why you are saving the tokens from the 1. part in your database and always update the token, when the token changes on the device.
Keep in mind, that a user might use multiple devices. You should have an identifier for you, such as username and along this username you save multiple firebaseIds and Firebase tokens. A single FCM can be directed to up to 100 tokens.
My case is the following: a user do something, then my Firebase script in Functions should notify his friends, but it don't know what lang set up for the app of certain friend.
If the Android app in foreground, there is no problem, I simply should send some json in notification payload and then parse it in onMessageReceived using current lang values. But I can't do the same, when the app in background or closed. So it sounds like I have to build multi-lang notifications at the backend, am I right? Any suggestions appreciated.
Searching for hours, I had found pretty good answer. You don't have to store lang constants in backend scripts, because you can manage notification's data even if your app in background or even closed. You just should clear notification field of notification payload. See full answer.
I'm using Firebase Cloud Messaging to send data payload and display notification in my Android and iOS apps. I'm integrating this in my iOS app and I'm in front of a curious thing.
Short story:
My notification server only send data messages (I mean JSON messages containing "data" field and with no "notification" field). In my Android app this is not a problem because I receive the message even if my app is in background in my Service implementation and I manually build a notification that I display. But in iOS case, how can I receive message when my app is in background to do the same? I mean if I add a "notification" field to my request I see the notification pop on my screen (when app is in background) and I can get data (located in the "data" field) after a click when my app is in foreground.
But can I have data when my app is in the background to manually display a notification?
Important precision:
The content of the displayed notification should be built app side and not server side because the server doesn't have enough informations, this is why I can't send the "notification" field in my payload message.
Thanks
EDIT: I solve (in part) this problem using this link. Shortly, I enabled both Background fetch and Remote notifications under Background Modes (and added content_available : true in the payload). Then I can process data received in didReceiveRemoteNotification: But now it still one thing: how make it works when my app is killed?
EDIT 2: I found something that could be the thing : that. Modifying the payload of a remote notification implies to handle incoming notifications before there are displayed. I'll try.
EDIT 3: Firebase doesn't support the mutable-content key required to make my solution found at EDIT 2 working. Discussion about that is here. So... Any idea?
I do not think it is possible to trigger "stock" notification on iOS from code the same way you do on Android using NotificationBuilder.
The only option available is to use hybrid messages with both notification and data payloads.
Check their doc for more information on hybrid messages.
On 16/02/14, Firebase added compliance with the "mutable-content" key. So that solved my problem and, I think, many people's problems.
Personal thinking:
I consider that 6 months late for this feature is really too much. I hope that future releases'll be up to date with iOS (as well as Android) releases.
I'm currently developing an app that uses the new firebase cloud messaging system. Now I see the topic feature of FCM.
I created a topic "news" and want to add sub topics e.g "news/weather" or "news/politics".
Altogether there are about 20 subtopics.
Is this even possible with FCM and is this god practice?
I usually start my topics with a prefix and then the subtopic, separating them with a _.
So if I have a sub-topic per user (great for sending user-to-user messages) I end up with:
/topics/users_puf
/topics/users_forj9
There might be better ways, so I hope others will answer too.
From my understanding of the GCM documentation you basically send whatever data you want as the data field. Then the client receives it and is responsible for handling it and displaying it. Is this wrong? What if like on iOS I just wanted to send a bit of text and have it display? Is there a standard key to use or anything? I'm pretty certain I've been through all the documentation I can find and don't see anything like this.
No, you're right. There are no standard keys like in iOS, it's all up to you seeing as you're the one responsible for the notification management.
Basically, in your BroadcastReceiver you should write the code for displaying a Notification in the status bar. Just parse your JSONObject from the recieved message and show whichever piece of text you may have set.