如何使用科尔多瓦删除显示的通知

how to remove displayed notification using cordova

如何删除已显示(显示在 phone 栏上)但用户未回复的通知?使用科尔多瓦 https://github.com/katzer/cordova-plugin-local-notifications/

我检查了不同的方法,但没有得到它的任何属性或功能。虽然注册的时候

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);

你可以选择

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

这是有效的,但我如何使用编码删除。

我试过通过id取消

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

这将在 onTrigger 中注册。并通过 id 取消将在 5 秒后运行

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));
}

您应该使用 cancel 方法通过它的 id 删除通知。来自插件文档:

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

其中 id 只是您要关闭的通知的 ID。

如您所述,autoCancel 是在用户单击通知 时自动取消。如果它不是真的,您需要在处理该通知的回调时从 Cordova 中取消它。

更新

原来你在用

new Date()

as id 添加的通知,是因为有这个限制

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.

new Date() 产生类似于

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

不可转换为数字。

我认为这里的术语有点混乱。 "Notification"既用于指示操作系统安排一系列重复消息的指令,也用于指代这些消息的单个实例。

autoCancel 属性用于从 Android 消息栏和 window 用户点击它启动您的应用程序时自动删除通知消息的单个实例。如果将此设置为 false,应用程序图标将在这些区域中保持可见,直到用户手动将其删除。

回答最初的问题:除了 autoCancel 属性之外,Katzer 的插件无法擦除与通知实例关联的应用程序图标。