简体   繁体   English

实现Phonegap系统通知插件

[英]Implementing Phonegap system notification plugin

I am trying to implement this plugin Phonegap system notification . 我正在尝试实现此插件Phonegap系统通知

I am calling the navigator.systemNotification.onBackground() and navigator.systemNotification.updateNotification(contentText, tickerText, number) methods inside my Javascript so that when a new RSS feed title comes up I should get a system notification and an updated counter for each new RSS feed. 我在我的Javascript中调用了navigator.systemNotification.onBackground()navigator.systemNotification.updateNotification(contentText, tickerText, number)方法,以便在出现新的RSS feed标题时,我应该获得系统通知和每个计数器的更新计数器新的RSS提要。

  1. Right now I get a system notification when a new feed is present but when a second feed comes up the counter is not increasing it still shows 1 on the notification (icon/image). 现在,当出现新的提要时,我会收到系统通知,但是当第二个提要出现时,计数器没有增加,它在通知(图标/图像)上仍显示1。
  2. I want the notification methods to run even in the background, which I am not able to test as the counter is not getting updated. 我希望通知方法甚至在后台运行,由于计数器没有更新,因此我无法对其进行测试。
  3. I am able to clear the system notification when I click the clear button . 单击清除按钮时,我可以清除系统通知。 Is it possible to clear it when the user clicks on the system notification? 用户单击系统通知时可以清除它吗?

I think I am doing something wrong in my Javascript. 我认为我在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();

This is the systemnotification.js : 这是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
    }
});

This is the systemnotification.java : 这是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();");
    }
}

In the SystemNotification.java file just replace the method onPause() for this one : 在SystemNotification.java文件中,只需为此方法替换onPause()方法:

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

For clearing the notification this is my answer. 为了清除通知,这是我的答案。

On android, you'll need to set the flag AUTO_CANCEL 在android上,您需要设置标志AUTO_CANCEL

Where you have this 你在哪里

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

Add this line right under 在下面添加此行

note.flags = Notification.FLAG_AUTO_CANCEL;

For notification to work in background, your JavaScript should be able to run in background. 为了使通知在后台运行,您的JavaScript应该能够在后台运行。 Solution described at PhoneGap discussion forum PhoneGap论坛上描述的解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 React 上实现 react-notification-system - Implementing react-notification-system on React Phonegap-plugin-push处于打开状态。(“ notification”)不会触发 - Phonegap-plugin-push's on.('notification') doesn't fire 如何通过使用phonegap localnotification插件清除通知栏上的项目 - how to clear the item on notification bar by using phonegap localnotification plugin 为什么cordova-local-notification-plugin不起作用(Phonegap,Android)? - Why is the cordova-local-notification-plugin not working (Phonegap, Android)? 使用Eclipse(推插件)在PhoneGAP中推送通知:未获得注册ID - Push Notification in PhoneGAP using Eclipse (Push Plugin) : Didn't get Registration ID PhoneGap应用上的Apache Cordova的Windows Azure通知中心插件-WindowsAzure未定义 - Windows Azure Notification Hubs plugin for Apache Cordova on PhoneGap App - WindowsAzure is not defined 当应用程序处于前台时,phonegap-plugin-push不触发新通知的回调函数 - phonegap-plugin-push not triggering callback function for new notification when app is in foreground 使用Phonegap解析推送通知 - Parse Push Notification with Phonegap 在Phonegap中加载通知 - Loading Notification In Phonegap phonegap本地通知错误 - phonegap local notification error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM