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

[Android] Add data/background message support #876

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

anatoly-spb
Copy link

@anatoly-spb anatoly-spb commented Apr 5, 2023

Description

One Line Summary

[Android] Add data/background message support

Details

Motivation

Since onesignal-cordova-plugin@3 we have no possibility to send data message without showing popup like a:

{
  "app_id": "xxxx",
  "include_player_ids": ["yyyy"],
  "data": {
     "action": "visit_refresh"
  },
  "content_available": true
}

To process data message with content_available=true on Android I propose to add OSRemoteNotificationReceivedHandler
according to https://documentation.onesignal.com/docs/data-notifications#android-data-notification-setup
https://documentation.onesignal.com/docs/service-extensions#android-notification-service-extension

And notify about data/background message via setNotificationWillShowInForegroundHandler. So client API is not changed.

See
#835
#865
#875

Scope

Android

OPTIONAL - Other

Testing

Unit testing

Manual testing

I subscribe on setNotificationWillShowInForegroundHandler() in application:

            OneSignal.setNotificationWillShowInForegroundHandler((notificationReceivedEvent)=>{
              const notification = notificationReceivedEvent.getNotification();
              this.logger.log(`PushService: notificationWillShowInForeground: notification=${JSON.stringify(notification)}`);
              // Silence notification by calling complete() with no argument
              notificationReceivedEvent.complete(notification);
            })

Run one on Android emulator from Android 7 to Android 12.

Send to application data message:

{
  "app_id": "xxxx",
  "include_player_ids": ["yyyy"],
  "data": {
     "action": "visit_refresh"
  },
  "content_available": true
}

And I see in a log the following:

PushService: notificationWillShowInForeground: notification={"notificationId":"b62ab6da-7b24-46b2-8635-550b8d3cdbbe","additionalData":{"action":"visit_refresh"},"rawPayload":{"google.delivered_priority":"normal","google.sent_time":1680680487811,"google.ttl":259200,"google.original_priority":"normal","custom":"{\"a\":{\"action\":\"visit_refresh\"},\"i\":\"b62ab6da-7b24-46b2-8635-550b8d3cdbbe\"}","from":"267986468402","google.message_id":"0:1680680487816734%24bd0d61f9fd7ecd","google.c.sender.id":"267986468402"},"priority":0,"fromProjectNumber":"267986468402","lockScreenVisibility":1,"androidNotificationId":326831907}

We get data message notification as expected.

Affected code checklist

  • Notifications
    • Display
    • Open
    • Push Processing
    • Confirm Deliveries

Checklist

Overview

  • I have filled out all REQUIRED sections above
  • PR does one thing
    • If it is hard to explain how any codes changes are related to each other then it most likely needs to be more than one PR
  • Any Public API changes are explained in the PR details and conform to existing APIs

Testing

  • I have included test coverage for these changes, or explained why they are not needed
  • All automated tests pass, or I explained why that is not possible
  • I have personally tested this on my device, or explained why that is not possible

Final pass

  • Code is as readable as possible.
    • Simplify with less code, followed by splitting up code into well named functions and variables, followed by adding comments to the code.
  • I have reviewed this PR myself, ensuring it meets each checklist item
    • WIP (Work In Progress) is ok, but explain what is still in progress and what you would like feedback on. Start the PR title with "WIP" to indicate this.

This change is Reviewable

@marshall86
Copy link

I'm trying this fix and it seems to be working! Any idea how to do the same thing on iOS please? Many thanks

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

Successfully merging this pull request may close these issues.

2 participants