实施Phonegap系统通知插件

时间:2011-08-02 15:16:22

标签: javascript android cordova

我正在尝试实现此插件Phonegap system notification

我在Javascript中调用navigator.systemNotification.onBackground()navigator.systemNotification.updateNotification(contentText, tickerText, number)方法,以便在出现新的RSS提要标题时,我应该为每个新的RSS提要获得系统通知和更新的计数器。

  1. 现在,当有新的Feed时,我会收到系统通知 当第二个进料出现时,计数器不会增加它 在通知(图标/图像)上显示1。
  2. 我希望通知方法即使在后台运行也是如此 我无法测试,因为计数器没有得到更新。
  3. 点击清除后,我可以清除系统通知 按钮。当用户点击时可以清除它 系统通知?
  4. 我认为我在Javascript中做错了。

        var oldEntry="";
        document.addEventListener("deviceready", onDeviceReady, false);
    
        // PhoneGap is ready
        //
        function onDeviceReady() {
            // Empty
        }
    
        // Show a custom alert
        //
        function sendRequest(){
          getRss("http://rss.cnn.com/rss/cnn_latest.rss.xml");      
          setTimeout('showAlert(newEntry.title)',4000);
        }
    
        function showAlert(data) {
            var st = randomString();
            if(oldEntry==""){
            oldEntry = data;
    
                navigator.systemNotification.onBackground();
                navigator.systemNotification.updateNotification(data, 'test' , '1');     
            } else {
                if(oldEntry!=data){
                    navigator.notification.alert(
                    data,               // message
                        'New Rss Entry',    // title
                        'New Rss Entry');
    
                    oldEntry = data;           
                }
           }
    
           setTimeout('sendRequest()',7000);
        }
    
        function randomString() {
            var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
            var string_length = 8;
            var randomstring = '';
            for (var i=0; i<string_length; i++) {
                var rnum = Math.floor(Math.random() * chars.length);
                randomstring += chars.substring(rnum,rnum+1);
            }
            return randomstring;
        }
    
       sendRequest();
    

    这是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 = "my title";
        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

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

3 个答案:

答案 0 :(得分:2)

在SystemNotification.java文件中,只需替换onPause()方法:

public void onPause()
{
    super.webView.loadUrl("javascript:try{PhoneGap.onResume.fire();}catch(e){};");
    super.webView.resumeTimers();
}

答案 1 :(得分:1)

为了清除通知,这是我的答案。

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

你有这个

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

下添加此行

note.flags = Notification.FLAG_AUTO_CANCEL;

答案 2 :(得分:1)

要在后台工作通知,您的JavaScript应该能够在后台运行。解决方案在PhoneGap discussion forum

中描述