I am sending notification and handle notification FirebaseMessagingService.
But if app on background FirebaseMessagingService cannot handle notification and not show image.
How can I do this? (If the application is in the foreground, the picture is shown in the notification.)
And if I use onesignal image showing all times.
{
"to" :"4sQrChOF16uMYYEKeiRz6dzCnN3m9OCE9jwOyPBOD92IlzljWvQ_1quiYyluP",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
},
"data": {
"body" : "great match!",
"title" : "Portugal vs. Denmark2222",
"img_url" : "https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcRYJ_0NZFg0htNXwmKkyTv76bw05EacXaXNnqd4ZrPB7wVTXNxR"
}
}
Here are 2 ways you can show an image in your notification consistently:
[Recommended approach] - Using the newer HTTP V1 API of FCM:
Here you can give an image URL as part of the notification payload and firebase will handle downloading and displaying the image for you. Here's the documentation link for the same.
Using the older legacy API of FCM:
Send a data-only payload(Don't send the notification object at all). Include image URL as part of the data payload. This will give you control in the onMessageReceived method(in both foreground and background cases). Here you can generate and display the notification however you like it. Retrieve the image URL here and download the image and create a notification with the image or however you want it to be rendered.
Related
I am using PushNotifications Capacitor plugin and FCM to send the notifications, everything works fine until I want to send a Push Notification with both notification and data fields on Android.
What I want to do is to send a custom data with the notification and consume the notification on app resume. I am using PushNotifications.getDeliveredNotifications() method to get the notifications on resume, on iOS I get all the notifications with proper data, but on Android data property is replaced with the object below:
{
body: "My Body",
data: {
android.appInfo: "ApplicationInfo{809371c app}",
android.bigText: "My Body",
android.progress: 0,
android.progressIndeterminate: false,
android.progressMax: 0,
android.reduced.images: true,
android.showChronometer: false,
android.showWhen: true,
android.template: "android.app.Notification$BigTextStyle",
android.text: "My Body",
android.title: "My Title",
gameDndOn: false,
specialType: "",
topFullscreen: false,
groupSummary: false,
id: 0,
title: "My Title"
}
}
I see in the FCM docs for Android apps that if both notification and data fields are present and the app is in the background the notification lands in the system tray and data in extras of the intent.
I am getting proper data when I tap on the notification, but in my case I want to get that on resume, without tapping on the notification.
Is there any way to get to that data in that case?
You have to create the message in this format
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Portugal vs. Denmark",
"body":"great match!"
},
"data" : {
"Nick" : "Mario",
"Room" : "PortugalVSDenmark"
}
}
}
Note : notification structure is critical for receiving a notification. You need to have title and body. This gets displayed in the notification drawer.
data can be customized as per your requirement. This will have the data that you need inside your app.
Suggestion, keep your payload small. Send data which is absolutely necessary and also safe to send over FCM.
Look at this part of the official documentation
For handling push notifications in capacitor, you should use the listeners. Here is the official docs way of doing it.
My application is running in the background and I send a push notification through postman using the payload below.
Now when I send this, I get the notification and when I tap it, it launches my application's Main Activity. But I want to launch the web browser which opens the link that I send in the notification body. How to do this?? Can somebody please help.
{
"to" : "MY_TOKEN",
"collapse_key" : "type_a",
"notification" : {
"body" : "https://developer.android.com/training/notify-user/build-notification#click",
"title": "Title of Your Notification in Title",
"key_1" : "Value for key_1",
"key_2" : "Value for key_2",
"click_action" : "OPEN_WEB"
}
}
I have been trying to send the Push notification using FCM. It works completely when the app is in Foreground or background. But if i kill the app from recent list then i'm not getting any notification.
This is my structure of FCM with data payload
{"registration_ids":["here is my FCM token"],
"data":{
"body" :"Hello All, Hope you had a great time!",
"title":"Title",
"click_action":"ACTIVITY_MESSAGE",
"sound":"default",
"type" : "message",
"message": "message_here",
"time" : "2016-12-14 03:37:pm"
}
}
This is the structure with data and notification
{"registration_ids":["Here is my FCM token"],
"notification":{
"body" : "Hello All, Hope you had a great time!",
"title":"title here",
"click_action":"ACTIVITY_HOME",
"sound":"default"
},
"data":{
"type" : "message",
"message": "message_here",
"time" : "2016-12-14 03:37:pm"
}
}
I have also tried the same with notification tag and data as well but nothing is working in case whent the app is killed. If any one have any idea how can i get the notification in case of my app is killed then do share.
You should try to put "forceStart": "1" inside the data brackets.
In this way you should force the application to start the notification.
I am using firebase to send notifications to my app. I want to save the notifications received on each device in a local database. The problem is, when the app is in the background and a notification is received, the method onMessageReceived() won't be called, but instead the notification would be added to the system tray as per documentation. The documentation also mentions the following for receiving a notification when the app is in background:
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.
So, when I'm trying to access the extras in the intent, the keys of the notification are not found.
My code in MainActivity.java:
if (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()) {
Object value = getIntent().getExtras().get(key);
Toast.makeText(this, "Key: " + key + " Value: " + value, Toast.LENGTH_LONG).show();
}
}
The notification we are sending from the server:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
}
}
Anyone has an idea of how I can retrieve the values inside the notification?
This is the intended behavior (as you've seen in the documentation). If you're using a notification-only message payload (which you are), onMessageReceived() will only be called if your app is in foreground, if not, the System Tray will be receiving it.
The data being referred to in (emphasis mine):
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.
is the data message payload (See Message Types).
You'll have to send a payload with both notification and data. Like so:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
},
"data": {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
}
}
Also see my answer here.
All you have to do is update your notification push format.
From:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
}
}
To
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
}
}
Explanation:
There is a firebase service running by your system which works for all app and handle all push notification if it contains the "notification" tag. but if contain the "data" tag only it just handover the task to the service created by the app where you can do anything.In your case show user notification and save it to the local db. For more you read here Documentation : Handle Messages.
It's not a simple or clean solution, but you could use a NotificationListenerService to receive a callback for each notification. A further limitation is that it supports only API 18 and above.
Add this to manifest file
<intent-filter>
<action android:name=".MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
I am sending a push notification using firebase endpoint. It is working successfully. I am using postman to send the request to FCM. My issue is I do not understand how to send a large icon with it.
FCM has two types of payloads you can send. Data payloads and notification payloads. See here. I'm focusing on notification payloads. How do I specify a local large icon to show ? I know I can specify a default notification icon in the manifest this way using metadata:
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#drawable/mylogo" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="#color/mycolor" />
But this is for a small icon. How do I specify LARGE icon I'd like to use ?
In the documentation I provided above for FCM, there is a example that looks like this:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
}
}
but this is for small icon. how to do large icon?
A large icon looks like this:
FCM doesn't have an API to set a large-icon.
If you want to achieve that you can send a data-message with custom payload, and create your own local notification inside onMessageReceived()