簡體   English   中英

實現Phonegap系統通知插件

[英]Implementing Phonegap system notification plugin

我正在嘗試實現此插件Phonegap系統通知

我在我的Javascript中調用了navigator.systemNotification.onBackground()navigator.systemNotification.updateNotification(contentText, tickerText, number)方法,以便在出現新的RSS feed標題時,我應該獲得系統通知和每個計數器的更新計數器新的RSS提要。

  1. 現在,當出現新的提要時,我會收到系統通知,但是當第二個提要出現時,計數器沒有增加,它在通知(圖標/圖像)上仍顯示1。
  2. 我希望通知方法甚至在后台運行,由於計數器沒有更新,因此我無法對其進行測試。
  3. 單擊清除按鈕時,我可以清除系統通知。 用戶單擊系統通知時可以清除它嗎?

我認為我在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();");
    }
}

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

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

為了清除通知,這是我的答案。

在android上,您需要設置標志AUTO_CANCEL

你在哪里

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

在下面添加此行

note.flags = Notification.FLAG_AUTO_CANCEL;

為了使通知在后台運行,您的JavaScript應該能夠在后台運行。 PhoneGap論壇上描述的解決方案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM