javascript - 清算系统通知

标签 javascript android cordova android-notifications

我正在尝试实现这个插件Phonegap system notification 。我正在阅读两个 Rss 提要并将其作为状态栏通知显示给用户。一旦通知显示给用户并且用户单击通知,他就会被带到应用程序,但我无法从状态栏中清除通知。您能否建议我通过查看我的代码来清除这些通知。 当用户单击状态栏通知时,如何调用 navigator.systemNotification.cancelNotification()

notification.js

google.load("feeds", "1");
google.setOnLoadCallback(function () {
var rss1old = '',
    rss1new = '',
    rss2old ='',
    rss2new ='',

    getRss = function (url, callback) {
        (url) && (function (url) {
            var feed = new google.feeds.Feed(url);

            feed.load(function (result) {
                (!result.error && callback) && (callback(result.feed.entries[0].title));
            });
        }(url));
    };

setInterval(function () {
    getRss(
        'http://yofreesamples.com/category/free-coupons/feed/?type=rss',
        function (title) {
            rss1new = title;
            if(rss1old !== rss1new) {
                rss1old = rss1new;
                navigator.systemNotification.onBackground();
                navigator.systemNotification.updateNotification(rss1new,1);
                navigator.notification.beep(1);
                navigator.notification.vibrate(2000);
            }

        }
    );
}, 5000);

setInterval(function () {
    getRss(
        'http://yofreesamples.com/category/real-freebies/feed/?type=rss',
        function (title) {
            rss2new = title;
            if(rss2old !== rss2new) {
                rss2old = rss2new;
                navigator.systemNotification.onBackground();
                navigator.systemNotification.updateNotification(rss2new,1);
                navigator.notification.beep(1);
                navigator.notification.vibrate(1000);  
            }

        }
    );
}, 6000);
});

SystemNotification.js -> 包含在插件中

function SystemNotification() {
}

SystemNotification.prototype.notificationEnabled = false;

SystemNotification.prototype.newCount = 0; //to keep track of multiple notifications events

SystemNotification.prototype.enableNotification = function () {
    this.notificationEnabled = true;
};

SystemNotification.prototype.disableNotification = function () {
    this.notificationEnabled = false;
};

SystemNotification.prototype.onBackground = function () {
    this.enableNotification();
};

SystemNotification.prototype.onForeground = function () {
    this.disableNotification();
};

SystemNotification.prototype.createStatusBarNotification = function (contentTitle, contentText, tickerText) {
    PhoneGap.exec(null, null, "systemNotification", "createStatusBarNotification", [contentTitle, contentText, tickerText]);
};

SystemNotification.prototype.updateNotification = function (contentText, tickerText, number) {
    this.newCount++;
    var contentTitle = this.newCount + "RssFeeds";
    if (this.newCount === 1) {
        this.createStatusBarNotification(contentTitle, contentText, tickerText);
    } else {
        PhoneGap.exec(null, null, "systemNotification", "updateNotification", [contentTitle, contentText, this.newCount]);
        this.showTickerText(tickerText);  //optional
    }
};

SystemNotification.prototype.cancelNotification = function (contentText) {
    this.newCount--;
    if (this.newCount === 0) {
        PhoneGap.exec(null, null, "systemNotification", "cancelNotification", []);
    }
    else {
    //updating the notification
        var contentTitle = "my title";
        PhoneGap.exec(null, null, "systemNotification", "updateNotification", [contentTitle, contentText, this.newCount]);
    }
};

SystemNotification.prototype.showTickerText = function (tickerText) {
    PhoneGap.exec(null, null, "systemNotification", "showTickerText", [tickerText]);
};

SystemNotification.prototype.touch = function () {
    PhoneGap.exec(null, null, "systemNotification", "touch", []);
};

PhoneGap.addConstructor(function () {
    if (typeof(navigator.systemNotification) == "undefined") {
        navigator.systemNotification = new SystemNotification();
        navigator.systemNotification.touch();  //this ensures that the plugin is added when phonegap kicks off
    }
});

Systemnotification.Java -> 包含在插件中

package com.yfs.project;

import org.json.JSONArray;
import org.json.JSONException;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;


import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;

public class SystemNotification extends Plugin {

    final int notif_ID = 1234;
    NotificationManager notificationManager;
    Notification note;
    PendingIntent contentIntent;

    @Override
    public PluginResult execute(String action, JSONArray args, String callbackId)
    {
        PluginResult.Status status = PluginResult.Status.OK;
        String result = "";

        try {
            if (action.equals("createStatusBarNotification")) {
                this.createStatusBarNotification(args.getString(0), args.getString(1), args.getString(2));
            }
            else if (action.equals("updateNotification")) {
                this.updateNotification(args.getString(0), args.getString(1), args.getInt(2));
            }
            else if (action.equals("cancelNotification")) {
                this.cancelNotification();
            }
            else if (action.equals("showTickerText")) {
                this.showTickerText(args.getString(0));
            }
            return new PluginResult(status, result);
        } catch(JSONException e) {
            return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
        }
    }

    private void updateNotification(String contentTitle, String contentText, int number)
    {
        note.setLatestEventInfo(this.ctx, contentTitle, contentText, contentIntent);
        note.number = number;
        notificationManager.notify(notif_ID,note);
    }

    private void createStatusBarNotification(String contentTitle, String contentText, String tickerText)
    {
        notificationManager = (NotificationManager) this.ctx.getSystemService(Context.NOTIFICATION_SERVICE);
        note = new Notification(R.drawable.rss, tickerText, System.currentTimeMillis() );
    //change the icon

    Intent notificationIntent = new Intent(this.ctx, Yfs.class);
        notificationIntent.setAction(Intent.ACTION_MAIN);
        notificationIntent = notificationIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        contentIntent = PendingIntent.getActivity(this.ctx, 0, notificationIntent, 0);

        note.setLatestEventInfo(this.ctx, contentTitle, contentText, contentIntent);

        note.number = 1;  //Just created notification so number=1. Remove this line if you dont want numbers

        notificationManager.notify(notif_ID,note);
    }

    private void cancelNotification()
    {
        notificationManager.cancel(notif_ID);
    }

    private void showTickerText(String tickerText)
    {
        note.tickerText = tickerText;
        notificationManager.notify(notif_ID,note);
    }

    public void onPause()
    {
        super.webView.loadUrl("javascript:navigator.systemNotification.onBackground();");
    }

    public void onResume()
    {
        super.webView.loadUrl("javascript:navigator.systemNotification.onForeground();");
    }
}

最佳答案

在 Android 上,您需要设置标志 AUTO_CANCEL

哪里有这个

note = new Notification(R.drawable.rss, tickerText, System.currentTimeMillis() );

在下面添加此行

note.flags = Notification.FLAG_AUTO_CANCEL;

关于javascript - 清算系统通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6985504/

相关文章:

javascript - 创建一个使用 React Native 的函数 'title case'

javascript - 如何在 Laravel Blade 中使用 Angular 变量

android - 在 Fragments 中使用 View 绑定(bind)在哪里更好? (onCreateView 与 onViewCreated)

android - 从源代码为小米 mi a1 构建沿袭 15.1 时获取 "invalid parameter name"

android - Google Play 商店为 SSL Handler cordova 应用程序 6.4.0 提供应用程序拒绝错误

JavaScript;如何在三位数字后设置点?

javascript - 链接到动态 anchor

jquery-mobile - jQuery Mobile : . animate({scrollTop}) 在修复页面转换后不起作用

android - 如何对蓝牙配件进行单元测试?

缺少cordova_plugins.js