I'm using firebase cloud function for send push notification. It works but issue by default it shows badge and does not play sound when notification comes.
Below is my playload code.
var playload = {
notification: {
title: msgData.title,
body: msgData.message,
sound: 'default',
badge: '0',
click_action: 'FLUTTER_NOTIFICATION_CLICK'
},
data: {
title: msgData.title,
body: msgData.message,
sound: 'default',
badge: '0',
click_action: 'FLUTTER_NOTIFICATION_CLICK'
}
}
var options = {
priority: "high",
timeToLive: 60 * 60 * 24
};
return admin.messaging().sendToDevice(tokens, playload, options).then((response) => {
console.log('Sent to all the devices');
return response;
}).catch((err) => {
console.log(err);
return 0;
})
I have set 'sound': 'default' and badge: '0' but doesn't help me.
UPDATED:
I have tried with double quote for both keys and values but didn't work yet.
var playload = {
"notification": {
"title": msgData.title,
"body": msgData.message,
"sound": "default",
"badge": "0",
"click_action": "FLUTTER_NOTIFICATION_CLICK"
},
"data": {
"title": msgData.title,
"body": msgData.message,
"sound": "default",
"badge": "0",
"click_action": "FLUTTER_NOTIFICATION_CLICK"
}
}
Have you tried to replace your single quotes with double quotes?
From JSON.org:
A value can be a string in double quotes, or a number, or true or
false or null, or an object or an array. These structures can be
nested.
The title and the body of your message is processed before it eventually will fail which might be why it seems that your formatting is okay.
Let me know whether this has helped you.
Related
I am trying to display notifications in my flutter app using awesome_notifications package (https://pub.dev/packages/awesome_notifications) . Below is my code.
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: UniqueKey().hashCode,
groupKey: message.data["senderUid"],
channelKey: 'basic_channel',
title: message.data["title"],
body: message.data["body"],
summary: message.data["body"], // Anything you want here
notificationLayout: NotificationLayout.Messaging,
),
);
Below is my Json data
"message": {
"token": recieverFcm,
"data": {
"title": senderName,
"body": message,
"chatRoomId": chatRoomId,
"sender_profile_pic": senderProfilePic,
"senderUid": senderUid,
"click_action": "OPEN_CHAT_ROOM"
},
"android": {
"priority": "high"
},
"apns": {
"payload": {
"aps": {
"category": "OPEN_CHAT_ROOM"
}
}
}
}
This works fine and I get notifications like below.
In here it is displaying the first letter of the sender's name as the icon/profile picture. Instead, I would like to display the real photo of the sender. This profile photo is already being sent by the JSON code.
How can I do this?
The question is same as here Call onMessage method when the app is in background in flutter and I am following the thread but since there is no response, I am asking this question again.
I've connected my Flutter app with Firebase Messaging and I am receiving notification when my app is in Foreground using the Flutter Local Notifications Plugin. But I cannot receive any notification when my app is in Background
Following is my code:
// FIREBASE CONFIGURATION & HANDLING
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
debugPrint("onMessage: $message");
final notification = message['data']['notification'];
Map responseBody = convert.jsonDecode(notification);
_showNotification(
icon: responseBody['icon'],
title: responseBody['title'],
body: responseBody['body'],
);
setState(() {});
},
);
// DISPLAY NOTIFICATION - HEADS UP
Future<void> _showNotification({String icon, String title, String body}) async {
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'your channel id',
'your channel name',
'your channel description',
importance: Importance.Max,
priority: Priority.High,
ticker: 'ticker',
);
var iOSPlatformChannelSpecifics = IOSNotificationDetails();
var platformChannelSpecifics = NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0,
title,
body,
platformChannelSpecifics,
);
}
Can someone please guide me how can I receive notifications when my app is in Background
Resolved!!
The issue was with the format of curl calling. As per the instructions given here
Changed this:
{
"data": {
"notification": {
"title": "Notification Title",
"body": "Notification Body",
},
"click_action": "FLUTTER_NOTIFICATION_CLICK"
},
"to": "YOUR_DEVICE_TOKEN"
}
To this:
{
"data": {
"click_action": "FLUTTER_NOTIFICATION_CLICK",
"id": "1",
"status": "done",
},
"notification": {
"body": "Notification Body",
"title": "Notification Title"
},
"priority": "high",
"to": "YOUR_DEVICE_TOKEN"
}
I am trying to speed up notifications delivery on firebase notifications.
Because sometimes it might take 10 seconds to get the notification.
Currently I am using this message to send notification with admin.messaging().send(message).
const message = {
notification: {
title: 'Immediate Notification',
body: 'Important Message',
},
data: {
title: 'data',
body: 'some data'
},
android: {
ttl: 1000,
"notification": {
"sound": "default"
}
},
apns: {
headers: {
"apns-expiration": "1000",
},
payload: {
aps: {
badge: 1,
"sound": "default"
},
},
},
};
I am not sure if the speed will increase.
Need Clerification:-
1) If I put "apns-priority": "10" for iOS ? and priority high for android ? Something like this:
headers: {"apns-priority": "10"}
"android":{"priority":"high"},
2) If I set Time To Live to 0 ? ttl:0 and "apns-expiration": 0
3) If I don't send anything in data object ?
Or maybe there are any other way to speed them up ?
Is there a way with FCM to send to two server keys at once with one reqeust? I'm currently using the following code to send notifications
const rp = require("request-promise");
const rawKeys = "key-one, key-two";
function sendToKey(serverKey) {
const requestOptions = {
method: "POST",
uri: "https://fcm.googleapis.com/fcm/send",
headers: {
Authorization: "key=" + serverKey
},
json: true,
body: {
to: "Device Token Here",
priority: "high",
content_available: true,
notification: {
title: "Test Title",
body: "Test Body",
deepLink: "https://google.com",
sound: "default"
},
data: {
title: "Test Title",
body: "Test Body",
deepLink: "https://google.com"
}
}
};
return rp(requestOptions)
.then(resp => {
console.log(JSON.stringify(resp));
});
}
rawKeys.split(",").map(serverKey => sendToKey(serverKey));
I'm trying to avoid running multiple reqeusts so is there a way to send to two keys within a single request?
I have a mobile app built on Ionic 3, using Firebase and FCM plugin to send notifications.
I have 2 problems :
The badge never appears (tested on iOs and Android) (but the notifications are working normally)
When I click on the notification, I am re-directed to my application's home page. But I would like to be re-directed on a specific page of my application. Apparently, that should be specified by changing the "activity" on the "click_action" parameter, however my app doesn't have any activity.
Thanks for your help.
Here is my code :
sendNotif(){
this.postRequest()
var headers = new Headers();
headers.append("Accept", 'application/json');
headers.append('Content-Type', 'application/json');
headers.append('Authorization', 'key=xxxxx:xxxx')
let options = new RequestOptions({ headers: headers });
let postParams = {
"notification": {
"title": "Mon-appli",
"body": "Nouvelle réservation",
"sound": "default",
"click_action": "FCM_PLUGIN_ACTIVITY",
"icon": "fcm_push_icon"
},
"data": {
"param1": "value1",
"param2": "value2"
},
"to": "/topics/all",
"priority": "high",
"restricted_package_name": ""
}
this.http.post("https://fcm.googleapis.com/fcm/send", postParams, options)
.subscribe(data => {
this.nb_notif = this.nb_notif +1;
}, error => {});
}
I ended up here because I have the same problem with badge as you. But I think that I can answer your number 2 problem:
The redirection has nothing to do with your notification set-up. You'll need to unsubscribe from the authentication observer.
So unsubscribe in app.component.ts from the listener as follow:
const authUnsubscribe = afAuth.authState.subscribe( user => {
if (user) {
authUnsubscribe.unsubscribe();
this.rootPage = 'HomePage';
}
else{
this.rootPage = 'LoginPage';
}
});
And for question number 1:
You'll have to add badge:1 in your notification:{..} description.
"notification": {
"title": "Mon-appli",
"body": "Nouvelle réservation",
"sound": "default",
"click_action": "FCM_PLUGIN_ACTIVITY",
"icon": "fcm_push_icon",
"badge":1
}