Push notifications on content change
The best way for achieving this is to use Firebase Cloud Functions. This will help you notify users when something interesting happens, in your case, when new content is available. You can use either Cloud Firestore or Firebase Realtime Database to achieve this. I will explain to you in my answer how can be done using the new Cloud Firestore
. For that I recommend you implement also Firebase Authentication. This will help you send notifications to a particular user or to a group of users when something new happens.
In order to achieve this, please consider following the steps below.
Implement Firebase Authentication. As soon as it is implemented, create a collection of users in which each user will be a document within users collection. Your database structure should look like this:
Firebase-root
|
--- users (collection)
|
--- uid1 (document)
| |
| --- //user properties
|
--- uid2 (document)
|
--- //user properties
Besides user details, you need to add to each user a tokenId
. You get can it very simply using the following line of code:
String tokenId = FirebaseInstanceId.getInstance().getToken();
A user document should look like this:
uid1
|
--- userName: "John"
|
--- userEmail: john@email.com
|
--- tokenId: "e_wLukMfq..." //very long token
|
--- //other details
Now, add a new collection to the user document named
notifications
, in which you need to add thenotification
you need to send and thesender
, every time something new happens. It should look something like this:uid1
|
--- userName: "John"
|
--- userEmail: john@email.com
|
--- tokenId: "e_wLukMfq..." //very long token
|
--- notifications (collection)
| |
| --- notificationId1
| |
| --- notificationMessage: "My Notification"
| |
| --- fromUser: "My Notification"
|
--- //other detailsNow you need to use Node.js to write a function in
Cloud Functions
that will listen for every new notification that appears within this reference:"users/{uid}/notifications/{notificationId}"
Once a new notification appears, you can use sendToDevice
function and the tokenId
to send the notification to a specific user. The notification will be handled by the Android system and will be displayed to the user. Note, this will work only when the app is in background
. You can receive notifications also when the app is in the foreground
by implementing FirebaseMessagingService
.
Changing the contents of a pushed notification
There is no way to change the content of push notifications because the system presents it to the user before informing the application.
If you want to have more control over what your notification should look like, you should use Rich Push Notification (RPN). With RPN, notification presentation is up to you (app side). You can custom text messages, adding images/audios/videos/gifs, and more. But it requires a little extra work.
You may also try to customize the appearance of the notification by using the Notification Content Extension
but it does not allow you to customize the initial banner (you need to tap the initial banner to see the full notification interface)
https://developer.apple.com/documentation/usernotificationsui/customizing_the_appearance_of_notifications
Change notification content when received
For Android,
yes, it is possible by overriding onMessageReceived
of FirebaseMessagingService
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check your notification is not null and subcribed to `food` topic.
if(remoteMessage.getFrom() == "food" &&
remoteMessage.getNotification() != null){
String title = remoteMessage.getNotification().getTitle(); // Title from the notification. E.g `Good morning`
// Get the `username` of the user stored in the device. Use SharedPreference.
String message = title + " " + username; // "Good morning username"
// Call your notification here.
}
}
Changing notification text using FirebaseNotificationPushManger
Set the meta name correctly so the Default Notification Channel can be recognized like:
<meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="@string/notification_channel_id" />
Related Topics
How to Dynamically Update a Listview on Android
How to Find Serial Number of Android Device
Android - Android.View.Inflateexception: Binary Xml File Line #8: Error Inflating Class Fragment
Android Deprecated Apache Module (Httpclient, Httpresponse, etc.)
What Does Layoutinflater in Android Do
Simplecursortreeadapter and Cursorloader for Expandablelistview
Custom Notification Layouts and Text Colors
How to Make the Corners of a Button Round
Intercepting Links from the Browser to Open My Android App
Programmatically Switching Off Android Phone
API Key for Gcm Is Suddenly Invalid? Unauthorized (401) Error
Android Update Activity UI from Service