Jump to content
TazKy

Android Push Notifications Question

Recommended Posts

I have been researching the ability to use push notifications to alert the user with an app visual message.  Using a message at the bottom of the app is not really that helpful.  The problem is that I cannot set the text message to show.  You have to set it manually in the settings ("Show as pop-up") if you can find it.  Surely this can be done in code.

 

Any suggestions?

 

Here is an example.

 

procedure SetNotification(const aTitle, aMessage: string);
begin
var vNotifiCenter:= TNotificationCenter.Create(nil);
  try
    if vNotifiCenter.Supported then
    begin
      var vNotification := vNotifiCenter.CreateNotification;
      try
        vNotification.AlertBody := aMessage;
        vNotification.Title := aTitle;
        vNotification.EnableSound := true;
        vNotifiCenter.PresentNotification(vNotification);
      finally
        vNotification.Free;
      end;
    end;
  finally
    vNotifiCenter.Free;
  end;
end;

Share this post


Link to post
17 minutes ago, TazKy said:

push notifications

What you're showing is a local notification.

18 minutes ago, TazKy said:

The problem is that I cannot set the text message to show.  You have to set it manually in the settings ("Show as pop-up") if you can find it.

Not sure what you mean - is the text in AMessage not showing in the notification? Which platform(s) is this an issue on?

  • Thanks 1

Share this post


Link to post
27 minutes ago, TazKy said:

The problem is that I cannot set the text message to show. 

I dont know if understood, but you can:

  • in iOS only use:
    • LNotification.HasAction := true;
      LNotification.AlertAction := 'ButtonNameXXXX';  // ButtonNameXXXX would be "a button in your form" form, then, it would be "clicked"
  • or using "NotificationCenter1ReceiveLocalNotification(Sender: TObject; ANotification: TNotification);" event, then, when user click on notification you can to do something like:
    • procedure TForm1.NotificationCenter1ReceiveLocalNotification(Sender: TObject; ANotification: TNotification);
      begin
        if ANotification.Title = 'Hello there' then
          ShowMessage('Thanks for your click baby');
      end;

would be that?

Share this post


Link to post

Hi Dave,

 

This is Android.  Okay, if you say it is location notification then that is fine with me.  At any rate it is a notification. 

 

For one you have to allow the notification.  And when that occurs and you test it only a sound occurs.  And to me the sound means nothing.  What needs is the accompanying text message.  And the only way for that to occur is to set that manually in the app notifications settings.  What I am trying to do is set the ability to see the text message in code.

 

I hope this makes sense.

Share this post


Link to post

Hi programmerdelphi2k,

 

That ShowMessage is an awkward ugly looking white block with an OK button.  That is not a notification.  That is an ugly Delphi show message. 

Share this post


Link to post
13 minutes ago, TazKy said:

What needs is the accompanying text message

If you mean that the banner does not show, when an app is in the foreground, by default you do not see the banner. If you want the banner to show when the app is in the foreground you need to use a channel with the Importance set to High, and set the notifications ChannelId property, e.g. (extending your code):

procedure SetNotification(const aTitle, aMessage: string);
begin
var vNotifiCenter:= TNotificationCenter.Create(nil);
  try
    if vNotifiCenter.Supported then
    begin
      var LChannel := vNotifiCenter.CreateChannel('MyChannel', 'MyChannel', 'My Channel');
      try
        LChannel.Importance := TImportance.High;
        vNotifiCenter.CreateOrUpdateChannel(LChannel);
      finally
        LChannel.Free;
      end;
      var vNotification := vNotifiCenter.CreateNotification;
      try
        vNotification.AlertBody := aMessage;
        vNotification.Title := aTitle;
        vNotification.EnableSound := true;
        vNotification.ChannelId = 'MyChannel';
        vNotifiCenter.PresentNotification(vNotification);
      finally
        vNotification.Free;
      end;
    end;
  finally
    vNotifiCenter.Free;
  end;
end;

 

  • Thanks 1

Share this post


Link to post

No.  There is a sequence that you have to do to get the text message to show.

 

1. Open Settings

2. Open App Notifications.

3. Find your app.

4. Tap on Notification Categories

5. Tap on Default.

6. With Alert selected set Show As Pop-up to true.

 

Then when you test the notification it plays a sound then a small text message appears with the small notification image.  Looks pretty.

 

That tells me that there should be a way in code to bypass this manual settings.  You are able to set the EnableSound.  Why can you not set the visible message?

 

Share this post


Link to post

Dave, that did it.  Thank you very much.

 

I hate when someone dangles a carrot and leaves things out.  What good is a notificaton sound without an accompanying message?

Share this post


Link to post

Dave,

 

It appears that your code works fine in Alexandria, but not Tokyo.  There is no TChannel.

Share this post


Link to post
47 minutes ago, TazKy said:

It appears that your code works fine in Alexandria, but not Tokyo.  There is no TChannel.

Correct. It was introduced in Delphi 10.3 in order to support changes in Android. If you're doing mobile development with Delphi, it pays to stay current.

  • Thanks 1

Share this post


Link to post

Understood.  However, I have to support some old circa 2016 Zebra scanners (Android v4.4.4) devices which means Tokyo development environment.

 

Again, thanks.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×