FCM wont stack similar messages? - android

I am trying to stack similar messages instead of sending separate notification for every same message.
Below is my code
const payload = {
notification: {
title: `added new report`,
body: change.after.data().report_title,
tag: "ReportLog",
},
data: {
click_action: "FLUTTER_NOTIFICATION_CLICK",
sound: "default",
status: "done",
},
};
const options = {
priority: "high",
collapseKey: "ReportLog",
};
await admin.messaging().sendToDevice(tokens, payload, options);
SO suppose i send the same message again and again , instead of stacking the messages, the old notification get replace by new

On the server side you can "remember" the last notify and don't send another. (Bad idea)
On the client you can do the same. (Also bad idea)
But the thing is, that the "remember" logic won't work if the app is the background, because the notify will be handled by the system and not by your logic in the app.

Related

Send push notifications via AWS Pinpoint to specified User

I am using react-native and amplify to send push notifications to devices via AWS Pinpoint. I can get the generated token for the devices. But I just need to send push notifications using user Id. I try to update the endpoint, but it's not working. Can anyone suggest me the proper way to handle this?
PushNotification.onRegister((token) => {
console.log('in app registration', token);
Analytics.updateEndpoint({
address: token,
channelType: "GCM",
OptOut: 'NONE',
userId: "12345"
}).then(data => {
console.log(data)
}).catch(error => {
console.log(error)
});
});
It depends how you would like to send the push notification. We have created UI that allows sending a push, which triggers a lambda.
Firstly, you need the app to update the endpoint with the token / address as you have done.
Then you can send the push from the lambda, as shown in this code.
const sendPushNotification = async () => {
const params = {
ApplicationId: "PINPOINT_ANALYTICS_ID",
SendUsersMessageRequest: {
Users: {
"12345": {} // replace numbers with userid here connected with pinpoint endpoint
},
MessageConfiguration: {
APNSMessage: {
Action: 'OPEN_APP',
Title: 'Title of push notification',
SilentPush: false,
Sound: 'default',
Body: 'Message you would like to send'
},
GCMMessage: {
Action: 'OPEN_APP',
Title: 'Title of push notification',
SilentPush: false,
Sound: 'default',
Body: 'Message you would like to send'
},
},
},
};
return pinpoint.sendUsersMessages(params).promise();
};
await sendPushNotification();
I was able to do that using #aws-amplify/analytics library. Following is the method that I used.
Analytics.configure(aws_exports);
PushNotification.onRegister((token) => {
//alert(token)
console.log('in app registration', token);
Analytics.updateEndpoint({
address: token, // The unique identifier for the recipient. For example, an address could be a device token, email address, or mobile phone number.
attributes: {
// Custom attributes that your app reports to Amazon Pinpoint. You can use these attributes as selection criteria when you create a segment.
hobbies: ['piano', 'hiking'],
interests: ['basketball']
},
channelType: 'GCM', // The channel type. Valid values: APNS, GCM
userId: '221XWsdfER234',
// User attributes
optOut: 'ALL',
userAttributes: {
interests: ['football', 'basketball', 'AWS']
// ...
}
}).then((data) => {
console.log(data)
}).catch(error => {
console.log(error)
})
});
With Amazon Pinpoint, you can’t send transactional message as Push notification. Meaning, you can’t send direct Push notification to a specific recipient.
Amazon Pinpoint - Push notification supports sending notifications to a targeted audience by creating a campaign and segment.
If it’s for testing only, from Pinpoint dashboard, you can send Test message to a specific user by using User ID or by using device token.
Read more here =>
Sending Transactional Messages from Your Apps - Amazon Pinpoint

cordova fcm push notifications using firebase functions issue

I am using firebase function and sending the notification like below
let pushPayload = {
notification: {
title: 'Doodles Notification',
body: "push testing",
sound: 'default',
badge: '1'
}
};
//push tokens need to be of customer as well as all the admins in the system. fetch admin push tokens
admin.messaging().sendToDevice(pushToken, pushPayload).then(
(resp) => {
console.log("push notification sent using test method")
return
}
).catch(
(err) => {
console.log("Error sending push notification:" + JSON.stringify(err))
return
}
)
The client side ionic app has method like below:
this.fcm.onNotification().subscribe(data => {
console.log("push message is:" + JSON.stringify(data))
alert(data.aps.alert.body)
});
on ios this all works great. however on android, the console.log prints
{"wasTapped":false}
I am ok with the above expected property but where is the data? The firebase documentation is confusing around it and I am sort of lost what is the correct payload I need to use that works with both ios and android.
Your pushPayload also needs to have a data array to pass data. I believe you also need speech marks around your keys:
"notification":{
"title":"Doodles Notification",
"body":"push testing",
"sound":"default",
"badge":"1"
},
"data":{
"Key1":"Test",
"Key2":"More data"
}
Documentation

Heads up notification using FCM

Do we have any way to show heads up notification, when receive push from Firebase Cloud Messaging? In foreground it is possible using Notification. But there is no way to show heads up notification, when app in background, because onMessageReceived is not calling.
I tried to set priority to "high", but no result.
Do you have any ideas?
After browsing multiple solutions in stackoverflow.com, I found below explanation to be most helpful
How to handle notification when app in background in Firebase
In your Android OnMessageReceived function/method, only Data Payload (RemoteMessage.getData) will trigger your custom notification such as head-up notification when App is in background. Notification Payload (RemoteMessage.getNotification) will not trigger your heads-up notification, and instead default to showing just a tiny icon on your notification tray.
If you're using Firebase Cloud Functions, do check that the information you're sending through is in the data payload, and notification payload should be removed if you want the heads up notification to work. Having notification payload will default to showing just a tiny icon on the notification tray only.
Hope this piece of information helps, and happy coding.
I am using node.js as my server platform. The trick is to remove the 'notification' field and move the 'title' and 'body' fields to 'data'. For my case, I changed:
var message = {
token: targetID,
android:{
priority: 'high'
},
notification: {
title: 'X',
body: req.body.msg
},
data: {
topic: req.body.topic
}
};
to
var message = {
token: targetID,
android:{
priority: 'high'
},
data: {
topic: req.body.topic,
title: 'X',
body: req.body.msg
}
};
then...
fcm.send(message, function(err, response){...
Please follow the below steps..
Now just click next on following two steps with the default setup
This step is very important and this is the main part which will manage the device’s system tray to show as a heads-up notification.
Now click on review and then publish. Let me know if you get the expected result.
Regardless of what platform you are calling firebase, all what you need is to add Title and Body fields to Notification inside Android and of course Priority = High
var aMessage = new Message
{
Topic = "atopic",
Notification = new Notification { Body = body, Title = title },
Data = new Dictionary<string,string> { { "ReferenceId", refId.ToString() } },
Android = new AndroidConfig
{
Priority = Priority.High,
Notification = new AndroidNotification
{
Body = body,
Title = title,
Color = "#ffcc00",
Icon = "ic_notification",
Sound = "default",
ChannelId = "my_notification_channel"
},
},
};

FCM Data Message should be non-collapsibl by default

Based on the documentation, it is my understanding that there are two types of messages that Firebase can send: Notification and Data. Also, they can be either collapsible or non-collapsible with non-collapsible being the default for data messages. This would mean that each message is delivered to the client app. See below:
However, when I send data messages to my client they are collapsed. For example, I send one and it appears in the notification bar with no problem but if I don't open it and another message comes in, it is replaced by the new message. Here's some of my code.
Data message:
//create the notification payload
let payload = {
data: {
title: 'Title',
context: context,
parent: parent,
body: user + " commented on your contribution.",
sound: 'default'
}
};
//send the notification
return admin.messaging().sendToDevice(userToken, payload).then(ok =>{
console.log('Notification sent to: ' + submitter);
}).catch(error => {
console.log('Could not send notification.');
});
What am I doing wrong? I want each notification to appear and not be replaced.
You are debugging at the wrong end. The actual problem is on the client side where you are posting the notification.
While posting the notification if the notification id is the same Android OS will replace the existing notification.
Look for the notify() in your code. Refer to this doc for more details : https://developer.android.com/reference/android/app/NotificationManager.html#notify(int, android.app.Notification)

React Native Push Notifications with react-native-fcm

I'm using this library.
I'm attempting to get push notifications to display in the notifications tray on my Nexus 5 (android 6.0.1). Using React Native 0.42, React Native CLI 2.0.1. I'm developing on Ubuntu 14.04.
I'm using firebase. I go into my console > notifications > send a message > specific device (which I get from remote debugging console.log, below).
I am logging notifications, as you can see in code, and they do get to my device, as I can see them in the logs.
But, I don't know how to display them in the notifications tray. Looking through the docs and searching forums, it seems they should show up by default.
componentDidMount() {
FCM.requestPermissions(); // for iOS
FCM.getFCMToken().then(token => {
console.log(token)
// store fcm token in your server
});
this.notificationListener = FCM.on(FCMEvent.Notification, async (notif) => {
console.log(notif)
});
});
It seems that "custom_notification" is required to display the notification in the top tray. I added this to my payload:
"custom_notification": {
"body": "test body",
"title": "test title",
"color":"#00ACD4",
"priority":"high",
"icon":"ic_notif",
"group": "GROUP",
"id": "id",
"show_in_foreground": true
}
So, I think the app must receive the notification, parse out the data, and add this custom_notification parameter.
How about the following in your constructor:
FCM.requestPermissions(); // for iOS
FCM.getFCMToken().then(token => {
console.log(token)
// store fcm token in your server
});
this.notificationListener = FCM.on(FCMEvent.Notification, async (notif) => {
// do some component related stuff
console.log(notif);
//alert(notif.fcm.body);
FCM.presentLocalNotification({
id: "UNIQ_ID_STRING", // (optional for instant notification)
title: "My Notification Title", // as FCM payload
body: notif.fcm.body, // as FCM payload (required)
sound: "default", // as FCM payload
priority: "high", // as FCM payload
click_action: "ACTION", // as FCM payload
badge: 10, // as FCM payload IOS only, set 0 to clear badges
number: 10, // Android only
ticker: "My Notification Ticker", // Android only
auto_cancel: true, // Android only (default true)
large_icon: "ic_launcher", // Android only
icon: "ic_launcher", // as FCM payload, you can relace this with custom icon you put in mipmap
big_text: "Show when notification is expanded", // Android only
sub_text: "This is a subText", // Android only
color: "red", // Android only
vibrate: 300, // Android only default: 300, no vibration if you pass null
tag: 'some_tag', // Android only
group: "group", // Android only
picture: "https://google.png", // Android only bigPicture style
ongoing: true, // Android only
my_custom_data: 'my_custom_field_value', // extra data you want to throw
lights: true, // Android only, LED blinking (default false)
show_in_foreground: true // notification when app is in foreground (local & remote)
});
});
FCM.subscribeToTopic('test_topic');

Categories

Resources