DelphiWorlds / Kastri

Cross-platform library for Delphi
MIT License
489 stars 119 forks source link

[FCM] click_action replacement for iOS #211

Closed havrlisan closed 1 month ago

havrlisan commented 7 months ago

There is currently no way on iOS to implement a custom action if the user clicks on a Firebase notification since the _clickaction 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 _clickaction 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 commented 7 months ago

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 commented 7 months ago

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 commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

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

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