Custom Android Notifications

Hi everyone, not sure if this was covered already, I searched and didn’t find anything. I’m looking for more information around customizing the notification when a GCM push notification comes in. As an example, apps like gmail or groupon have custom dropdowns when the notification is viewed in the status dropdown like reply, or show an image in the background etc. Is this possible to be done with Corona, I currently don’t see how to do it.

Bump, is this in the works or would it require the Enterprise?

Hello David,

Perhaps this thread will point you in the right direction…

http://forums.coronalabs.com/topic/29950-notification-icon-on-android/

Brent Sorrentino

Hi Brent, thanks for replying but this isn’t what I was looking for. I was asking how to modify the notification, specifically, the text and title of the alert, for example see this: http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomExpandedView

My issue is that text I want to display is getting cutoff, is there a way to expand this and customize as in the above article?

We currently have a secret/experimental API that allows you to set the individual notification’s title, text, and badge number separately.  It’s not documented and we reserve the right to change it in the future (hence why I call it experimental), but you’re welcome to give it a go.
 
So, instead of setting the notification’s “alert” property to a string like this…

"data": { "alert": "Hello World!" }

 
You can set the individual notification properties like this instead…

"data": { "alert": { "title": "My Title", "body": "My body of text.", "number": 5 } }

 
You can do the same via a local notification on Android as well…

local settings = { alert = { title = "My Title", body = "This is my body text.", number = 5 } } system.scheduleNotification(0, settings)

 
That’s as much as you can do in Corona at the moment.  We don’t support all of Android status bar notification features (which is extensive and very powerful) such as custom views because there isn’t a cross-platform equivalent on iOS.  If you want to go to that level, then unfortunately, your only option is to write it in Java yourself via Corona Enterprise… which I’ll tell is you going to be a lot of work.  Especially if you want to support Android OS 2.x and above, because Android notification APIs have changed between Android 2.x, 3.x, and 4.x.
 
In any case, I hope this helps.

I’m coming in from PHP, but the alert value i’m sending to GCM doesn’t seem to be coming through: 

$fields = array(

        ‘registration_ids’ => $registrationIDs,

        ‘data’ => array( “alert” => array(“title”=>“Custom content title”,“body”=>$message,“number”=>3) )

    );

json encoded it looks like:

“data”:{“alert”:{“title”:“Custom content title”,“body”:“custom body message”,“number”:3}}

In the device, the body is coming through as {“body”:“custom body message”,“title”:“Custom content title”,“number”:3}

What release does that build to?

Are you including the “registration_ids” in your JSON packet?

Are you also passing your Google API key via the HTTP header?

Because my JSON example up above is only a partial example, showing you only the “data” part of the JSON table.

I suggest that you test with our sample app “Notifications/GooglePushNotifications” project first, which comes with the Corona SDK.  That app pushes a notification to itself via Corona’s network.request() API when you tap on the screen.  Have a look at the “main.lua” file’s onTap() function for an example on how to set up the JSON packet.  It’s easier to test with that sample app because it removes an extra level of complexity from the equation (your server and PHP).  Once you’re able to successfully push a notification with an alert that your satisfied with, then attempt to do so via your server.

Anyways, that’s my 2 cents.  I hope this helps!

It’s actually part of the overall data, I just didn’t want to expose it. Yes I am sending API key etc, which I can successfully get the notification to show up on the device if I simply send $alert=“string value message”. I’m making a curl request and json encoding the array as part of the request. I pulled the example from elsewhere. 

Is there a specific daily build of Corona that this is supported in? Just curious if that may be part of the issue?

I’ve just tried pushing an “alert” table via GCM and I’m having a problem with it too.  It turns out that Google flattens the “alert” JSON table into a single string, tricking Corona’s receiver into thinking its just a single alert message.  Bummer.  So, unfortunately there is no way to do this at the moment.  Well, I did say it was experimental.  It’s only been tested with local/scheduled notifications, which works on that end.  We’ll have to redesign this in the future to work-around these behaviors.

Bump, is this in the works or would it require the Enterprise?

Hello David,

Perhaps this thread will point you in the right direction…

http://forums.coronalabs.com/topic/29950-notification-icon-on-android/

Brent Sorrentino

Hi Brent, thanks for replying but this isn’t what I was looking for. I was asking how to modify the notification, specifically, the text and title of the alert, for example see this: http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomExpandedView

My issue is that text I want to display is getting cutoff, is there a way to expand this and customize as in the above article?

We currently have a secret/experimental API that allows you to set the individual notification’s title, text, and badge number separately.  It’s not documented and we reserve the right to change it in the future (hence why I call it experimental), but you’re welcome to give it a go.
 
So, instead of setting the notification’s “alert” property to a string like this…

"data": { "alert": "Hello World!" }

 
You can set the individual notification properties like this instead…

"data": { "alert": { "title": "My Title", "body": "My body of text.", "number": 5 } }

 
You can do the same via a local notification on Android as well…

local settings = { alert = { title = "My Title", body = "This is my body text.", number = 5 } } system.scheduleNotification(0, settings)

 
That’s as much as you can do in Corona at the moment.  We don’t support all of Android status bar notification features (which is extensive and very powerful) such as custom views because there isn’t a cross-platform equivalent on iOS.  If you want to go to that level, then unfortunately, your only option is to write it in Java yourself via Corona Enterprise… which I’ll tell is you going to be a lot of work.  Especially if you want to support Android OS 2.x and above, because Android notification APIs have changed between Android 2.x, 3.x, and 4.x.
 
In any case, I hope this helps.

I’m coming in from PHP, but the alert value i’m sending to GCM doesn’t seem to be coming through: 

$fields = array(

        ‘registration_ids’ => $registrationIDs,

        ‘data’ => array( “alert” => array(“title”=>“Custom content title”,“body”=>$message,“number”=>3) )

    );

json encoded it looks like:

“data”:{“alert”:{“title”:“Custom content title”,“body”:“custom body message”,“number”:3}}

In the device, the body is coming through as {“body”:“custom body message”,“title”:“Custom content title”,“number”:3}

What release does that build to?

Are you including the “registration_ids” in your JSON packet?

Are you also passing your Google API key via the HTTP header?

Because my JSON example up above is only a partial example, showing you only the “data” part of the JSON table.

I suggest that you test with our sample app “Notifications/GooglePushNotifications” project first, which comes with the Corona SDK.  That app pushes a notification to itself via Corona’s network.request() API when you tap on the screen.  Have a look at the “main.lua” file’s onTap() function for an example on how to set up the JSON packet.  It’s easier to test with that sample app because it removes an extra level of complexity from the equation (your server and PHP).  Once you’re able to successfully push a notification with an alert that your satisfied with, then attempt to do so via your server.

Anyways, that’s my 2 cents.  I hope this helps!

It’s actually part of the overall data, I just didn’t want to expose it. Yes I am sending API key etc, which I can successfully get the notification to show up on the device if I simply send $alert=“string value message”. I’m making a curl request and json encoding the array as part of the request. I pulled the example from elsewhere. 

Is there a specific daily build of Corona that this is supported in? Just curious if that may be part of the issue?

I’ve just tried pushing an “alert” table via GCM and I’m having a problem with it too.  It turns out that Google flattens the “alert” JSON table into a single string, tricking Corona’s receiver into thinking its just a single alert message.  Bummer.  So, unfortunately there is no way to do this at the moment.  Well, I did say it was experimental.  It’s only been tested with local/scheduled notifications, which works on that end.  We’ll have to redesign this in the future to work-around these behaviors.

How to add custom icon and big picture in this json data

>> How to add custom icon and big picture in this json data

You can’t.  You can only set 1 custom icon for all notifications, which we document here…

   https://docs.coronalabs.com/guide/events/appNotification/index.html#android-icons

How to add custom icon and big picture in this json data