Obtaining custom notification header in Android

Hi,

When sending a custom push notification (not from a template) through the SDK I set a custom header like so:

                publishOptions.putHeader("signalId", signalId);

How can I obtain that header when the notification is opened in Android?

Best,
Milen

Hi, please checkout the documentations for getting headers value - https://backendless.com/docs/android/push_notification_content.html
It’s described at the very bottom

Anton

Hi Anton,

This is what the linked documentation says:
// if retrieving in a button handling activity:
Intent intent = this.getIntent();
String headerValue = intent.getStringExtra( “sample_header_name” );

However, I don’t have buttons on my notifications and use them to launch the app when the user taps the notification itself. The intent of the main activity that is launched has no extras. So I don’t know how to obtain the notification info in this use case.

Thanks,
Milen

Hi, Milen.
For now we can only advise to construct a template (based on your message data) and retrieve headers from it.
You no need to use buttons in templates to get your headers. They will be available just in the intent, that comes into activity handler on a tap.

Hi Oleg,

I don’t use a template because I need to customize the notification from code and as far as I know you cannot customize templates.
The two specific things I need are:

  1. Deliver notification to particular devices that are determined dynamically in code
  2. Add custom header information that is also dynamic

(I know about Smart Text but it doesn’t achieve the job in this case as the info is not directly related to the user, receiving the notification)

Any suggestion on how I can retrieve custom header information in Android while achieving the above two requirements? I can successfully do this in iOS but not in Android.

Thanks!

Hi @milen-marinov

I believe for now getting header values is available only from Intent if notification was sent from a template. I’ve scheduled a task to add a feature for extracting header values in Android for the rest of the cases. We’ll notify you as soon the task is complete. I think it’s a matter of 1-2 weeks

Anton

1 Like

Hi Anton,

Thanks a lot!

Hi Anton,

Can you update me on the progress of this task? I really need this functionality and missing it is blocking work on my app :frowning:

Thanks,
Milen

Hello? Can I get some attention on this topic?

The fix in the repo now https://github.com/Backendless/Android-SDK/pull/371 we will release android sdk on this week

Great! Thanks for the update! Looking forward to the new release!

Hey @milen-marinov!

Fix is now available in version 5.2.5 of Android SDK. It’s now available in Maven central.
Code snippet for extracting headers:

String headerValue = intent.getStringExtra( “your_header_name” );

This code works when headers are added either with putHeader(String header) or setHeaders(Map<String, String> headers) method

Regards,
Anton

1 Like

I tried it and it works! Thanks!

I ran into a problem - if the app receives several notifications the custom headers from the first one remain unchanged even after the other notifications are received.
Steps to reproduce:

  1. Send a notification with “custom_header” = “1”
  2. Open notification (main activity is launched)
  3. From the main activity obtain the value of “custom_header” (it is correctly “1”)
  4. Minimize app
  5. Send another notification with “custom_header” = “2”
  6. Open notification (main activity is again launched)
  7. From the main activity obtain the value of “custom_header”

Expected result:
“custom_header” now has a value of “2”

Actual result:
“custom_header” is still “1”

Am I missing something or is this a bug with the SDK?

Hi @milen-marinov, did you verify it with debugger? Just wanted to make sure it is not anything in your code?

Does the same behavior occur if you restart the app instead of minimizing it?

Hi @mark-piller,

Yes, I found this while debugging why the app kept focusing the item from the first notification. The problem is indeed that getIntent().getStringExtra("custom_header") returns the value from the first notification.

Just tried restarting the app and curiously enough the behaviour remains the same!

Thanks, we will look into it. Internal ticket number is BKNDLSS-19103

1 Like

Hey @milen-marinov

I’ve tried changing values of custom headers on push notification publishing and everything worked fine in my case. What I did:

  1. In onMessage() of my custom PushService class (which extends BackendlessFCMService) getting the header as followed:
    final String customHeader = intent.getStringExtra("customHeader");

  2. Tried sending a message via console (Messaging tab > Messages screen) and via Android SDK v 5.2.5

  3. Then simply printing header’s value to logcat.
    Log.i(“HEADER”, customHeader);
    Every message that was sent has different value for custom header, and the result proofs that SDK itself correctly handles this

     2019-07-03 10:49:55.055 3323-4185/com.testing.backendless_messaging_test I/HEADER: WORKS!
     2019-07-03 10:50:33.419 3323-4185/com.testing.backendless_messaging_test I/HEADER: 111111111111111111111111111111111111111111
     2019-07-03 10:59:14.460 3323-4185/com.testing.backendless_messaging_test I/HEADER: 1000
     2019-07-03 10:59:40.009 3323-4185/com.testing.backendless_messaging_test I/HEADER: 2000
    

Thus it’s a problem which needs to be handled on client side

Anton

Hi @anton-govorushkin,

I don’t use a custom PushService. I just obtain the header from the extras of the launched Activity's Intent:
getIntent().getStringExtra(Signal.KEY_SIGNAL_ID)

How differently should this be handled?

Our Android developer will contact you shortly with additional details/questions