Search code examples
javacodenameone

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.


Solution

  • You're relying on APIs such as badging which aren't supported everywhere. You should remove that code. The reason notifications aren't invoked is that you didn't implement the LocalNotificationCallback interface in your main class. You just wrote the method without implementing the interface.