How to group FCM notifications (having in one stack if there is more than one notification),
I tried using the 'tag' key in my payload (as It was suggested in other answers) but it makes that new notification replaces the last notification.
This what I am using for notification:
data: {
'notification': {
'title': notificationTitle,
'body': notificationBody,
},
'priority': 'high',
'data': {
'click_action': 'FLUTTER_NOTIFICATION_CLICK',
'id': '1',
'status': 'done'
},
'to': userToken
},
}
Related
How can I save two records with the id of the other in flutter?
I'm trying to save two records with id of the other, I can do that, but when I try to save more than 2 at same time some id come with blank. This is my code:
collectionReferenceRel.add({
'idRoom': id,
'room': rel,
'rel': room,
'id': '',
}).then((idRel) {
idRel1 = idRel.id;
},
);
collectionReferenceRel.add({
'idRoom': id,
'room': room,
'rel': rel,
'id': '',
}).then((value2) {
idNode2 = value2.id;
}).whenComplete(() async {
await collectionReferenceRel.doc(idRel1).update({
'id': idRel2,
});
await collectionReferenceRel.doc(idRel2).update({
'id': idRel1,
});
}).catchError((error) {
CustomFullScreenDialog.cancelDialog();
CustomSnackBar.showSnackBar(
context: Get.context,
title: 'Error',
message: 'Something went wrong',
backgroundColor: Colors.green);
[![enter image description here][1]][1] },
);
https://api.flutter-io.cn/flutter/dart-async/Future/wait.html
Future.wait([
collectionReferenceRel.add({
'idRoom': id,
'room': rel,
'rel': room,
'id': '',
}).then((idRel) {
idRel1 = idRel.id;
},
),
collectionReferenceRel.add({
'idRoom': id,
'room': room,
'rel': rel,
'id': '',
}).then((value2) {
idNode2 = value2.id;
})
]).whenComplete(() async {
await collectionReferenceRel.doc(idRel1).update({
'id': idRel2,
});
await collectionReferenceRel.doc(idRel2).update({
'id': idRel1,
});
}).catchError((error) {
CustomFullScreenDialog.cancelDialog();
CustomSnackBar.showSnackBar(
context: Get.context,
title: 'Error',
message: 'Something went wrong',
backgroundColor: Colors.green);
[![enter image description here][1]][1] },
);
Consider using the set() method provided the cloud firestore api.
Usage Example from the reference.
final city = <String, String>{
"name": "Los Angeles",
"state": "CA",
"country": "USA"
};
db.collection("cities")
.doc("LA")
.set(city)
.onError((e, _) => print("Error writing document: $e"));
For saving more than one document consider coupling it with the Future wait for a clean code.
/// Create a list to add all documents
final List docs = [];
/// create the documents with unique identifiers
/// beforehand using a package such as `Uuid`
final docA = {
'id': 'unique_identifier_a',
'idRoom': id,
'room': rel,
'rel': room,
}
docs.add(docA);
final docB = {
'id': 'unique_identifier_b',
'idRoom': id,
'room': rel,
'rel': room,
}
docs.add(docB);
/// Create futures from the documents
final futures = docs.map((e) => collectionRef.doc(e.id).set(e));
/// Save the documents in shot and wait for all
await Future.wait(futures);
i tried method using firebase_messaging: 7.0.1 but now its firebase_messaging: 14.0.2.
i got this error :
Converting object to an encodable object failed: Instance of 'Future<String?>'
sendNotif(String? title, String? body, String? id) async {
try {
await http
.post(
Uri.parse('https://fcm.googleapis.com/fcm/send'),
headers: <String, String>{
'Content-Type': 'application/json',
'Authorization': 'key=$serverToken',
},
body: jsonEncode(
<String, dynamic>{
'notification': <String, dynamic>{
'body': body.toString(),
'title': title.toString()
},
'priority': 'high',
'data': <String, dynamic>{
'click_action': 'FLUTTER_NOTIFICATION_CLICK',
'id': id.toString(),
},
'to': FirebaseMessaging.instance.getToken(),
},
),
)
.then((value) => (value) {
print("send succeffuly");
});
} catch (e) {
print("THe ERROR : $e");
}
}
As the error message says, FirebaseMessaging.instance.getToken() returns a Future<String>, while the post API expects only current (non-Future) values.
The simplest fix is to await the value:
'to': await FirebaseMessaging.instance.getToken(),
how can get paylod data when I using FCM in flutter?
I tried used this methods, but I still could not got the data...
where has issues?
void getInitialMessage() async {
RemoteMessage? message =
await FirebaseMessaging.instance.getInitialMessage();
print(message?.data["type"]);
if (message != null) {
if (message.data["type"] == "noti") {
print("AAAAA");
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) =>
PostDetailScreen(postid: message.data["postid"]),
),
);
} else if (message.data["type"] == "active") {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => PostDetailScreen(postid: '456'),
),
);
}
}
}
When I go to postman to test, I will send this format
Future<void> sendPushMessage(String token, String body, String title,
String postid, String images) async {
try {
await http.post(
Uri.parse('https://fcm.googleapis.com/fcm/send'),
headers: <String, String>{
'Content-Type': 'application/json',
'Authorization':
'key=AAAAm2nkpqg:APA91bH9l8kYkJqGyGnVJhUe4dmG5KeYVrErEB_vl7vhZDGBAgFGOYsyHguDna-SBeP8juVoTtLQ61aI61QZ-46JFwaR-8KPai7CT6n4-jRZFBIMOHEl1Ph',
},
body: jsonEncode(
<String, dynamic>{
'notification': <String, dynamic>{'body': body, 'title': title},
'priority': 'high',
'timeToLive': 24 * 60 * 60,
'data': <String, dynamic>{
'click_action': 'FLUTTER_NOTIFICATION_CLICK',
'id': '1',
'status': 'done',
'type': 'noti',
'postid': 'Mi6Y3sE9E0uz4uAvQjwC'
},
"to": token,
},
),
);
} catch (e) {
print("error push notification");
}
}
I can receive the Notification, but I have setting "type": "noti". So when I click the notification message, it shoule be print AAAAA and Navigator to PostDetailScreen. But It's always open app then go to homepage....
Send out FCM message with this structure, here I use NodeJS Firebase Cloud Function, but the important thing is to add the second, options part:
await admin.messaging().sendToDevice(tokens, {
data: {
click_action: 'FLUTTER_NOTIFICATION_CLICK',
type: 'noti',
postid: 'ulEdnFiEZxyyc33UNvJs'
},
notification: {
title: 'title',
body: 'body'
},
}, {
contentAvailable: true,
priority: 'high',
timeToLive: 24 * 60 * 60, // in seconds
});
After this, in getInitialMessage you should have:
message.data['type'] // 'noti'
message.data['postid'] // 'ulEdnFiEZxyyc33UNvJs'
currently I am trying to send a notification to multiple device. I already read the documentation on how to send notification to device group , they mentioned that I need to use registration_ids: [...] instead of to: token. And I also read somewhere that they mentioned about notification_key which is it will enabled to send the notification to the other device. So, I'm stuck on finding the key. But then, after few days browsing, I found out that here stated that the notification_key already deprecated. So, I would like to ask if any of you guys know how to send notification to multiple device without using console.
This I my code segment to send push the notification:
try {
await http.post(
Uri.parse('https://fcm.googleapis.com/fcm/send'),
// Uri.parse('https://fcm.googleapis.com/fcm/notification'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': 'key=$serverKey',
'project_id':'....',
},
body: jsonEncode(
<String, dynamic>{
'notification': <String, dynamic>{
'body': 'this is a body2',
'title': 'this is a title'
},
'priority': 'high',
'data': <String, dynamic>{
'click_action':
'FLUTTER_NOTIFICATION_CLICK',
'id': '1',
'status': 'done'
},
'registration_ids': ['fbN9aYoORhihksrTo7j5D2:APA91b......', 'fArqpgKrTJ0fL8SUKddy2F:APA91bFWf1cnVMS8.......'],
// 'to': _token,
},
),
);
} catch (e) {
print("error push notification");
}
It work fine if I used to instead of registration_ids, but the things is as I understand is to is used if I want to send notification only for one device.
I already stuck with this issue for three days and still not found any solution. Most of them are using console instead. Your help will really made my day. Thank you in advance!
I found a solution and its work!
var serverKey =
'AAAAUb...';
QuerySnapshot ref =
await FirebaseFirestore.instance.collection('users').get();
try {
ref.docs.forEach((snapshot) async {
http.Response response = await http.post(
Uri.parse('https://fcm.googleapis.com/fcm/send'),
headers: <String, String>{
'Content-Type': 'application/json',
'Authorization': 'key=$serverKey',
},
body: jsonEncode(
<String, dynamic>{
'notification': <String, dynamic>{
'body': 'this is a body',
'title': 'this is a title'
},
'priority': 'high',
'data': <String, dynamic>{
'click_action': 'FLUTTER_NOTIFICATION_CLICK',
'id': '1',
'status': 'done'
},
'to': snapshot.data() ['tokenID'],
},
),
);
});
} catch (e) {
print("error push notification");
}
I'm using paypalrestsdk for Credit Card Payment. When I switch to SANDBOX mode and make a request, the paypal service return me this:
{'update_time': u'2016-11-17T16:47:46Z',
'payer':
{'payment_method': u'credit_card',
'funding_instruments': [
{'credit_card':
{'first_name': u'first_name',
'billing_address': {'city': u'London', 'postal_code': u'123','line1': u'fooo', 'country_code': u'EN'},
'expire_month': u'12',
'number': u'xxxxxxxxxxxx1111',
'last_name': u'last_name',
'expire_year': u'2020',
'type': u'visa'}}]},
'links': [
{'href': u'https://api.sandbox.paypal.com/v1/payments/payment/PAY-1GH35642K71421451LAW56MQ',
'method': u'GET',
'rel': u'self'}
],
'transactions': [
{'item_list': {
'items': [
{'currency': u'USD',
'price': u'367.77',
'name': u'Foooo',
'quantity': u'10'}],
'shipping_address': {'city': u'London', 'line1': u'line1', 'recipient_name': u'name', 'phone': u'321312', 'state': u'state', 'postal_code': u'123', 'country_code': u'EN'}},
'related_resources': [],
'amount': {'currency': u'USD', 'total': u'3688.77', 'details': {'subtotal': u'3677.70', 'shipping': u'11.07'}},
'description': u'Charge for order: #1'}],
'state': u'created',
'create_time': u'2016-11-17T16:47:46Z',
'intent': u'sale',
'id': u'PAY-1GH35642K71421451LAW56MQ'}
why is the related_resources empty? How can i test my code in sandbox mode? Of course in PRODUCTION mode related_resources contain sales as in example: https://developer.paypal.com/docs/integration/direct/accept-credit-cards/
The number of credit card is 4111111111111111.
4111111111111111 is not working anymore, you can try with an other one like 4929931129414294