本地通知侦听器未执行操作
Local notification listener didn't perform action
我想为本地通知添加一个侦听器,以便当用户按下通知时浏览器打开,这是我的代码:
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() {
}
}
但在新闻发布的那一刻没有执行任何操作。
另一个问题是通知中传递的图像没有出现。
我试图查看官方文档,但没有找到任何有用的信息。
您所依赖的 API(例如并非所有地方都支持的徽章)。您应该删除该代码。未调用通知的原因是您没有在主 class 中实现 LocalNotificationCallback
接口。你只是写了方法,没有实现接口。
我想为本地通知添加一个侦听器,以便当用户按下通知时浏览器打开,这是我的代码:
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() {
}
}
但在新闻发布的那一刻没有执行任何操作。 另一个问题是通知中传递的图像没有出现。 我试图查看官方文档,但没有找到任何有用的信息。
您所依赖的 API(例如并非所有地方都支持的徽章)。您应该删除该代码。未调用通知的原因是您没有在主 class 中实现 LocalNotificationCallback
接口。你只是写了方法,没有实现接口。