简体   繁体   English

通知后台服务

[英]Background Service for notifications

I'm working at a Notification provider for my Android application with a WebView that uses a JavaScriptInterface to get info from a remote Javascript file that according to received info from the server, it triggers my application to send a heads up notification. 我在一个Android应用程序的Notification提供程序中使用WebView,该WebView使用JavaScriptInterface从远程Javascript文件中获取信息,该Javascript文件根据从服务器接收到的信息,触发我的应用程序发送抬头通知。

So, I've added a JavaScript Interface into the onCreate void: 因此,我在onCreate void中添加了一个JavaScript接口:

OnomeWebContainer.addJavascriptInterface(new WebAppInterface(this), "Android");

And created the WebAppInterface class 并创建了WebAppInterface类

// Interfacing with Javascript Code
public class WebAppInterface {
    Context mContext;

    WebAppInterface(Context c) {
        mContext = c;
    }

    @JavascriptInterface
    public void showTempHumUnavailableNotification(String toast) {
        OnomeTempHumUnavailableNotification();
    }

    @JavascriptInterface
    public void showSecurDudeAlertNotification(String toast) {
        OnomeSecurDudeAlertNotification();
    }
}

The JavaScript File triggers the notification when a certain file is not available on the remote server. 当某个文件在远程服务器上不可用时,JavaScript文件将触发通知。

function showDHT11Alert() {
$.ajax({
    url: 'http://serveraddress/OnomeWeb/resources/data/tmp/onomeDHT11On',
    success: function () {
        $('#onomenotif_DHT11Unavailable').hide();
    },
    error: function () {
        $('#onomenotif_DHT11Unavailable').show();
        showDHT11Toast();
    },
});
}

function showDHT11Toast(toast) {
        Android.showTempHumUnavailableNotification(toast);
}

The notification provider works perfecly, even if the app is in background, but obviously if I close the app from the recent apps screen, the notification provider stops working. 通知提供程序可以正常运行,即使该应用程序在后台运行,但显然如果我从最近的应用程序屏幕关闭了该应用程序,通知提供程序将停止工作。 Now I'd like to make this JavaScript Interface working in a background service, even if the app is closed from the recent apps. 现在,我想使此JavaScript接口在后台服务中运行,即使该应用程序已从最近的应用程序中关闭。

The background service should be the best solution to my problem, but how can I interface this with the main WebView? 后台服务应该是我的问题的最佳解决方案,但是如何将其与主WebView交互? Is this possible? 这可能吗?

The background service should be the best solution to my problem, but how can I interface this with the main WebView? 后台服务应该是我的问题的最佳解决方案,但是如何将其与主WebView交互? Is this possible? 这可能吗?

Unfortunately its not possible. 不幸的是,这是不可能的。 The JavaScriptInterface is injected by your app by calling JavaScriptInterface是由您的应用通过调用而注入的

OnomeWebContainer.addJavascriptInterface(new WebAppInterface(this), "Android");

And its only available to your WebView. 并且它仅适用于您的WebView。 Other WebViews won't inject it and as a result won't be able to trigger your showTempHumUnavailableNotification 其他WebView将不会注入它,因此将无法触发您的showTempHumUnavailableNotification

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM