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



我使用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;
}
}

}

但编译后的代码会报告错误"missingreturn语句"。你可以尝试在这里构建这个代码代码编辑器

编辑时间:如果喜欢这个,我试着在后面添加退货声明

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

最新更新