I have set in my server side application the FCM service and I have been sending notifications (one by one) and has worked as expected.
Now, I'm trying to send notifications using MulticastMessage. It works good, devices are receiving notifications properly. However I don't know how to set a different badge number for every user.
I'm using the addAllTokens method to set the devices, and I was looking something like addAllBadges to send a list of badges corresponding to every device I insert in addAllTokens, but I can not find a solution. Is it possible? How can I do that?
Thank you very much!
You can achieve this by doing something like.....
First of all you need to query your DB to get the device token and badge count respectively
So say like you have and array
const badgeCounts = [{
device_token:'aaaaaaaaaaaaaaaaaaaaaa',
badge_count: 1
},{
device_token:'bbbbbbbbbbbbbbbbbbbbbb',
badge_count: 2
},{
device_token:'cccccccccccccccccccccc',
badge_count: 3
},{
device_token:'dddddddddddddddddddddd',
badge_count: 4
}]
Now you can map over this array and compose array of fcm messages, something like below :-
const fcmMessages = [];
badgeCounts.forEach((data) => {
fcmMessages.push({
token: data.device_token, //device token
apns: {
payload: {
aps: {
alert: {
title: "your title",
body: "your body",
},
badge: data.badge_count, // badge
contentAvailable: true,
},
},
},
data: {
// any payload goes here...
},
notification: {
title: "your title",
body: "your body",
},
});
});
/// in firebase messiging you can do like
messaging.sendAll(fcmMessages);
refer doc https://firebase.google.com/docs/reference/admin/node/admin.messaging.Messaging-1#sendall
As far as I know the message to each user in a multicast message is exactly the same. So if you need to show a different badge count, you'll need to do separate messages for these. But all recipients with the same content/badge count could be part of the same multicast message.
Related
iOS distinguishes between messages by UNNotificationInterruptionLevel. I would like to achieve that messages sent via FCM have the time-sensitive interruption-level.
Is this equivalent to just sending messages in FCM with high priority? Unfortunately it's not super clear to me from looking at the docs.
The interruption level is automatically handled by system, not by FCM. That's different than the high priority.
You should be able to use it as it is by following Apple's documentation. FCM supports passing down the interruption-level in the payload.
I achieved this by having this payload in my firebase console file:
var message = {
notification: {
title: "Notification Title",
body: `${initiatedUsername} sent you message`,
},
"data": {
"target_exec":"messaging"
},
"apns": {
"payload": {
"aps": {
"alert": {
"title": "Notification Title",
"body": `${initiatedUsername} sent you a message`
},
"badge": 1,
"sound": "default",
"interruption-level": "time-sensitive"
}
}
},
token: fcmToken,
};
You can change the token attribute to topic aswell, if you'd rather like to send a message to a topic.
Hope this might help somebody out here!
I am developing a bot to link to my NodeJS application and am using quick replies to receive the user's email address and telephone number.
However, the reply contains a text and payload value that are the same, which makes catching the response and processing it impossible.. So I must be doing something wrong.
Here's what I send:
response = {
"text": "We need your phone number to match you with our records",
"quick_replies":[
{
"content_type":"user_phone_number",
"payload":"PHONE_NUMBER"
}
]
}
callSendAPI(sender_psid, response);
But when the user clicks their Quick Reply button I get:
{ sender: { id: '<some value>' },
recipient: { id: '<some value>' },
timestamp: 1622370102305,
message:
{ mid:
'<some value>',
text: 'me#example.com',
quick_reply: { payload: 'me#exmaple.com' }
}
}
How would I identify a specific Quick Reply response for processing?
With text replies I can assign a payload, then listen out for that payload being returned.
If the payload of a quick reply is dynamic, I don't see a way to process the user response since if (response.message.quick_reply.payload === 'PHONE_NUMBER') can't work here like the rest of the script.
Unfortunately, according to the docs, that's just how it is.
For an email/phone quick reply, the message.quick_reply.payload will either be the email or phone number as appropriate.
However, while the quick replies are available, the user can still manually type in a different email or phone number to what they have registered with Facebook - it's just for convenience. Because they can send back any free form text they like, you should be parsing the message.text property anyway.
parseResponseForEmailAndPhone(response) {
const text = response.message.text;
if (looksLikeAnEmail(text)) {
return { email: text };
} else if (looksLikeAPhoneNumber(text)) {
return { phone: text };
}
// TODO: handle other message
// unlikely, but could even be a sentence:
// - "my phone is +000000"
// - "my email is me#example.com"
// - "+000000 me#example.com"
// You also need to handle non-consent
// - "you don't need it"
// - "I don't have one"
// - "skip"
const result = {};
// please use a library for these instead,
// they are used here just as an example
const phoneMatches = /phoneRegEx/.exec(text);
const emailMatches = /emailRegEx/.exec(text);
if (phoneMatches) {
result.phone = phoneMatches[1];
}
if (emailMatches) {
result.email = emailMatches[1];
}
return result;
}
I have created a PWA and implemented push notifications.
From what I understand notifications can be replaced using tag and behaviour controlled using renotify. Google documentation here.
This may be a good way to keep down the amount of displayed notifications, but is there a way to group notifications in a PWA?
In native apps I have noticed that notifications get "grouped" or "stacked" into one notification if there are more than a certain amount of notifications. Can this be achieved in a PWA?
Code for showing a push notification:
self.addEventListener('push', function (event){
console.log('Push Notification received', event)
const defaultData = {title: 'New!', content: 'Something new happened!', openUrl: '/'}
const data = (event.data) ? JSON.parse(event.data.text()) : defaultData
var options = {
body: data.content,
icon: '/img/ico/manifest-512.png',
badge: '/img/ico/badge96.png',
// tag: 'myTag',
// renotify: true,
data: {
url: data.openUrl
}
}
event.waitUntil(
self.registration.showNotification(data.title, options)
)
})
Kind regards /K
How to skip/open a specific screen based notification. Example: When a user clicks on the notification, the application should open and go directly to the notifications page instead of the home page.
when i was using react-native-push-notification i mannage to get the text of my notification this way :
componentWillMount() {
var _this = this;
PushNotification.configure({
onRegister: function(token) {
_this.props.InitToken(token.token);
},
onNotification: function(notification) {
setTimeout(() => {
if(!notification['foreground']){
_this.props.InitNotif(notification['message']);
}
}, 1);
PushNotification.localNotificationSchedule({
title: 'Notification with my name',
message: notification['name'], // (required)
date: new Date(Date.now()) // in 60 secs
});
},
// ANDROID ONLY: GCM Sender ID (optional - not required for local notifications, but is need to receive remote push notifications)
senderID: "YourID",
});
}
where
_this.props.InitToken(token.token);
and
_this.props.InitNotif(notification['message']);
are redux based function that update your state with the notification token and message. When you'r state is updated whith the notification message your can change route ou display the message on screen.
PS:
i dont know if it was the good way but i was working
componentWillMount() is depreciated.
I use nativescript-local-notifications and nativescript-plugin-firebase.
I want to create notification each time, when server send me a message. Here is code:
firebase.init({
storageBucket: 'gs://shao-by-partner-firebase.appspot.com',
persist: true, // optional, default false
onPushTokenReceivedCallback: function (token) {
Config.device_token = token;
console.log("Firebase plugin received a push token: " + token);
},
onMessageReceivedCallback: function (message) {
LocalNotifications.schedule([{
id: 1,
title: 'The title',
body: 'Recurs every minute until cancelled',
ticker: 'The ticker',
badge: 1,
smallIcon: 'res://heart.png'
}]).then(
function() {
console.log("Notification scheduled");
},
function(error) {
console.log("scheduling error: " + error);
}
)
}
}).then(
function (result) {
console.log("Firebase is ready");
},
function (error) {
console.log("firebase.init error: " + error);
}
);
}
But always created two notifications instead of one. And first notification is empty. And second notification is that I created. What's wrong?
I could be wrong here, but you are using two plugins to handle remote notifications?
nativescript-plugin-firebase already handles notifications for you, I don't think you need the local notifications plugin.
In the example you posted, you are receiving the original notification (the blank one), pluss the local one that you create in the onMessageReceivedCallback.
https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/master/docs/MESSAGING.md#handling-a-notification
The is nothing in your onMessageReceived to determine if the message had any content.
You can try adding a conditional statement to your onMessageReceived function and check if the message has content. eg
if (message.title){
//send local notification
}
I found solution. Each time, when I create LocalNotification, I delete notification with id 0:
LocalNotifications.cancel (0);
But I still don't know,where it comes from.