我已将OneSignal集成到我的Android应用程序中以用于推送通知,所有工作正常并且通知都已通过。但是,为了以自定义方式处理这些通知,我需要更改什么?接收方需要从通知中解析JSON有效负载,并根据收到的内容将用户发送到应用中的正确位置。
答案 0 :(得分:1)
您需要设置NotificationOpenedHandler
类来处理通知的打开。请参阅以下示例代码进行设置。
import com.onesignal.OneSignal;
public class YourAppClass extends Application {
@Override
public void onCreate() {
super.onCreate();
OneSignal.startInit(this)
.setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
.init();
}
// This fires when a notification is opened by tapping on it or one is received while the app is running.
private class ExampleNotificationOpenedHandler implements NotificationOpenedHandler {
@Override
public void notificationOpened(String message, JSONObject additionalData, boolean isActive) {
try {
if (additionalData != null) {
if (additionalData.has("actionSelected"))
Log.d("OneSignalExample", "OneSignal notification button with id " + additionalData.getString("actionSelected") + " pressed");
Log.d("OneSignalExample", "Full additionalData:\n" + additionalData.toString());
}
} catch (Throwable t) {
t.printStackTrace();
}
}
}
如果您需要更改应用程序打开时启动的活动,请参阅OneSignal Changing the open action of a notification说明。