NativeScript / push-plugin

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

Aggregate multiple Android Notifications #93

Open svzi opened 7 years ago

svzi commented 7 years ago

Hi there!

I’m looking for a way to aggregate my Android Notifications using this push-plugin, because I want to avoid showing 10 times the same message to the user in the notifications bar…

It should be possible to update existing notifications: https://developer.android.com/guide/topics/ui/notifiers/notifications.html#Updating

I've already talked with @Pip3r4o about that on Slack, here is what he answered to my initial request:

pete.k [10:19 AM] In the Background Notification demo I did I believe I just pass a certain parameter to the notification service, and no matter how many times the notification is fired, it shows just 1 icon on the notification bar [10:19]
let me check it out [10:22]
https://github.com/NativeScript/sample-android-background-services/blob/master/app/notifications/NotificationIntentService.js#L39 You send a specific flag to the Android Intent. I'd post this as an issue in the respective repo so that it gets looked at

Any way you can add that new feature? I guess it could become handy to a lot of projects. 👍

Best, Sven

AntonDobrev commented 7 years ago

@svzi Sven, thanks for the suggestion.

There is a similar functionality already implemented in the plugin.

Utilizing the data.notId key in the payload will allow for setting this with server control. For example - sending the same notId with the payload will update the existing notification in the tray with the same notId.

According to your words, this is the functionality you are looking after. Let me know if this is the case.

svzi commented 7 years ago

@AntonDobrev Anton, thanks for the answer.

I just tried it and sent some pushes with same data.notId, but they all show up in the notification tray of my device. So I guess I'm doing something wrong. Could you please offer any additional documentation or some code sample?

Best, Sven

AntonDobrev commented 7 years ago

@svzi Sven, here is an example:

Sending the following payload (using the same notId for the pushes you want to replace). For example, these title and message will replace the notification currently in the tray and with the same notId value.

Let me know if this works for you.

{
    "data":{
        "title":"some title 4",
        "message":"some message 4",
        "notId":123
    }
}
svzi commented 7 years ago

@AntonDobrev Anton, thanks for the example, but sadly that doesn't work. 😞 The notId doesn't change anything.

I've also tried to add it to the notificationproperty instead of data, but that doesn't help either.

This is my complete test push:

{
    to: device_token, // required fill with device token
    collapse_key: 'test_key',
    data: {
        notId: 1,
        your_custom_data_key: 'your_custom_data_value2'
    },
    notification: {
        title: 'Test',
        body: 'test body data.',
        icon: 'notification',
    }
}

For testing purposes I just modify notification.titleand notification.bodyand push again. The old notification in my tray doesn't get replaced, instead the new push message will be added as well.

svzi commented 7 years ago

Any update?

Daxito commented 7 years ago

I haven't tried it myself but, isn't there a "collapse_key" option you set in the payload for this?

See: http://docs.telerik.com/platform/backend-services/javascript/push-notifications/send-and-target/push-set-options.html#android-specific-fields

I DO use Telerik Backend Service but right now I am not using this option.

Daxito commented 7 years ago

It applies when the device is off though, don't know.

svzi commented 7 years ago

Thanks @Daxito, but you're right. It only applies when the device is offline and the message has not been sent to the device. As you may notice I already use that key (https://github.com/NativeScript/push-plugin/issues/93#issuecomment-279957444).

svzi commented 7 years ago

@AntonDobrev Any update on this issue? This is really important to our app!