Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Android Notifications Not Working


This question is answered. Helpful answers available: 0. Correct answers available: 1.


Permlink Replies: 11 - Last Post: Jun 30, 2017 9:46 AM Last Post By: Remy Lebeau (Te...
Joe Fermani

Posts: 10
Registered: 2/4/13
Android Notifications Not Working  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 28, 2017 5:38 AM
I am currently using Delphi 10 Seattle edition. I have an app written and deployed for both iOS and Android. The app supports push notifications. While the app is running, it can receive push notifications without any problems for both platforms. All the data comes in correctly. I have an event which gets triggered for receiving the data and it works fine and my logging confirms all data is there.

procedure TMobilityPushService.OnReceiveNotification(Sender: TObject; const ANotification: TPushServiceNotification);
var
    Data: string;
 
begin
    Data := Format('Received push notification. Data Key: (%s)  JSON: (%s)', [aNotification.DataKey, aNotification.Json.ToString]);
    Logger.Log(Data, Log_Normal);


My problem is on Android when the app is NOT running. The device gets the notification but it only displays the App icon and the name of the app. It does not display any of the text nor the title of the message in the notification. When I try the same thing on iOS, the iPhone displays the message correctly. Users can then tap the message and the App loads so the user can log in. Users can also tap the message in Android and it brings up the app as well. The only issue on Android is that the message text is not displaying.

When I setup my notifications on Google, Google's directions are to use the new end point. Messages are getting sent to https://fcm.googleapis.com/fcm/send
In my Android manifest template I added the following line for notifications:

<service android:name="com.embarcadero.gcm.notifications.GCMIntentService" />


When I compile the app Delphi adds a receiver section to my Android manifest:
<receiver android:name="com.embarcadero.rtl.notifications.NotificationAlarm" />
<receiver android:exported="true" android:name="com.embarcadero.gcm.notifications.GCMNotification" android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="**************" /> // My specific app name is here
</intent-filter>
</receiver>


I have written my own push server for sending notifications and its working fine for both platforms with the exception of the problem above. Thinking my server had the problem, I logged into my Google account, and used Google's test Notification on their web page. I get the same results using the Google Notification.

My code to send the Notification is as follows from my server:
    HttpClient.Request.ContentType := 'application/json';
    HttpClient.Request.CustomHeaders.Add('Authorization: key = ' + MyAuthKey); // Server Key from Google
    HttpClient.Request.CharSet := 'utf-8';
 
    JSONMsg := TJSONObject.Create;
    JSONMsg.AddPair('to', devicetoken);
 
    JSONInfo := TJSONObject.Create;
    JSONInfo.AddPair('body', edtMessage.Text);
    JSONInfo.AddPair('title', MyMessageTitle);
    JSONInfo.AddPair('priority', 'high');
    JSONTrue := TJsonTrue.Create;
    JSONFalse := TJsonFalse.Create;
    JSONInfo.AddPair('dry_run', JSONFalse);
 
    JSonMsg.AddPair('notification', JSonInfo);
    StatusMemo.Lines.Add('Message to send: ' + JSONMsg.ToString);
 
    JSONToSend := TStringStream.Create(JSONMsg.ToString, TEncoding.UTF8);
 
    StatusMemo.Lines.Add('Sending Android message to device: ' + deviceToken);
    try
        Response := HttpClient.Post('https://fcm.googleapis.com/fcm/send', JSONToSend);
    except
        on E:Exception do begin
            StatusMemo.Lines.Add('Message send failed: ' + E.Message);
        end;
    end;


At this point, I am at a loss as to why Android does not display the message body or title, if the app is not running. When the app is running, the app gets the full message and I can see in the JSON all the information. If I send a notification from the APP to the device, the phone will properly display the message.

In the app I added the following code to see if the App can send a message to the phone:
    MyNotice := fNotification.CreateNotification;
    if (MyNotice = nil) then Exit;
 
    try
        MyNotice.Name := 'AlertNotification';
        MyNotice.Title := anAlert.Title;
        MyNotice.AlertBody := anAlert.MobileMessage;
        MyNotice.AlertAction := 'Mobility - Alert';
        MyNotice.Number := aNumber;
        MyNotice.FireDate := now;
        fNotification.PresentNotification(MyNotice);
    finally
        FreeAndNil(MyNotice);
    end;
end;


This code works fine and the notification displays fine on the phone. Only if I receive a notification and the app is closed, I get a blank notification.

Any idea?

Thanks for the help.... I am really stuck as to what the problem may be.
loki loki

Posts: 787
Registered: 7/1/02
Re: Android Notifications Not Working
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 28, 2017 6:11 AM   in response to: Joe Fermani in response to: Joe Fermani
Francesco Marano

Posts: 59
Registered: 11/18/02
Re: Android Notifications Not Working
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 28, 2017 6:12 AM   in response to: Joe Fermani in response to: Joe Fermani
Any idea?

Hello,

Could this be useful ?
The developer says there may be an issue in play services version 8.4.0.
https://stackoverflow.com/questions/35059614/android-notification-is-not-showing-its-content-when-app-is-not-running

Regards,
Francesco
Joe Fermani

Posts: 10
Registered: 2/4/13
Re: Android Notifications Not Working  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 28, 2017 1:42 PM   in response to: Joe Fermani in response to: Joe Fermani
I have found my problem! It appears to be a bug with Delphi. My original message sent was:
{"to":<MyDeviceID>","notification":{"body":"It finally works!!!!!","title":"CODY Mobility Title","priority":"high","content_available":false,"dry_run":false}}


If I change my message to the following, it now works:
{"to":"<MyDeviceID>","data":{"message":"It finally works!!!!!","title":"CODY Mobility Title","priority":"high","content_available":false,"dry_run":false}}


I had to change from "notification" to "Data" and I had to change the "body" element to a "message" element. With those changes in place, the phone gets and displays the message when the app is closed. Of course, this no longer matches Google's specs for sending a notification with Firebase Cloud Messaging. I dug into Delphi code and I found a Delphi java script called NotificationPublisher.Java. In there, there is code which specifically looks for a message element.
 if (jsonVal != null) {
if (jsonVal.has("message"))
{ msg = jsonVal.optString("message"); }
 
else if (jsonVal.has("msg"))
{ msg = jsonVal.optString("msg"); }
 
else if (jsonVal.has("alert"))
{ msg = jsonVal.optString("alert"); }
 
if (jsonVal.has("title"))
{ title = jsonVal.optString("title"); }
 
}
} else {
// Look for msg or message in bundle
if (key.equals("message"))
{ msg = valstr; } else if (key.equals("msg")) { msg = valstr; }
 
if (key.equals("title"))
{ title = valstr; }
}


I am not sure where Delphi checks for Notification elements versus Data elements. But I believe this part of the problem. I created a support ticket so Embarcadero can look into this further. If anyone else has this problem... Just change your message and it'll work.
Markus Humm

Posts: 5,113
Registered: 11/9/03
Re: Android Notifications Not Working  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 28, 2017 2:41 PM   in response to: Joe Fermani in response to: Joe Fermani
Am 28.06.2017 um 22:42 schrieb Joe Fermani:


I am not sure where Delphi checks for Notification elements
versus Data elements.> But I believe this part of the problem. I created a support ticket
so Embarcadero can look into this further.

Hello,

it's nice you created a ticket, but it would be helpful if you posted
the number of it here as well so that those interested in mit can follow it.

Greetings

Markus
Joe Fermani

Posts: 10
Registered: 2/4/13
Re: Android Notifications Not Working  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 29, 2017 4:27 AM   in response to: Markus Humm in response to: Markus Humm
markus.humm wrote:

it's nice you created a ticket, but it would be helpful if you posted
the number of it here as well so that those interested in mit can follow it.


Not a problem. Its: RSP-18499

Edited by: Joe Fermani on Jun 29, 2017 6:08 AM

Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: Android Notifications Not Working  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 29, 2017 6:06 AM   in response to: Joe Fermani in response to: Joe Fermani
Joe Fermani wrote:

it's nice you created a ticket, but it would be helpful if you posted
the number of it here as well so that those interested in it can
follow it.   


Not a problem. Its: RSP-18499

Please don't put quotes in {code} tags. Just quote them as given by the
editor.

--
Rudy Velthuis http://www.rvelthuis.de

"A man loses his illusions first, his teeth second, and his
follies last." -- Helen Rowland
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Android Notifications Not Working  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 29, 2017 5:22 PM   in response to: Rudy Velthuis (... in response to: Rudy Velthuis (...
Rudy Velthuis (TeamB) wrote:

Please don't put quotes in {code} tags. Just quote them as given by
the editor.

Or use {quote} instead.

--
Remy Lebeau (TeamB)
Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: Android Notifications Not Working  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 29, 2017 5:27 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:

Rudy Velthuis (TeamB) wrote:

Please don't put quotes in
 tags. Just quote them as given by
the editor.</div>
 
Or use {quote} instead.</div>
 
If I look at the "Markup" panel in the web editor, I don't see that as
valid markup. Only 
.

Quotes are marked by >, and that is automatically done when you reply.

--
Rudy Velthuis http://www.rvelthuis.de

Lady Astor: "Mr. Churchill, you're drunk!"
Winston Churchill: "Yes, and you, Madam, are ugly. But tomorrow,
I shall be sober."
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Android Notifications Not Working  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 29, 2017 5:44 PM   in response to: Rudy Velthuis (... in response to: Rudy Velthuis (...
Rudy Velthuis (TeamB) wrote:

If I look at the "Markup" panel in the web editor, I don't see that as
valid markup. Only
.</div>
 
And yet, <div class="jive-quote"> is still supported, and even used by the web editor,
too.
 
<div class="jive-quote">Quotes are marked by ></div>
 
Sure, typically.  And you can also wrap quotes in </div> too, just
like code can be wrapped in 
. Try it, it works.

--
Remy Lebeau (TeamB)
Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: Android Notifications Not Working  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 29, 2017 11:14 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:

Rudy Velthuis (TeamB) wrote:

If I look at the "Markup" panel in the web editor, I don't see that
as valid markup. Only
.</div>
 
And yet, <div class="jive-quote"> is still supported, and even used by the web editor,
too.
 
<div class="jive-quote">Quotes are marked by ></div>
 
Sure, typically.  And you can also wrap quotes in </div> too, just
like code can be wrapped in 
. Try it, it works.

This should appear as a quote.

But not in a NNTP newsreader?
--
Rudy Velthuis http://www.rvelthuis.de

"Acceptance without proof is the fundamental characteristic of
Western religion, rejection without proof is the fundamental
characteristic of Western science."
-- Gary Zukav
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Android Notifications Not Working  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 30, 2017 9:46 AM   in response to: Rudy Velthuis (... in response to: Rudy Velthuis (...
Rudy Velthuis (TeamB) wrote:

This should appear as a quote.

But not in a NNTP newsreader?

Not in NNTP, no. But in the web interface, yes.

--
Remy Lebeau (TeamB)

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02