如果您想通过通知传递数据,您需要data
在 OneSignal创建通知REST API POST 调用上设置该字段。
{
"app_id": "5eb5a37e-b458-11e3-ac11-000c2940e62c",
"included_segments": ["All"],
"data": {"user_level","10"},
"contents": {"en": "English Message"}
}
或 OneSignal 仪表板上“新消息”页面上选项下的“附加数据”字段。
然后,您可以调用setNotificationOpenedHandler
应用程序的代码以将NotificationOpenedHandler
will fire 添加到您的应用程序中notificationOpened
。
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) {
Log.d("OneSignalExample", "Full additionalData:\n" + additionalData.toString());
}
} catch (Throwable t) {
t.printStackTrace();
}
}
additionalData
将包含您在data
创建通知时在字段上设置的内容。
OneSignal.getTags
仅获取您在用户上设置的标签值OneSiganal.sendTags
。