urbanairship/urbanairship-cordova

Read the 'actions' from a push

AE1NS opened this issue · 11 comments

AE1NS commented

Inside the listener for the event 'urbanairship.push' I get the notification as parameter. I can just read properties like 'title' and 'message' from it, but would it also be possible to read the 'actions'?

We want to achieve a fallback when a push is received and the user is inside the app at that moment.
I.e.: The user is on the start page and receives a push. In this moment, I would like to read the action (i.e. deeplink) from the received push, so I could show an html alert and could redirect the user to this page.
A solution would be the custom keys to store the 'path', thats our current solution, but I would prefer to get the actions data. Because a non technical employee could use airship to create pushes, without adding additional custom keys, that needs additional documentations.

Additional question: When the user is inside the app and receives a push, does Android show a push in the system notification center and iOS doesnt? In our previous implementation (using capacitors push notifications), I never got a system notification center entry in Android, while the user had the app opened. Thats also a point, why I would like to use the fallback inside the app.

I think that should be possible. On iOS you probably already have the actions in the extras but on Android you do not.

Android by default will show a notification in the center, but you can control it with sending the extra com.urbanairship.foreground_display: true. iOS its controlled by foreground presentation options.

I can probably throw together something real fast if you want to test it out.

Started this branch: https://github.com/urbanairship/urbanairship-cordova/tree/GH-393

My xcode is freezing so I need to figure that out before I can do the iOS action parsing. Also need to run some tests on Android to make sure it all works as expected

AE1NS commented

I see the 'action' property in the notification now, but its content seems wrong. Its always actions: {^d: null}.

Regarding the foreground notification: Did you have a typo? Because you mentioned com.urbanairship.foreground_display but added com.urbanairship.foreground_notifications in the code. I tried to add it to the cordova preferences/settings but I still get a system notification while the app is in foreground.

That is just the storage value, it does not need to match the notification value. I will take a look at the actions issue

@AE1NS pushed a change to fix the actions being null

AE1NS commented

Ok thanks! What about foreground display? How exactly can I use this config? Do I just have to set the cordova plugin config com.urbanairship.foreground_display to false?
In my case (because I am using capacitor) I have to set this config inside my capacitor.config.ts:

"cordova": {
    "preferences": {
        "com.urbanairship.production_app_key": "...",
        "com.urbanairship.production_app_secret": "...",
        "com.urbanairship.development_app_key": "...",
        "com.urbanairship.development_app_secret": "...",
        "com.urbanairship.in_production": "false",
        "com.urbanairship.development_log_level": "debug",
        "com.urbanairship.production_log_level": "error",
        "com.urbanairship.auto_launch_message_center": "true",
        "com.urbanairship.site": "EU",
        "com.urbanairship.foreground_display": "false"
    }
}

All configs are working fine, but I still get a system notification while the app is in foreground.

You can enable/disable it through JS. We persist the value so you only have to call it once. UAirship.setAndroidForegroundNotificationsEnabled(true)

AE1NS commented

UAirship.setAndroidForegroundNotificationsEnabled(false) to disable it for android looks good! Thank you!

Regarding the actions property, I see the deep link url now, but the key of the object looks strange. Is this intended to be ^d?
actions: {^d: 'https://...'}

The action names are kinda wonky, the are from a time when we were very limited on the payload size we could send to iOS so we had short names. Here is a resource file with all the action names:

<resources>
    <ActionEntry class="com.urbanairship.automation.actions.CancelSchedulesAction">
        <name>cancel_scheduled_actions</name>
        <name>^csa</name>
    </ActionEntry>

    <ActionEntry class="com.urbanairship.iam.actions.LandingPageAction">
        <name>landing_page_action</name>
        <name>^p</name>
    </ActionEntry>

    <ActionEntry class="com.urbanairship.automation.actions.ScheduleAction">
        <name>schedule_actions</name>
        <name>^sa</name>
    </ActionEntry>


    <ActionEntry class="com.urbanairship.chat.OpenChatAction">
        <name>open_chat_action</name>
    </ActionEntry>
    <ActionEntry class="com.urbanairship.chat.SendChatAction">
        <name>send_chat_action</name>
    </ActionEntry>

    <ActionEntry class="com.urbanairship.messagecenter.actions.MessageCenterAction">
        <name>open_mc_action</name>
        <name>^mc</name>
        <name>open_mc_overlay_action</name>
        <name>^mco</name>
    </ActionEntry>

    <ActionEntry
        class="com.urbanairship.actions.AddCustomEventAction"
        predicate="com.urbanairship.actions.AddCustomEventAction$AddCustomEventActionPredicate">
        <name>add_custom_event_action</name>
    </ActionEntry>

    <ActionEntry
        class="com.urbanairship.actions.tags.AddTagsAction"
        predicate="com.urbanairship.actions.tags.AddTagsAction$AddTagsPredicate">
        <name>add_tags_action</name>
        <name>^+t</name>
    </ActionEntry>

    <ActionEntry class="com.urbanairship.actions.ClipboardAction">
        <name>clipboard_action</name>
        <name>^c</name>
    </ActionEntry>

    <ActionEntry class="com.urbanairship.actions.DeepLinkAction">
        <name>deep_link_action</name>
        <name>^d</name>
    </ActionEntry>

    <ActionEntry class="com.urbanairship.actions.EnableFeatureAction">
        <name>enable_feature</name>
        <name>^ef</name>
    </ActionEntry>

    <ActionEntry class="com.urbanairship.actions.PromptPermissionAction">
        <name>prompt_permission_action</name>
        <name>^pp</name>
    </ActionEntry>

    <ActionEntry
        class="com.urbanairship.actions.FetchDeviceInfoAction"
        predicate="com.urbanairship.actions.FetchDeviceInfoAction$FetchDeviceInfoPredicate">
        <name>fetch_device_info</name>
        <name>^fdi</name>
    </ActionEntry>

    <ActionEntry class="com.urbanairship.actions.OpenExternalUrlAction">
        <name>open_external_url_action</name>
        <name>^u</name>
    </ActionEntry>

    <ActionEntry
        class="com.urbanairship.actions.tags.RemoveTagsAction"
        predicate="com.urbanairship.actions.tags.RemoveTagsAction$RemoveTagsPredicate">
        <name>remove_tags_action</name>
        <name>^-t</name>
    </ActionEntry>

    <ActionEntry
        class="com.urbanairship.actions.SetAttributesAction"
        predicate="com.urbanairship.actions.SetAttributesAction$SetAttributesPredicate">
        <name>set_attributes_action</name>
        <name>^a</name>
    </ActionEntry>

    <ActionEntry class="com.urbanairship.actions.ShareAction">
        <name>share_action</name>
        <name>^s</name>
    </ActionEntry>

    <ActionEntry class="com.urbanairship.actions.ToastAction">
        <name>toast_action</name>
    </ActionEntry>

    <ActionEntry class="com.urbanairship.actions.RateAppAction">
        <name>rate_app_action</name>
        <name>^ra</name>
    </ActionEntry>

    <ActionEntry class="com.urbanairship.actions.WalletAction">
        <name>wallet_action</name>
        <name>^w</name>
    </ActionEntry>

    <ActionEntry class="com.urbanairship.actions.SubscriptionListAction">
        <name>subscription_list_action</name>
        <name>^sla</name>
    </ActionEntry>
</resources>

You will probably only care about a few of them, like landing_page_action, add_tag_action, remove_tag_action, deep_link_action, etc..., but you could also just use the runAction method to run all of them when you think its appropriate.

AE1NS commented

Ok I see. Thanks for your fast replies!