How to localize numbers in a GCM/FCM notification message? - android

When sending a push notification through GCM (switching to FCM soon) to an Android device, how can I localize numbers that appear in the message title/body based on the language of the phone?
Example Message:
John Doe 137
If the device language is set to Arabic the numbers should localize
Localized Message
John Doe ١٣٧
Since I don't know the users device language when sending the notification, how can I localize the number when the message is received? The onMessageReceived callback does not execute unless the application is running in the foreground, so I can not perform a string replacement on the numbers there.
For context Below is an example of the gcmNotification JSON I'm sending to GCM from my services which is taking advantage of some of the other localization params the payload allows you to use.
"gcmNotification": {
"title_loc_key": "some_title_key",
"title_loc_args": "[\"john\", \"Doe\", 137]",
"icon": "TheIcon",
"body_loc_key": "some_key",
"sound": "somesound",
"color": "Blue",
"collapse_key": "somekey",
}

In general you don't try. Arabic numerals (the normal 0-9) are well understood. If you want to insist on trying, you can do it client side with a string replace. Or server side by posting your locale to it and letting the server translate before sending down.

To solve the issue, I ended up creating just a helper class with a method to handle the conversion. I run the message text through it and it returns the translated values. It has worked well for me so far.
public static class NumberHelper
{
public static string ConvertToArabicNumbers(this string input)
{
return input.Replace('0', '\u0660')
.Replace('1', '\u0661')
.Replace('2', '\u0662')
.Replace('3', '\u0663')
.Replace('4', '\u0664')
.Replace('5', '\u0665')
.Replace('6', '\u0666')
.Replace('7', '\u0667')
.Replace('8', '\u0668')
.Replace('9', '\u0669')
.Replace('.', '\u066B');
}
}

Related

Firebase: Check for changes in JSON and automatically send notification

UPDATE:
Ok so from all the comments I got convinced that using Firebase is the best solution for a number of reasons but how would I check for that change (canceled == yes) in my json file from firebase and automatically send the notification if the course is canceled.
Sample JSON response:
{
"class" : "Class",
"time" : "00:00",
"trainer" : "T",
"canceled" : ""
},
{
"class" : "Class",
"time" : "00:00",
"trainer" : "T",
"canceled" : ""
}
INITIAL QUESTION
I am parsing a json api and one of the fields is called canceled.
I would like my app to set a notification whenever that canceled key has a value of yes and so far it's easy, just create a simple notification like in the android developer topic and if canceled == yes update the Builder and then notify();.
The issue here is how can I keep checking for changes in the json after the app has been closed and set the notification ?
I have access to the server and make changes to the api if required.
Can I do this without the help of any third party services?
As #Sujal mentioned With Android O you have restriction on running background service for a long time. So It's better move this thing to server side. Whenever there is a change in the status i.e. Whenever key canceled becomes yes send a notification to android device which would show the Notification.
Ref : https://firebase.google.com/docs/cloud-messaging/
Ref : https://www.codementor.io/flame3/send-push-notifications-to-android-with-firebase-du10860kb

Twilio SMS body doesn't concatenate URL parameters in Azure Logic App

I'm currently building a logic app in Azure to send text messages with Twilio. Everything is working smoothly except that I can't send complex URL in the body.
For exemple if I send this : https://example.com?id=26 I will recieve the the correct string, but the parameter is not concatenated correctly with the domain in the clickable link.
So when I click the link in the text message it only opens the https://example.com . The message I'm sending is built like this :
"Send_Text_Message_(SMS)": {
"inputs": {
"body": {
"body": "#{triggerBody()?['message']}#{triggerBody()?['url']}#{body('Insert_row')?['Id']}",
"from": "xxx-xxx-xxxx",
"to": "#triggerBody()?['phone']"
},
message: Hello please click this link
url :https://example.com?id=
id: 26
so the final body would be like : [message][url][id]
I've tried sending a simple string like https://example.com?id=8 to see if it was the "complex" body concatenation that was failing, but it's still not working.
Can anyone help :) ?
For anyone that would be stuck with this here's how I tricked android :
I added a / after the .com so now the url I'm sending is :
https://example.com/?id=36

How to deliver messages atomically when using Firebase without a backend logic

I´m creating this chat app using Firebase. I wanted to create a system
where every device having the chat app installed is responsible for delivering chat messages.
Even if the device did not send the message it plays a part in the pool of workers, that deliver chat messages.
Now for a normal chat app the device that creates the chat message can easily send the message using E.g. OneSignal.
In my case user can chat with abstract things and there can be many people listening for new chat messages. One is sending and 1000+ is listening.
Here is where the problem comes. If one device is responsible for notifying 1000+ other it can get really heavy on the load. I was thinking all other devices can help out with the delivery,
so I´m trying this Firebase database notification entry:
{
"NOTIFICATIONS" : {
"-Kg4_cnR9T8Efx77rL2n" : {
"targetId" : "-KfpVVenyQccKqAxxrvE",
"text1" : "There´s a message for you",
"text2" : "Pluto",
"text3" : "",
"time" : 1490447404504,
"type" : "chatMessage"
},
"-Kg4_eWQFZndhztPqSTp" : {
"targetId" : "-KfpWz7ZWBAa_8pLM7tI",
"text1" : "There´s a message for you",
"text2" : "Saturnus",
"text3" : "",
"time" : 1490447411536,
"type" : "chatMessage"
}
}
}
and when one device is creating a message it post this new NOTIFICATIONS that will be picked up by all devices that are listening.
Now since all devices is listening on the NOTIFICATIONS and will grab one NOTIFICATIONS and send the chat message heads up, to all other devices that are registered on the targetId the problem obviously arise.
Here are the TARGET_VISITORS. This are the users that are registers for new chat messages.
{
"TARGET_VISITORS" : {
"-KfpVVenyQccKqAxxrvE" : {
"HnhLyXRxUINmlltK3jdsfxx2QBYiQ53" : {
"notify" : true,
"time" : 1490300590623,
"userFirebaseId" : "HnhLyXRxUINmlltK3jdsfxx2QBYiQ53"
}
},
"-KfpWz7ZWBAa_8pLM7tI" : {
"HnhLrryXUINmlltK3jdsfxx2QBYi3455" : {
"notify" : true,
"time" : 1490300581677,
"userFirebaseId" : "HnhLrryXUINmlltK3jdsfxx2QBYi3455"
}
}
Can I use the Firebase Transaction for this?
Like one device pick up a NOTIFICATIONS and then gets maybe 10 TARGET_VISITORS (there can be 1000+), and in a Firebase transaction locking down TARGET_VISITORS to perform the chat message heads up delivery on his userFirebaseId or OneSignal PlayerId.?
After that the TARGET_VISITORS can save the NOTIFICATIONS id and by doing that prevent getting duplicate messages.
Maybe creating a visitor lock rule like:
"TARGET_VISITORS_LOCK": {
".read": "auth != null",
"$FirebaseId": {
// This need to be a final entity.
// we can write as long as old data or new data does not exist.
// In other words, we can write if we're deleting or creating data, but not updating data.
".write": "!data.exists() || !newData.exists() && auth != null"
}
},
And then run an Data Fan Out updateChildren on the TARGET_VISITORS_LOCK, if it fails it means some other device is running the updateChildren before me and sending the notification to the single device. What will happen her if device having an offline period?
Many devices in the pool of workers can have the same NOTIFICATIONS to try to send and they will fight over TARGET_VISITORS, so to speak
Then comes the problem to know when to remove a NOTIFICATIONS entry, remove it when all TARGET_VISITORS have been notified :) Yea this is quite fun :)
It´s all theoretical at this point and i´m reading a loot so hoping for some input about this?

Android push notification error in Corona

Even by using sample project provided in Corona SDk , I get a notification with error 400. I guess my json data is correct. Following is the code for Json message.
local jsonMessage =
[[
"registration_ids": ["]] .. tostring(googleRegistrationId) .. [["],
"data":
{
"alert": "Hello World!",
"sound": "default"
}
}
]]
This is the message on my device.
Based on the 400 error code, the problem must be your JSON :
400
Only applies for JSON requests. Indicates that the request could not
be parsed as JSON, or it contained invalid fields (for instance,
passing a string where a number was expected). The exact failure
reason is described in the response and the problem should be
addressed before the request can be retried.
With all the square brackets and html tag, it's really hard to understand from your question how your JSON actually looks like.
Anyway, here's how it should look like :
{
"registration_ids": ["some reg id"],
"data":
{
"alert": "Hello World!",
"sound": "default"
}
}
I solved this error by changing the format of my json and checking the format on this link http://jsonlint.com/# . This was a great help and also I replace alert icon by custom icon by using this:
http://developer.android.com/guide/practices/ui_guidelines/icon_design_status_bar.html

sendMultipartTextMessage sends garbled messages

I'm writing an Android application that allows a call and response between two devices. I'm currently using sendMultipartTextMessage to send a message longer than 160 characters. However the message received is not the message I sent.
String response = "abcd abcd abcd abcdabcd abcd abcd abcd abcd...to 300 chars";
Log.i("response",response);
SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(response);
for (String part : parts) {
Log.i("part",part);
}
sms.sendTextMessage(sender, null, "This should be normal", null, null);
sms.sendMultipartTextMessage(sender, null, parts, null, null);
The Log.i runs as expected and outputs the message in parts. The sendTextMessage also sends the correct SMS to my partner emulator. However, the parts sent by sendMultipartTextMessage all come back oddly translated. For example the above response would be received as "BEGIABEGIABEGIABEGIA..." and so on with a few minor variations for spaces and numbers.
What is causing sendMultipartTextMessage to garble the SMS?
Its a bug in the platform: see http://code.google.com/p/android/issues/detail?id=13737. It might be limited to the simulator, so try it on a real device.
sendMultipartTextMessage method does not work correctly when you run your android application from any simulator.
so you need to test your android application on actual android smart phone. one more thing to consider is always pass null as third second argument (string containing phone number of sender) while sending SMS otherwise code will not sms.
i hope this will help you.

Categories

Resources