gpt4 book ai didi

java - OneSignal customKey 获取错误 : missing return statement report

转载 作者:行者123 更新时间:2023-11-30 01:58:45 27 4
gpt4 key购买 nike

我使用codeEditor进行 appybuilder 扩展。我尝试获取应用程序从 OneSignal 获取推送通知时获得的 customkey 值。这是我尝试制作的完整代码:

import android.content.Context;
import android.util.Log;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.runtime.*;
import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.DesignerProperty;
import com.google.appinventor.components.annotations.PropertyCategory;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.annotations.SimpleProperty;
import com.google.appinventor.components.annotations.UsesLibraries;
import com.google.appinventor.components.annotations.UsesPermissions;
import com.onesignal.OSNotification;
import com.onesignal.OSNotificationAction.ActionType;
import com.onesignal.OSNotificationOpenResult;
import com.onesignal.OSPermissionSubscriptionState;
import com.onesignal.OneSignal;
import com.onesignal.OneSignal.LOG_LEVEL;
import com.onesignal.OneSignal.NotificationOpenedHandler;
import com.onesignal.OneSignal.NotificationReceivedHandler;
import com.onesignal.OneSignal.OSInFocusDisplayOption;
import org.json.JSONObject;

@DesignerComponent(version = 1, description = "This Extension was created with the AppyBuilder Code Editor.<br>" +
"Create your own here:<br><a href='https://editor.appybuilder.com' target='_blank'>https://editor.appybuilder.com</a><br>",
category = ComponentCategory.EXTENSION,
nonVisible = true, iconName = "http://appyBuilder.com/extensions/icons/extension.png")
@SimpleObject(external = true)
public class OneSignalPlus extends AndroidNonvisibleComponent {
private ComponentContainer container;
private boolean soundEnabled = true;
private boolean subscriptionEnabled = true;
private boolean vibrateEnabled = true;

private class ExampleNotificationReceivedHandler implements NotificationReceivedHandler {
private ExampleNotificationReceivedHandler() {
}

public void notificationReceived(OSNotification notification) {
JSONObject data = notification.payload.additionalData;
String notificationID = notification.payload.notificationID;
String title = notification.payload.title;
String body = notification.payload.body;
String smallIcon = notification.payload.smallIcon;
String largeIcon = notification.payload.largeIcon;
String bigPicture = notification.payload.bigPicture;
String smallIconAccentColor = notification.payload.smallIconAccentColor;
String sound = notification.payload.sound;
String ledColor = notification.payload.ledColor;
int lockScreenVisibility = notification.payload.lockScreenVisibility;
String groupKey = notification.payload.groupKey;
String groupMessage = notification.payload.groupMessage;
String fromProjectNumber = notification.payload.fromProjectNumber;
String rawPayload = notification.payload.rawPayload;
Log.d("OneSignalPush", "NotificationID received: " + notificationID);
if (data != null) {
String customKey = data.optString("customkey", null);
if (customKey != null) {
Log.d("OneSignalPush", "customkey set with value: " + customKey);
}
}
}
}

class NotificationOpenHandler implements NotificationOpenedHandler {
NotificationOpenHandler() {
}

public void notificationOpened(OSNotificationOpenResult osNotificationOpenResult) {
ActionType actionType = osNotificationOpenResult.action.type;
JSONObject data = osNotificationOpenResult.notification.payload.additionalData;
Log.d("OneSignalPush", "NotificationID received: " + data);
if (data != null) {
String customKey = data.optString("customkey", null);
if (customKey != null) {
Log.i("OneSignalExample", "customkey set with value: " + customKey);
} else {
Log.i("OneSignalExample", "No data");
}
}
}
}

public OneSignalPlus(ComponentContainer container) {
super(container.$form());
this.container = container;
OneSignal.setLogLevel(LOG_LEVEL.DEBUG, LOG_LEVEL.NONE);
OneSignal.startInit(container.$context()).autoPromptLocation(false).setNotificationReceivedHandler(new ExampleNotificationReceivedHandler()).inFocusDisplaying(OSInFocusDisplayOption.Notification).init();
}

/**
* Get Custom Key
*/
@SimpleProperty(category = PropertyCategory.BEHAVIOR, description = "Get custom Key. If ther is no customkey it will return '-1'.")
public final String GetCustomKey(OSNotificationOpenResult osNotificationOpenResult) {
try {
ActionType actionType = osNotificationOpenResult.action.type;
JSONObject data = osNotificationOpenResult.notification.payload.additionalData;
Log.d("OneSignalPush", "NotificationID received: " + data);
if (data != null) {
String customKey = data.optString("customkey", null);
if (customKey != null) {
return customKey;
} else {
return "-1";
}
}
} catch (NullPointerException e) {
return false;
}
}

/**
* Showing Log
*/
@SimpleProperty(description = "If you want to enable the log then set it to true.")
public final void EnableLog(boolean z) {
PushNotifications pushNotifications = this;
if (z) {
LOG_LEVEL log_level = LOG_LEVEL.DEBUG;
OneSignal.setLogLevel(log_level, log_level);
return;
}
OneSignal.setLogLevel(LOG_LEVEL.DEBUG, LOG_LEVEL.NONE);
}

@SimpleProperty(category = PropertyCategory.BEHAVIOR, description = "Get the subscription Status")
public final boolean GetSubscriptionStatus() {
try {
return OneSignal.getPermissionSubscriptionState().getSubscriptionStatus().getSubscribed();
} catch (NullPointerException e) {
return false;
}
}

}

但是编译时的代码给出了错误报告“缺少返回语句”。您可以尝试在此处构建此代码 codeEditor

编辑:我尝试在 if 之后添加 return 语句,如下所示

public final String GetCustomKey(OSNotificationOpenResult osNotificationOpenResult) {
ActionType actionType = osNotificationOpenResult.action.type;
JSONObject data = osNotificationOpenResult.notification.payload.additionalData;
String customKey;
if (data != null) {
customKey = data.optString("customkey", null);
if (customKey != null) {
return customKey;
} else {
return "-1";
}
}
return "-1";
}

public final String GetCustomKey(OSNotificationOpenResult osNotificationOpenResult) {
ActionType actionType = osNotificationOpenResult.action.type;
JSONObject data = osNotificationOpenResult.notification.payload.additionalData;
String customKey;
if (data != null) {
customKey = data.optString("customkey", null);
if (customKey != null) {
return customKey;
}
}
return "-1";
}

但它给了我更多警告和错误报告

你能告诉我代码中有什么错误或错误吗

最佳答案

您需要从内部 if 语句中删除 return "-1";

public final String GetCustomKey() {
if (data != null) {
customKey = data.optString("customkey", null);
if (customKey != null) {
return customKey;
}
}

return "-1";
}

为了获得奖励积分,您可以稍微清理一下方法,例如

public final String GetCustomKey(OSNotificationOpenResult osNotificationOpenResult) {
ActionType actionType = osNotificationOpenResult.action.type;
JSONObject data = osNotificationOpenResult.notification.payload.additionalData;

return data.containsKey("customkey") ? data.get("customkey").toString() : "-1";
}

关于java - OneSignal customKey 获取错误 : missing return statement report,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53549793/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com