简体   繁体   English

本地通知侦听器未执行操作

[英]Local notification listener didn't perform action

I want to add a listener to the local notification so when the user press on the notification a browser opens, this is my code:我想在本地通知中添加一个侦听器,因此当用户按下浏览器打开的通知时,这是我的代码:

  public class MyApplication {

private Form current;
private Resources theme;

Form hi;
Form web;

EncodedImage ei;
Image img;
String url = "https://d1fmx1rbmqrxrr.cloudfront.net/cnet/optim/i/edit/2019/04/eso1644bsmall__w770.jpg";

public void init(Object context) {
    // use two network threads instead of one
    updateNetworkThreadCount(2);

    theme = UIManager.initFirstTheme("/theme");

    // Enable Toolbar on all Forms by default
    Toolbar.setGlobalToolbar(true);

    // Pro only feature
    Log.bindCrashProtection(true);

    addNetworkErrorListener(err -> {
        // prevent the event from propagating
        err.consume();
        if (err.getError() != null) {
            Log.e(err.getError());
        }
        Log.sendLogAsync();
        Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
    });
}

public void start() {
    if (current != null) {
        current.show();
        return;
    }

    try {
        ei = EncodedImage.create("/loading.gif");
    } catch (IOException ex) {
        System.out.println(ex.getMessage());
    }
    img = URLImage.createToStorage(ei, url, url, URLImage.RESIZE_SCALE);

    LocalNotification n = new LocalNotification();
    n.setId("Welcome");
    n.setAlertBody("Welcome");
    n.setAlertTitle("Welcome");
    // n.setAlertSound("/notification_sound_bells.mp3"); //file name must begin with notification_sound
    n.setAlertImage(img.toString());

    n.setId("5");
    n.setBadgeNumber(0);
    Display.getInstance().scheduleLocalNotification(
            n,
            System.currentTimeMillis() + 10 * 1000, // fire date/time
            LocalNotification.REPEAT_NONE // Whether to repeat and what frequency
    );

    if (n.getBadgeNumber() > 0) {
        localNotificationReceived("5");
    }
    hi = new Form("Hi World", BoxLayout.y());
    web = new Form("web", BoxLayout.y());

    hi.add(new Label("Hi World"));

    BrowserComponent browser = new BrowserComponent();
    browser.setURL("https://www.codenameone.com/");
    web.add(browser);
    hi.show();
}

public void localNotificationReceived(String notificationId) {
    web.show();
}

public void stop() {
    current = getCurrentForm();
    if (current instanceof Dialog) {
        ((Dialog) current).dispose();
        current = getCurrentForm();
    }
}

public void destroy() {
}

}

but no action was executed on the moment of the press.但在新闻发布时没有采取任何行动。 Another problem the image passed in the notification didn't appear.通知中传递的图像没有出现的另一个问题。 I tried to see the official documentation but nothing useful found.我试图查看官方文档,但没有发现任何有用的信息。

You're relying on APIs such as badging which aren't supported everywhere.您依赖的 API,例如并非所有地方都支持的徽章。 You should remove that code.您应该删除该代码。 The reason notifications aren't invoked is that you didn't implement the LocalNotificationCallback interface in your main class.未调用通知的原因是您没有在主 class 中实现LocalNotificationCallback接口。 You just wrote the method without implementing the interface.您只是编写了方法而没有实现接口。

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

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