Is there a standard default simple push message JSON for Android? - android

I have written a program to send a simple text message as push to both iOS and Android devices. However, I have been told that I should not bother the users to write their own JSON. So all I ask from the user is to write the body of the message.
In iOS this will be assigned to the alert attribute in their JSON. But I see on Android that you can have different variables. So I am looking for the attribute that will always work as the message text. This is my Android message JSON:
{
'title':'no title',
'body':'This is the text message!'
}
But I am wondering if body is a standard tag I can use with any app? Or will it become unavailable if the users connects my program to another app?

This format should work for both iOS and Android:
{
"alert": "My_Alert_Text",
"title": "My_Alert_Title",
"myKey1": "Custom_Value_1",
"myKey2": "Custom_Value_2",
....
"myKeyN": "Custom_Value_N"
}

Related

How to send a notification to specific platform?

I want to send two different notifications to Android and iOS. I want to send a notification message to iOS, this way iOS displays a nice notification. For Android I want to send a data message, this way I can handle the notification on Android and also in the background (because I don't get a callback in the background and want to handle it myself).
I looked through the docs, but couldn't find anything about sending to a specific platform. How do I do that?
Also other suggestions on how to do this are welcome, but keep in mind that I specifically want to handle the notification myself through a callback on Android (onMessageReceived)
Update: A recent feature was added for FCM that gives an option to provide specific params for specific platforms, called Platform Overrides:
Customizing a message across platforms
Messages sent by the FCM v1 HTTP protocol can contain two types of JSON key pairs:
a common set of keys to be interpreted by all app instances that receive the message.
platform-specific blocks of keys interpreted only by app instances running on the specified platform.
Platform-specific blocks give you flexibility to customize messages for different platforms to ensure that they are handled correctly when received. In many scenarios, it makes sense to use both common keys and platform-specific keys in a given message.
When to use common keys
Whenever you're targeting app instances on all platforms — iOS, Android, and web
When you are sending messages to topics
The common keys that are interpreted by all app instances regardless of platform are message.notification.title, message.notification.body, and message.data.
When to use platform-specific keys
When you want to send fields only to particular platforms
To send platform-specific fields in addition to the common keys
Whenever you want to send values to specific platforms only, don't use common keys; use platform-specific key blocks. For example, to send a notification to only iOS and web but not Android, you must use two separate blocks of keys, one for iOS and one for web.
When you are sending messages with specific delivery options, use platform-specific keys to set them. You can specify different values per platform if you want; but even when you want to set essentially the same value across platforms, you must use platform-specific keys. This is because each platform may interpret the value slightly differently — for example, time-to-live is set on Android as an expiration time in seconds, while on iOS it is set as an expiration date.
Example: notification message with platform-specific delivery options
The following v1 send request sends a common notification title and content to all platforms, but also sends some platform-specific overrides. Specifically, the request:
sets a long time-to-live for Android and Web platforms, while setting the APNs (iOS) message priority to a low setting
sets the appropriate keys to define the result of a user tap on the notification on Android and iOS — click_action, and category, respectively.
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Match update",
"body":"Arsenal goal in added time, score is now 3-0"
},
"android":{
"ttl":"86400s",
"notification"{
"click_action":"OPEN_ACTIVITY_1"
}
},
"apns": {
"headers": {
"apns-priority": "5",
},
"payload": {
"aps": {
"category": "NEW_MESSAGE_CATEGORY"
}
}
},
"webpush":{
"headers":{
"TTL":"86400"
}
}
}
}
See the HTTP v1 reference documentation for complete detail on the keys available in platform-specific blocks in the message body. For more information about building send requests that contain the message body, see Build Send Requests.
I remember answering a similar question before but cant seem to find it. There is currently no option to specify which platform a message would be sent. The simplest way you could do this is by using Topics Messaging.
Everytime the token is generated for the first time, you determine from your client app the Platform type and subscribe them to the corresponding topic (e.g. topics/(Android/iOS)_<Your App Name>), then sned the messages as needed.
It's also good to keep track of the registration tokens from your Server, if you're using Firebase DB, you could put them inside a node:
/pushTokens
/android
/{userId} : string
/ios
/{userid}: string
This would let you check from your backend side and adjust your payload as needed when sending single messages.
I was facing same issue with Android Development. We used Laravel on backend side.
On backend side, developer has to maintain different scenario for both platforms.
In Android :
FCM::sendTo($tokens, $option, null, $data);
Here, null is passed as a notification builder. When you pass null in notification and create databuilder($data), you can receive message in onMessageReceived method when app in background.
For iOS :
FCM::sendTo($tokens, $option, $notification, $data);
Here, you can pass notification builder in $notification. So, iOS device can also get notification.

Is aps dictionary necessary in iOS notification payload? Can we have same payload for iOS and Android?

I just started working on notifications on iOS and it seems that apple has defined a format for payload to receive notifications.
So currently, I am using this payload and everything is working as expected. I am getting title, subtitle, body sound, image.
{"aps" : {
"alert" : {
"title" : "Introduction To Notification",
"subtitle" : "Session 707",
"body" : "New Notification Look Amazing"
},
"sound" : "default",
"category" : "message",
"badge" : 1,
"mutable-content": 1
},
"attachment-url": "https://pusher.com/static_logos/320x320.png"
}
Lets say I want to have a single payload for both Android and iOS.
Is there a standard format defined in Android for notifications or can you set any data in Android and the client has to manually handle and display these notifications?
How can I create a payload which works for both?
Update for cross-platform payloads: A recent feature was added for FCM that gives an option to provide specific params for specific platforms, called Platform Overrides.
The sample payload you posted seems to be in-line with the official parameters for APNs. When using GCM or FCM, the parameters to be used are different (see the links).
Is there a standard format defined in Android for notifications or can you set any data in Android and the client has to manually handle and display these notifications?
It depends on which type of message payload you're planning to use. There are 2 types of Messages for GCM/FCM, notification and data.
notification messages only have predefined set of parameters available, while data messages can be used to have custom key-value pairs. Both are usually handled by the client, but note that the behavior for Android and iOS are different depending on the message type you use (see the links).
How can I create a payload which works for both?
As I mentioned in comments section in the other post:
You'll have to do the mapping in your own database/app server. Yes. What I was thinking here was every time a registration token is generated on the client app side, you send it to your database/app server along the type of device (i.e. "Android", "iOS"). So that when you'll be sending messages, you'll first have to check the type of device. I did say it's more work, but it's a sure way to give you control over things. AFAIK, it is the developer's responsibility to keep track of the registration tokens and any details that should be associated with it.
You are not allowed to put custom tags inside aps tag. Here's what documentations says about it:
Providers can specify custom payload values outside the Apple-reserved aps namespace. Custom values must use the JSON structured and primitive types: dictionary (object), array, string, number, and Boolean.
So in your case you should do something like:
{
"aps": {
"alert": "Hello World",
"sound": "default"
},
"Person": {
"Address": "Your address",
"Name": "Your Name",
"Number": "XXXXXXXXXX"
}
}
Therefore you can read your custom payload with looking for it's key in main JSON, rather than in "aps":

Worklight: save notifications in jsonStore when notification been received by device

I am using worklight for my hybrid application. I have implemented push notification which is notifying to my device. I want to pass notification to jsonstore when its been received by my device.
I am using this piece of code.
WL.Client.Push.onMessage = function (props, payload) {
//jsonStore code
//end of jsonStore code
WL.SimpleDialog.show("FMB Notification", "Provider notification data: " + JSON.stringify(props), [ {
text : 'Close',
handler : function() {
WL.SimpleDialog.show("FMB Notification", "Application notification data: " + JSON.stringify(payload), [ {
text : 'Close',
handler : function() {}
}]);
}
}]);
};
Also I am not able to receive notification when app is open.
Thanks.
If you are not able to receive notifications, why are you asking about storing them? I would imagine you will want to fix the latter in order to achieve the former... and that said - don't ask two questions in 1 question.
You do not explain what is this app that you are unable to receive push with when the app is opened. Is it iOS or Android or something else? Is it event source notifications or tag notifications? Are you certain it is properly configured? What is your MFPF version, build number? Did you follow the sample app? Is that working for you, but yours doesn't? Where is your full implementation?
Since you failed to provide any meaningful information, my suggestion to you is to take the sample application, configure it and see that you are able to receive notifications "when the app is open".
Once you get that, you can take the JSONStore sample, which demonstrates initializing a storing and saving data in it.
From there on, the road to combining the existing API calls from one sample into anothr in order to save the notification's payload value is clear.

Apigee push notifications on Android not delivered when app in background (Phonegap)

I am trying to use apigee push notifications with Android phonegap app build 3.3.0, PushPlugin.
My push notifications sent when the app is in foreground are displayed correctly, but when the app is in background they are not shown.
It looks like the issue is that apigee sends a push with payload that contains "data" property (e.payload.data), instead of "message" property, and since there is no "message" property and that's why android does not display it.
Can you please advise if it is possible to change the payload format of apigee push messages or make the phonegap app to handle "data" in the payload correctly?
Thanks!
I'm not sure how flexible the phone gap plugin is regarding background processing, but yes, you may provide any payload you'd like when you create the message to push to your devices.
If you look Apigee's Creating and managing notifications document, you'll see in each example, there is a that can specified. If it is a JSON object (instead of a string), it will be delivered "as is" to the client. In other words, you could specify during notification creation that the for your notifier is something like:
{ "message" : "my message" }.
Hope that helps.

Correctly formatting GCM notifications?

I'm currently trying out the google cloud messaging service with its sample application "Guestbook." https://developers.google.com/cloud/samples/mbs/
I'm attempting to send notifications that should appear as a simple "hello" toast on screen for about 5 seconds.Doing this from the project settings page (pictured here)works.Perhaps i've misinterpreted what this for.
However my attempts to do it manually via fiddler2 cause the guestbook client application to crash instead of making the intended toast flash up onscreen.
Here is an example of the POST request i used to attempt a push notification
I have also done other variations of the above where i have included "topicId:_broadcast" to no avail.
Is there something in the formatting that i have wrong? or missing information i should have included.
You got the format a bit wrong. It should be :
{
"registration_ids":["xxx", "yyy"],
"data": {
"message": "test",
"duration": "5"
}
}

Categories

Resources