Flutter fcm push notifications are not able to show icon badges - flutter

final data = {
"notification": {
"title": title,
"body": body,
},
"apns": {
"payload": {
"aps": {"badge": 1}
}
},
"to": pid
};

When you receive your payload data you have to update the badge manually when you are using Flutter.
Integrate this library in your code so it can manage it easily
All you have to do is read the badge that is coming inside the FCM body from the following code block and increase the badge depending on that number OR just increase the badge every time you receive a new notification:
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('Got a message whilst in the foreground!');
print('Message data: ${message.data}');
});

You have to use this type of Response when you get push notification then badges will show definitely.
code response:-
{
"notification": {
"body": "This is an FCM notification message!",
"title": "FCM Message",
"badge":4
},
"priority": "high",
"data": {
"click_action": "FLUTTER_NOTIFICATION_CLICK",
"id": "1",
"status": "done"
},
"to": "eLk6p-WZ_kC-tZL60lRnp-:APA91bFhdwojQVVa-6wSCR9nTctBRZg0_Hx0ZACouKK2DsSjQzQX6EjrwCtJmWXfJzi2dO3t_ckcZmeWmoWoHMkgwrjMK5IAUJ3h1teBYD2mq-PjFb8-W3_PqMugl90e2fz7_AZYyTV-"
}

Related

FirebaseMessaging.onBackgroundMessage is not invoked when my app is on background in flutter , its happening only in Ios devices

I Am making an app which has push notification Property app is working when we hit notification on the foreground , but when app is on background my _backgroundHandler() method is not invoking, its happening only In IOS App Only
also struggle this issue.
set payload as below. mutable-content make sense.
apns: {
payload: {
aps: {
'mutable-content': 1,
'content-available': 1
}
}
}
https://github.com/firebase/flutterfire/issues/9381#issuecomment-1229167020
set "mutable-content:1" in payloads, iOS passes the notification to our notification service app extension.
For more information APNs payload, see the following links:
https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/generating_a_remote_notification
Enable background fetch, background processing and remote notification in Xcode
Edit
Add mutable key to payload
{
"to": "dWdhfjfjdbzbmjJ5....",
"content_available": true,
"mutable_content": true,
"data":
{
"message": "some msg",
"mediaUrl": "image url here"
},
"notification":
{
"body": "notification msg",
"sound": "default"
}
}
EDIT
var payload = {
notification: {
title: `msg title here`,
body: `msg body here`
}`,
},
// Set Android priority to "high"
android: {
priority: "high",
},
// Add APNS (Apple) config
apns: {
payload: {
aps: {
contentAvailable: true,
},
},
headers: {
//"apns-push-type": "background", // This line prevents background notification
"apns-priority": "10",
},
},
token: "dnqTQVso60GfnnuOjHv8_e:APA91bElr-K3xkQMdYHX8VMrasdfasdfkjhasidfgjn"
};
Adding this in my payload solved my issue:
apns: {
payload: {
aps: {
'mutable-content': 1,
'content-available': 1
}
}
}
I have face this issue too.
#goza's answer should been accepted.
This is my sending json.
{
"message":{
"token":"fcm token",
"notification":{
"title":"NotifyTitle",
"body":"NotifyBody"
},
"data":{
"title":"DataTitle",
"body":"DataBody"
},
"apns":{
"payload":{
"aps":{
"content-available":1
}
}
}
}
}

FCM and Swift firebase-sdk version 8.1.0 localization problem

I'm hoping to get some answers here, I have been searching and try almost everything. I even tried to customized my firebase sdk to fit to localization format but it doesnt work. the Push notification on my iphone shows: REMINDER_TO_BRUSH_YOUR_TEETH instead of the localization text i have on my app.
What i tried is:
"notification": {
"title": "CompanyTitle",
"body": "REMINDER_TO_BRUSH_YOUR_TEETH"
"body_loc_key": "REMINDER_TO_BRUSH_YOUR_TEETH" <----- Also tried to add that here
},
"androidConfig": {
"priority": "high",
"ttl": "120s",
"notification": {
"color": "#FFFF00",
"sound": "default"
}
},
"apnsConfig": {
"payload": {
"aps": {
"badge": 17,
"body_loc_key": "REMINDER_TO_BRUSH_YOUR_TEETH" <----- Added that
}
}
},
"token": "cwCqkaNhrE_GsNzrNXxn7V:APA91bHNLg0F7ho9tvOqosWopX9L28FnZxHNoiwxvWJDekVt9hX2RgNa6W_CNUaRe22J2iKcMc5waGHOiC57fLF1H4c-yqOtN7eTujYGWwdMAoGHJEM828-mIa1sMCrSQbZwL7MZOKJI"
}
My piece of code:
private Message.Builder getPreconfiguredMessageBuilder(PushNotificationRequest request) {
AndroidConfig androidConfig = getAndroidConfig(request.getTopic());
ApnsConfig apnsConfig = getApnsConfig(request.getTopic(), request.getBadge(), "REMINDER_TO_BRUSH_YOUR_TEETH");
Notification notification = Notification
.builder()
.setTitle(request.getTitle())
.setBody(request.getMessage())
.build();
return Message.builder()
.setApnsConfig(apnsConfig).setAndroidConfig(androidConfig).setNotification(notification);
}
private ApnsConfig getApnsConfig(String topic, int badge, String locKey) {
return ApnsConfig.builder()
.setAps(Aps.builder().setBadge(badge).setLocKey(locKey).setCategory(topic).setThreadId(topic).build()).build();
}
Thank you for your help in Advance.
This appears to be a syntax issue. Try the following:
"apnsConfig": {
"payload": {
"aps": {
"alert": {
"title": "some title",
"body": "some body"
}
}
}
}

How can I send notification from Postman using FCM?

I am using a Post method from the postman to send notifications to my flutter app, If I include the body outside of the notification in this way I can send notifications.
{
"to": "cu6HFvgXSCyKz5kjp..........",
"notification": {
"sound": "default",
"body": "test body",
"title": "test title",
"content_available": true,
"priority": "high"
},
"data": {
"sound": "default",
"body": "test body",
"title": "test title",
"content_available": true,
"priority": "high",
"ride_request_id":"asdfasd"
}
}
I can parse the data to get my value from ride_request_id but the catch is I get double notification, so FCM has suggested including the body inside the notification, Now, neither do I get the notification nor able to parse it. Although in Postman it shows as successful.
{
"to": "cu6HFvgXSCyKz5kjp..........",
"notification": {
"sound": "default",
"body": "test body",
"title": "test title",
"content_available": true,
"priority": "high",
"data": {
"sound": "default",
"body": "test body",
"title": "test title",
"content_available": true,
"priority": "high",
"ride_request_id": "asdfasd"
}
}
}
here is an article about sending a notification using postman:
Test FCM Notification with POSTMAN

Rich Push notification with firebase and sending push notification with FCM on ios devices

I have integrated the normal text based push notification through Firebase Cloud Messaging. The push is send through FCM server and its working fine.
But I am stuck on media based push notification including images and media based notification. I have also tested with postman console with format mentioned below:
Use a service api.
URL: https://fcm.googleapis.com/fcm/send
Method Type: POST
Headers:
Content-Type: application/json
Authorization: key=your api key
Body/Payload:
{ "notification": {
"title": "Your Title",
"text": "Your Text"
},
"data": {
"message": "Offer!",
"mediaUrl": "https://cdn.pixabay.com/photo/2018/01/21/01/46/architecture-3095716_960_720.jpg"
},
"to" : "to_id(firebase refreshedToken)"
}
Through this, I am receiving only normal text based push. What should be the error or correct way to send rich notification in ios 10 or later device??
Thanks in advance.
The correct format is this (tested)
{
"to": "deviceFCMtoken",
"notification":{
"title" : "Check this Title",
"subtitle" : "Subtitle",
"body" : "Body",
"mutable_content" : true,
"category" : "categoryID"
},
"data":{
"image-url": "www.something.com"
}
}
You should provide the mutable-content and content-available in your FCM payload. Both are boolean and must also be outside the notification parameter
{
"to" : "to_id(firebase refreshedToken),
"mutable_content": true,
"content-available": true,
"data": {
"message": "Offer!",
"mediaUrl": "https://cdn.pixabay.com/photo/2018/01/21/01/46/architecture-3095716_960_720.jpg
},
"notification": {
"title": "my title",
"subtitle": "my subtitle",
"body": "some body"
}
}

sending push notification to Iphone using FCM

I am trying to send push notification to iPhone using FCM but notification is not received on my phone. Is there any issue in request json? please help me.
my request json is
{ "notification":{"aps": {"alert": "Hello","badge": 1,"sound ": "default "},"pushtype": "", "message": "Hello"} ,"to" : "c6IJyzv8LiY:APA91bHE1g-51dWnh_ofus0nUfVGwWQBV8zOp5fn9VcQe5GLL6gqLUCQQv1RUnYvQ0MP7Q5bKwgG1_8Uuudf67HV4hU1N-3U4u4uRVpdXw3QrivE8ONxjAAfcbn5cIUzu3B6FbHR8EkG"}
and my response json is:
{\"multicast_id\":5810493506209108338,\"success\":1,\"failure\":0,\"canonical_ids\":0,\"results\":[{\"message_id\":\"0:1473238399084785%fc69f82ff9fd7ecd\"}]}
I have fixed my problem, correct request json is:
{
"notification ": {
"title": "some title",
"text": "some text"
},
"to": "XjV2kOqh1yXNkJsQop17RlwSwT4jZpuAu-...",
"priority": "high"
}