NativeScript / push-plugin

Contains the source code for the Push Plugin.
Apache License 2.0
123 stars 45 forks source link

TrayNotification <missing message content> #98

Open philippsander opened 7 years ago

philippsander commented 7 years ago

I copied the sample code and actually recieve a message.

PushPlugin.onMessageReceived(function callback(message, data, notification) { });

However, my first parameter is null, the second one contains the data I actually need.

My console log:

02-13 16:29:00.040 14012 14101 W System.err: org.json.JSONException: No value for message 02-13 16:29:00.040 14012 14101 W System.err: at org.json.JSONObject.get(JSONObject.java:389) 02-13 16:29:00.040 14012 14101 W System.err: at org.json.JSONObject.getString(JSONObject.java:550)

How do I manipulate Notification (message and action)?

I'm using 0.1.2

philippsander commented 7 years ago

can someone help me with this please?

AntonDobrev commented 7 years ago

@philippsander At some point and depending on your payload, the message parameter will be empty. Please read more here - https://github.com/NativeScript/push-plugin#receive-and-handle-messages-from-fcm-on-android

If there is further ambiguity or uncertainty let me know the payload you are sending and I will try to help you out.

Happy coding!

philippsander commented 7 years ago

sorry... I don't understand how to use it.

PushPlugin.onMessageReceived(function callback(message, data, notification) {});

this is my method. How am I supposed to change the message of the notification?

the exception I posted appears before anything I do in the body of the callback.

my data looks like this:

{\"Header\":\"PSander Test\",\"Body\":\"my body\",\"foreground\":true}

AntonDobrev commented 7 years ago

@philippsander

Philip, here are some more explanations. However, you need first to be examine the payload sent to your app and what you would like to handle.

For example, payload for Android consists of a JSON object that contains either a data or notification keys in it, or both.

Utilizing the callback parameters depends on what you are sending with the payload. The message argument is usually precooked for you if there is a message key in the data payload. For full experience usually you need to leverage the second and third parameter depending on the payload and the app state (more in the documentation link from my previous reply). That is why I asked for your payload.

Example:

{
    "data":{
        "title":"my title",
        "message":"my message", // will be populated in the first argument of the callback 
... other keys
    },
   "notification": {
   // as per the docs https://firebase.google.com/docs/cloud-messaging/concept-options
    }
}

Regards, Anton

philippsander commented 7 years ago

The firebase system is coming from someone else. I need to manipulate the notification in my app if possible. is that possible or not?

again, that's my playload in data: {\"Header\":\"PSander Test\",\"Body\":\"my body\",\"foreground\":true}

AntonDobrev commented 7 years ago

@philippsander

Yes, you can leverage the data and notification arguments of the callback:

PushPlugin.onMessageReceived(function callback(message, data, notification) {

});`

Manipulating the way the notification is shown in the notification tray is not customizable via the plugin, if this is what you would like to achieve. It can be controlled via the notification payload or with further tweaks in the native code of the plugin.

Let me know more details on the use case and surely, the structure of the payload (if convenient).

EDIT:

Just reproduced it. Same comments as above.

philippsander commented 7 years ago

I feel so stupid.... how?!

data is a string... message and notification are null

PushPlugin.onMessageReceived(function callback(message, data, notification) {
      let dataObject = JSON.parse(data);
      dataObject.message = 'my message';

      data = JSON.stringify(dataObject);
});

this is not working.

philippsander commented 7 years ago

i just want to change the message of the notification from within my app... it can't be that hard.

AntonDobrev commented 7 years ago

@philippsander To be honest, initially I did not get right your question.

According to your payload, your app will receive in the second argument of the callback an object with the following keys: Header, Body, and the plugin set property foreground. However, the entry in the tray will note feature any title or message because the plugin is expecting them as "title" and "message" keys in the data payload.

To change the title and message you have the following two ways. Note that you cannot change them from the app because the notification is already processed as of that moment in time (however, you should be able to create with the native APIs a new notification and place it in the tray if you need to).

  1. From the server:
    • Set the title and message keys in the data payload (the plugin is designed to set them in the tray):
      {
      "data":{
      "title":"title 1",
      "message":"message 1"
      }
      }
    • set the title and body of the notification:
{
    "notification":{
        "title":"my title",
        "body":"my message"
    }
}
  1. From the plugin native code:

As you can see in the native code here the tray's notification is built there with title and message. You can use default values if you need to by modifying the plugin.

I hope this answers your question.

Some further notes:

The plugin automatically handles some keys in the data object like message, title, color, smallIcon, largeIconand uses them to construct a notification entry in the tray. More information on these keys is available in the documentation of the Telerik Platform Notifications service documentation article.

philippsander commented 7 years ago

I think it would be a handy feature to be able to modify the msgData.

beforeMessageReceived for example