Java OneSignal customKey获取错误:缺少返回语句报告

Java OneSignal customKey获取错误:缺少返回语句报告,java,onesignal,Java,Onesignal,我用来制作appybuilder扩展。我试图获取应用程序从OneSignal获得推送通知时获得的customkey值。 以下是我尝试编写的完整代码: import android.content.Context; import android.util.Log; import com.google.appinventor.components.common.ComponentCategory; import com.google.appinventor.components.annotation

我用来制作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;
    }
}

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


你能给我一个提示,代码中有什么错误或更少的地方吗?你需要删除
返回“-1”超出内部
if
语句

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";
}

我已通过在问题中添加错误报告对其进行了编辑。我先试试你的加分:)这看起来与osNotificationOpenResult完全不同。我已经编辑了我的问题,但仍然是同一个问题,我只是添加了我的完整代码以及我试图构建代码的地方。
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";
}