Muhammad Faizan Khan
Muhammad Faizan Khan

Reputation: 10541

how to remove displayed notification using cordova

how can i remove notification which has been displayed(presend on phone bar) but not repspond by the user? using cordova https://github.com/katzer/cordova-plugin-local-notifications/

i check different method but didn't get any property or function of it. although when registering

window.plugin.notification.local.add({
    id:         String,  // A unique id of the notification
    date:       Date,    // This expects a date object
    message:    String,  // The message that is displayed
    title:      String,  // The title of the message
    repeat:     String,  // Either 'secondly', 'minutely', 'hourly', 'daily', 'weekly', 'monthly' or 'yearly'
    badge:      Number,  // Displays number badge to notification
    sound:      String,  // A sound to be played
    json:       String,  // Data to be passed through the notification
    autoCancel: Boolean, // Setting this flag and the notification is automatically cancelled when the user clicks it
    ongoing:    Boolean, // Prevent clearing of notification (Android only)
}, callback, scope);

you have an option of

  autoCancel: Boolean, // Setting this flag and the notification is automatically cancelled when the user clicks it

This is working but how can i removed using coding.

I tried cancel By id

function cancelLocalNotificationById(id){
        window.plugin.notification.local.cancel(id, function(){
            alert("cancel callback", id);
        });
    }
}

this will register in onTrigger. and cancel by id will run after 5 seconds

function onTrigger(){
 window.plugin.notification.local.ontrigger = function (id, state, json) {
            alert("onTrigger fired");
            alert(id);

                // Cancel alert after 5 seconds...
                timeouts.push(setTimeout(function(){
                    cancelLocalNotificationById(id);
                    alert(id);
                    //alert("cancel reslut"+cancel.status);
                },5000));
}

Upvotes: 2

Views: 3633

Answers (2)

Michel Reij
Michel Reij

Reputation: 346

I think there's a bit of confusion of terms here. "Notification" is both used to denote the instruction to the operating system to schedule a series of repeating messages and it is used to refer to a single instance of those messages.

The autoCancel attribute is used to automatically remove a single instance of a notification message from the Android messaging bar and window when the users taps it to launch your app. If you set this to false, the app icon remains visible in those areas until the user erases it manually.

To answer the original question: there is no way with Katzer's plugin to erase the app icon associated with an instance of the notification other than the autoCancel attribute.

Upvotes: 0

Roope Hakulinen
Roope Hakulinen

Reputation: 7405

You should use cancel method to remove the notification by it's id. From the plugin documentation:

window.plugin.notification.local.cancel(ID, function () {
    // The notification has been cancelled
}, scope);

where id is simply the id of notification you want to dismiss.

As you mentioned autoCancel is to do the cancelling automatically when user clicks the notification. Without it being true, you need to cancel it from within Cordova while you handle the callback of that notification.

Update

As it turned out that you are using

new Date()

as id for notifications added, it is the cause since there is this restriction

Note: On Android the notification id needs to be a string which can be converted to a number. If the ID has an invalid format, it will be ignored, but cancelling the notification will fail.

and the new Date() yields something like

Wed Jan 07 2015 14:16:10 GMT+0200 (FLE Standard Time)

which isn't convertible to number.

Upvotes: 1

Related Questions