Amazon SNS Message Structure Not working - push-notification

{
"default":"This is the default Message",
"APNS_SANDBOX":{
"aps":{
"alert":{
"title":"JustFollow",
"body":"ABc"
}
}
},
"GCM":{
"data":{
"message":"abc"
}
}
}
This is the Josn mesage which will be send at the time when Amazon Notification is Published, But when on Mobile we get the Notification, it will taking nothing Except "default tag". I want to Fetch GCM and APNS_SandBox Details.
SEND NOTIFICATION CODE
_amazonSimpleNotificationServiceClient.Publish(new PublishRequest
{
Subject = StringConstant.ApplicationName,
Message = "abc",
//MessageStructure ="json",
TopicArn = topicArn
});

If sending message format in below format, it is working properly.
{
"default":"abc",
"GCM":"{\"data\":{\"message\":\"12\",\"url\":\"www.amazon.com\"}}",
"APNS_SANDBOX": "{\"aps\":{\"alert\": \"12\",\"url\":\"www.amazon.com\"} }"
}

Related

Facebook Messenger Quick Replies

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;
}

HMS cancel notification with tag not working ( (HMS push kit)

I am integrating Huawei Push Kit (https://pub.dev/packages/huawei_push) in Flutter application.
I test to schedule a notification to notify every minutes in my Huawei phone, and I able to get the notification as expected. But when I tried to cancel the notification with clearNotification() function (shown below), the notification seem like not cancelled. Wondering do I miss out anything?
scheduleLocalNotification() async {
try {
Map<String, dynamic> localNotification = {
HMSLocalNotificationAttr.TITLE: 'Notification Title',
HMSLocalNotificationAttr.MESSAGE: 'Notification Message',
HMSLocalNotificationAttr.TICKER: "OptionalTicker",
HMSLocalNotificationAttr.TAG: "push-tag",
HMSLocalNotificationAttr.BIG_TEXT: 'This is a bigText',
HMSLocalNotificationAttr.SUB_TEXT: 'This is a subText',
HMSLocalNotificationAttr.LARGE_ICON: 'ic_launcher',
HMSLocalNotificationAttr.SMALL_ICON: 'ic_notification',
HMSLocalNotificationAttr.COLOR: "white",
HMSLocalNotificationAttr.VIBRATE: true,
HMSLocalNotificationAttr.VIBRATE_DURATION: 1000.0,
HMSLocalNotificationAttr.ONGOING: false,
HMSLocalNotificationAttr.DONT_NOTIFY_IN_FOREGROUND: false,
HMSLocalNotificationAttr.AUTO_CANCEL: false,
HMSLocalNotificationAttr.INVOKE_APP: false,
HMSLocalNotificationAttr.ACTIONS: ["Yes", "No"],
HMSLocalNotificationAttr.REPEAT_TYPE: RepeatType.MINUTE,
HMSLocalNotificationAttr.FIRE_DATE:
DateTime.now().add(Duration(minutes: 1)).millisecondsSinceEpoch,
};
Map<String, dynamic> response =
await Push.localNotificationSchedule(localNotification);
print("Scheduled a local notification: " + response.toString());
} catch (e) {
print("Error: " + e.toString());
}
}
clearNotification() async {
Push.cancelNotificationsWithTag('push-tag');
}
Currently, Huawei Push Kit does not support clearNotification() function. You can use the Sending Downlink Messages API. There is an auto_clear parameter, and you can set message display duration, in milliseconds. Messages are automatically deleted after the duration expires.
Please refer to the following code:
{
"validate_only": false,
"message": {
"android": {
"notification":{"body":"Big Boom Body!","click_action":{"type":3},"title":"Big Boom Title","auto_clear": 360000}
},
"token":[
"token1",
"token2"]
}
}
For more information, see docs
Your clearNotification function which calls Push.cancelNotificationWithTag('push-tag') is async.
Possibility is Push.cancelNotificationWithTag might get called before.
Either call it directly and not thru async function or add await.

this.fcm.onNotification().subscribe() not called on Android device

I have an Ionic 4 app which I have integrated with Firebase for authentication etc. I want to implement Firebase's cloud messaging so I can push messages to my app on both Android and iOS. I have done this pretty easily on iOS and I have sent a message via Postman which shows on my iPhone and I see the JSON of the message I have sent. When I try it on Android it doesn't work. Both devices receive the message but handle it very diferently.
I have read in a lot of places that you need to set the click_action to FCM_PLUGIN_ACTIVITY but when I do that the app doesn't even open on Android. When I take it out the app loads when you click the message but it doesn't show the body of the message like on iOS in my alert.
import { FCM } from '#ionic-native/fcm/ngx';
...
constructor(public platform: Platform, public fcm: FCM)
...
this.platform.ready().then(() => {
this.fcm.onNotification().subscribe(data => {
alert(JSON.stringify(data));
});
this.fcm.onTokenRefresh().subscribe(token => {
// Register your new token in your back-end if you want
// backend.registerToken(token);
});
}).catch((error) => {
this.showFailureMessage(error.message);
});
This is what I am posting off to... https://fcm.googleapis.com/fcm/send
{
"notification":{
"title":"My Title",
"body":"My Body",
"sound":"default",
"click_action":"FCM_PLUGIN_ACTIVITY"
"icon":"fcm_push_icon",
},
"data":{
"type":"Something",
},
"to":"/topics/all",
"priority":"high",
"restricted_package_name":""
}
Any help would be very much appreciated.
You seem to be missing the data.wasTapped part in your subscribe. Here, try this:
this.fcm.onNotification().subscribe(data => {
if (data.wasTapped) {
alert('Received in background');
alert(JSON.stringify(data));
} else {
alert('Received in foreground');
alert(JSON.stringify(data));
}
});

React Native: Disable sound/vibration on push notification

I'm sending push notifications from server to clients via google FCM.
In the react-native app I've registered these listeners:
this.notificationOpenedListener = firebase.notifications().onNotificationOpened(async (notificationOpen) => {
})
this.notificationListener = firebase.notifications().onNotification(async (notification) => {
});
the notification data contains information if there should be a sound / vibration or not when the notification is received.
However, I can't find any documentation about completely disabling sound/vibration on demand.
How can I achieve this?
UPDATE
I've tried setting sound to an empty string on server side, but there's still sound/vibration on notification.
var message = {
data: {
userId: fromUserId,
},
notification: {
title: `My notifcation`,
body: `Body`,
sound: "",
},
}
return sendToTopic(topicId, message)
When setting your notification target, remove sound parameter.
const notify = {
...message.notification,
show_in_foreground: true,
notify_type: message.NotificationType,
obj_id: message.ObjectId,
sound: '' // remove this
};
firebase.messaging().createLocalNotification(notify);
I'm not sure how you are setting up your Push notifications but you can tag extra data into the JSON like this:
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data":{
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
}
}
}
This means you could add a boolean to it to manually play a tone on arrival or not play a tone when the subscription fires, like this:
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data":{
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark",
"playTone" : "true"
}
}
}
Then it's the case of having a check in your callback to check the response:
this.notificationListener = firebase.notifications().onNotification(async (notification) => {
if(notification.data.playtone) {
// Play Tone
} else {
// Don't
}
});
In general though push notifications are handled by the OS and not the application, you can hook into it like you have and perform actions on the arrival of a push notification but normally they all arrive in the same "Drop everything and deal with me" style.
Both Android and Apple do support priority but it might not be what you're after:
https://firebase.google.com/docs/cloud-messaging/concept-options#setting-the-priority-of-a-message

Firebase - notificationclick event not receiving payload

I'm struggling with this problem for more than a week... We have implemented push message in Chrome by using Firebase and a Service Worker. Everything works just fine, the messages are being sent and received correctly with the payload. On the service worker, we handle the push message to display a notification and the notificationclick event to send the user to a specific URL when clicking on it, then close the notification.
The problem is with 'old' notifications: if a user receives a message but doesn't clicks on it right away, it keeps there for a while then after some time (not sure how much) he clicks the notification - he gets redirected to https://[domain]/firebase-messaging-sw.js
We have traced the entire process: the notification gets received with all the info properly (the url is also correct, actually if he clicks right when the message is received it works just fine). But if the notification lives there for a while it gets 'emptied'.
The message being sent is pretty simple (just for showing there are no TTLs, nor expiration parameters being used). The curl command looks like that:
curl -X POST
-H "Authorization: key=[SERVER API KEY]"
-H "Content-Type: application/json"
-d '{
"notification":{
"click_action":"[URL to be redirected]",
"icon":"[A custom image]",
"title":"[News title]"},
"to":"/topics/[A topic]"
}'
"https://fcm.googleapis.com/fcm/send"
This is the code for processing the push message on the service worker:
self.addEventListener('push', function(event) {
console.log('Push message received', event);
var data = {};
if (event.data) {
data = event.data.json();
}
event.stopImmediatePropagation();
showNotification(data.notification);
});
function showNotification(notification) {
var click_action = notification.click_action; //<-- This is correct!
var options = {
body: notification.body,
icon: notification.icon,
subtitle: notification.subtitle,
data: {
url: click_action
}
};
if (self.registration.showNotification) {
return self.registration.showNotification(notification.title, options);
}
}
And the code for managing notificationclick event is pretty straightforward:
self.addEventListener('notificationclick', function(event) {
var url = '';
try {
url = event.notification.data.url; // <-- event.notification is null randomly!!
} catch (err) {}
event.waitUntil(self.clients.openWindow(url));
});
Is there any reason for loosing the payload after a certain time on the service worker context? Is this time specified on any documentation somewhere?
Thanks in advance for your help!
//payload of push message
{
"notification": {
"title": data.title,
"body": data.text,
"click_action": url,
"tag": "",
"icon": ""
},
"to": token
}
//in service woker
self.addEventListener("notificationclick", (event) => {
event.waitUntil(async function () {
const allClients = await clients.matchAll({
includeUncontrolled: true
});
let chatClient;
for (const client of allClients) {
if (client['url'].indexOf(event.notification.data.FCM_MSG.notification.click_action) >= 0) {
client.focus();
chatClient = client;
break;
}
}
if (!chatClient) {
chatClient = await clients.openWindow(event.notification.data.FCM_MSG.notification.click_action);
}
}());
});
so in this basically, on click of notification you are redirected to application ,and if the application is not open you are opening the application in new tab , as you are concerned that in event you are not able to get the click_action, could you try event.notification.data.FCM_MSG.notification.click_action by using this and see if you are getting the url, and add the event listener in beginning of service worker https://github.com/firebase/quickstart-js/issues/102

Resources