Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FCM] click_action replacement for iOS #211

Closed
havrlisan opened this issue Feb 14, 2024 · 6 comments
Closed

[FCM] click_action replacement for iOS #211

havrlisan opened this issue Feb 14, 2024 · 6 comments
Assignees
Labels
enhancement New feature or request

Comments

@havrlisan
Copy link

There is currently no way on iOS to implement a custom action if the user clicks on a Firebase notification since the click_action key is unsupported on iOS. From what I've found, it is possible to bypass this by using the category key for iOS instead. By default, the category key is used to display a dialog to perform a quick action instead of opening the application. This way it'll be possible to implement a custom behavior inside Delphi (just as with click_action for Android), or even create and register a UNNotificationCategory (although it's probably not worth the hassle).

References:
Apple docs - Configuring Categories and Actionable Notifications
Firebase docs - Notification payload support
SO - just a confirmation other people have done it

@DelphiWorlds DelphiWorlds self-assigned this Feb 14, 2024
@DelphiWorlds DelphiWorlds added the enhancement New feature or request label Feb 14, 2024
@DelphiWorlds
Copy link
Owner

Just FYI - I started work on something like this not long ago. The first link you provided has made me change direction a bit. I will be implementing something :-)

@havrlisan
Copy link
Author

Just FYI - I started work on something like this not long ago. The first link you provided has made me change direction a bit. I will be implementing something :-)

Glad I could be of help!

@DelphiWorlds
Copy link
Owner

DelphiWorlds commented Jul 9, 2024

Added support for category (and action). The demo has been updated to illustrate how to add categories/actions, however to also illustrate here:

procedure TfrmMain.AddCategories;
var
  LCategory: INotificationCategory;
begin
  LCategory := FCM.AddCategory('category1', Category1Handler);
  LCategory := FCM.AddCategory('category2');
  LCategory.AddAction('action1', 'Confirm', Category2ConfirmHandler);
  LCategory.AddAction('action2', 'Deny', Category2DenyHandler);
end;

The first line of the routine is the simplest case - a category with no actions. When the user taps the notification, Category1Handler is called.
The rest of the routine demonstrates adding actions to a category. The action "buttons" are presented when the user swipes down on the notification itself (i.e. instead of tapping it), and tapping one of the action buttons will invoke the relevant handler:

image

NOTE: For this to function properly when the app is not running, or is in the background, you need to ensure that remote-notification is selected in the UIBackgroundModes property in Version Info in the Project Options:

image

Also, if using FCMSender to send messages from your server, the value of the ClickAction property of TFCMMessage will be included as the category in the aps member of the payload

@gmurt
Copy link
Sponsor

gmurt commented Jul 9, 2024

Hi Dave,

Great work! Looking forward to trying this out.

Just one thing, the way I would need to use it is to build the actions dynamically from a call to the server.

Having a handler method with no parameters makes it difficult to do this as we don't know which action was tapped.

If it could pass the action ID to the handler method, we could build the actions at runtime, use the same handler for all actions and then send the pressed action id back to the server for any required processing.

Hope this makes sense, let me know if not 👍

@DelphiWorlds
Copy link
Owner

Yes, it made sense. Let me know if the changes I just pushed suit you, which allows adding of actions optionally without a handler, and use OnNotificationCategory to handle categories and/or actions generically

@gmurt
Copy link
Sponsor

gmurt commented Jul 9, 2024

Yes, this is exactly what I meant, makes it much more flexible!

Thanks a lot Dave, will be trying this later today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants