I don't understand very well how to send a notification to a user that receives an action. For example, when User A adds a comment or a like to User B, I want User B to receive a notification.
If User A adds a comment or a like, I can send a notification to each follower that is subscribed in the topic of User A in this mode. Topic subscription by followers:
FCMPlugin.subscribeToTopic('topicExample');
And I send a notification by the REST API by User A:
{
"notification":{
"title":"Notification title", //Any value
"body":"Notification body", //Any value
"sound":"default", //If you want notification sound
"click_action":"FCM_PLUGIN_ACTIVITY", //Must be present for Android
"icon":"fcm_push_icon" //White icon Android resource
},
"data":{
"param1":"value1", //Any data to be retrieved in the notification callback
"param2":"value2"
},
"to":"/topics/topicExample", //Topic or single device
"priority":"high", //If not set, notification won't be delivered on completely closed iOS app
"restricted_package_name":"" //Optional. Set for application filtering
}
But I don't understand how to send a notification to User B, because he is not subscribed to User A and is it possible to send the notification only to users subscribed to a certain topic?
Related
I'm using Capacitor for my App, I have an use case similar to whatsapp, when users receive a new message if your app is in background, you receive FCM notification in the notification tray, later if user open that particular chat directly by opening the app(without clicking on the notification), i'd like to clear that notification from the notification tray.
so here's what I thought
get active notification
find the active chat notifications by filtering extra data sent by FCM
clear those notifications programatically
however android's notificationManager.getActiveNotifications doesn't seem to provide access to FCM data.
below is the payload i'm posting to FCM(i want to use chatId and type in data section to clear active notifications).
{
"android": {
"priority": "high"
},
"notification": {
"title": "you have a new message from XYZ",
"body": "hello"
},
"data": {
"messageId": "62971793-0111-4c9b-bbb2-bb78262a0543",
"chatId": "631e162f5f286c23d13e06d5",
"type": "CHAT_NEW_MESSAGE"
},
"token": "fcmToken"
}
Here's the capacitor plugins Android Logic to get the delivered/active notification.
how can I achieve above use case?
I'm sending notifications to certain users via the postman. The notification successfully reaches the device. However, it won't show in lock screens as well as a pop up from the top of the screen when the user is actively using the device (like WhatsApp notifications). How to send the notification with more priority to Firebase using the REST API?
{
"notification":{
"image":"",
"sound":"default",
"title":"Order Packing",
"body":"We are packing your order"
},
"data":{
"linkType":"order"
},
"default_sound":true,
"to":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"tag":"orderStatus",
"priority":"high",
"notification_priority":"PRIORITY_MAX"
}
I receive notifications when the app is closed and when it's open, but when it's closed and I click on the push notification that has a link it only opens the app and doesn't open the browser, and if the app is open and I click on the push notification the browser opens.
How can I open the browser when I click on a push notification that has a URL and when the app is closed.
An FCM with notification payload does not go through the onMessageReceived when your app is not running. It's posted by Google Play Services on your behalf directly to notification shade. Once clicked your main activity is launched and intent extras contain the data payload.
You have two options:
Continue using notification payload and handle the data in your main activity onCreate.
That's the activity started by launcher. Use this for information push notifications. If your app is not running it won't be started, which saves battery.
Don't use notification payload.
Use this for business logic push messages. This will always start your app process and let you handle the message in onMessageReceived.
Read more here:
https://firebase.google.com/docs/cloud-messaging/android/receive
https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
Use the below payload if you want to open the WEB URL while clicking on the FCM notification when the app is Killed or in the Background.
{
"to": "device-token",
"priority":"high",
"data":{
"body": "there",
"title": "Hi",
"LinkUrl": "https://www.google.com"
}
}
Note: I have not used "notification" in the above payload, if you use the below payload while using the notification keyword in your payload it will open the app not the URL,
{
"to": "device-token",
"priority":"high",
"notification":{
},
"data":{
"body": "there",
"title": "Hi",
"LinkUrl": "https://www.google.com"
}
}
Cheers!
I know that there is the same question with this title but unfortunately it is not answered right and it is accepted!!! here
I want to know how I can find out a FCM message received when app is in background to do some action on message received before clicking by user. but when app is in background onMessageReceived is not triggered!
I googled so much and could not find a good way.
To handle FCM push notification from onMessageReceived() when the app in background the server should always send Data only messages.
Notification messages can only handle when the app is in the foreground. When the app is in the background an automatically generated notification is displayed. When the user taps on the notification they are returned to the app. Messages containing both notification and data payloads are treated as notification messages.
With FCM, you can send two types of messages to clients:
Notification messages, sometimes thought of as "display messages."
Data messages, which are handled by the client app.
A notification message is the more lightweight option, with a 2KB limit and a predefined set of user-visible keys. Data messages let developers send up to 4KB of custom key-value pairs. Notification messages can contain an optional data payload, which is delivered when users tap on the notification.
Notification messages
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
}
}
Notification messages are delivered to the notification tray when the app is in the background. For apps in the foreground, messages are handled by these callbacks:
onMessageReceived() on Android. The notification key in the data bundle contains the notification.
Data messages
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
},
}
On Android, a client app receives a data message in onMessageReceived() and can handle the key-value pairs accordingly.
Note these further platform-specific details:
On Android, the data payload can be retrieved in the Intent used to launch your activity.
Messages with both notification and data payloads
{
"to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
},
"data" : {
"Nick" : "Mario",
"Room" : "PortugalVSDenmark"
}
}
App behavior when receiving messages that include both notification and data payloads depends on whether the app is in the background or the foreground—essentially, whether or not it is active at the time of receipt.
When in the background, apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification.
When in the foreground, your app receives a message object with both payloads available.
Reference
About FCM messages
Handling received messages
As I've mentioned in the comments section, when sending a message with a notification message payload the Android System (Notification Tray) will handle the push notification when your app is in background.
You should use a data-only message payload, so that it will always be handled by onMessageReceived().
See the Handling Messages docs in Android for more details.
Handle notification messages in a backgrounded app
When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default.
This includes messages that contain both notification and data payload (and all messages sent from the Notifications console). In these cases, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.
When in the background, apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification.
Is there any way to handle the data payload without user tapping on the notification?
Basing from the FCM docs on Handling Messages:
App state Notification Data Both
Foreground onMessageReceived onMessageReceived onMessageReceived
Background System tray onMessageReceived Notification: system tray
Data: in extras of the intent.
It's not specifically handled ONLY by tapping on the Notification. You may handle it in the onMessageReceived(). Pretty sure the tap action also depends on how you implement it.
If you intend for it to do something else, do provide more details.
If you want to receive data payload when app is not running then you need to use a custom app-server with http protocol(in your case) which will send only data payload no notification and will send data to fcm endpoint like this
{ "data": { "some_key" : "some_value",
"some_more_key" : "some_more_value" },
"registration_ids": ["device-token","device-token2"] }
or just to send data from server to one client
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
"Nick" : "Test",
"body" : "Your message here",
"Room" : "Some Test Room"
},
}
Then using POST you can send downstream message to your device and it will work the way you want. If you want to have a basic idea of such app server please consider this : How to push notification to client when Firebase has a new entry?
I have posted basic Java Servlet code here and also a node.js code by firebase official blog.
Hope it helps.