Android / OneSignal - 知道点击的通知是否被分组
Android / OneSignal - Know if the clicked notification was grouped
所以这个问题是不言自明的。我想知道点击的通知是分组通知还是单个通知。在此基础上,将推出Activity。
例如,如果通知被分组,我想启动 "All messages" Activity,如果没有,它只会启动聊天 Activity。
这是我当前的代码:
public class NotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
private Application application;
String message;
public NotificationOpenedHandler(Application application) {
this.application = application;
}
@Override
public void notificationOpened(OSNotificationOpenResult result) {
// Get custom data from notification
JSONObject data = result.notification.payload.additionalData;
message = data.optString("message");
startApp(message);
}
private void startApp(String text) {
Intent intent;
SharedPreferences sharedPreferences = application.getSharedPreferences("appdata", Context.MODE_PRIVATE);
if (sharedPreferences.getInt("pending_notifications", 1) > 1) {
intent = new Intent(application, DemoActivity.class);
} else {
intent = new Intent(application, ReadLetterActivity.class);
}
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("pending_notifications", 0);
editor.apply();
intent.putExtra("message", text);
startActivity(intent);
}
}
如您所见,我尝试使用 SharedPreferences,但效果不佳。
总结一下,我想知道是否有办法(本机或由 OneSignal 提供)知道通知是否已分组。谢谢
没关系。找到了出路。这可以完美区分单个通知和分组通知,即使来自组的通知是单独打开的。
@Override
public void notificationOpened(OSNotificationOpenResult result) {
if (result.notification.groupedNotifications == null) {
// if the clicked notification was single/clicked separately from a group
Toast.makeText(getApplicationContext(), "Single notification", Toast.LENGTH_LONG).show();
} else {
// if the clicked notification was grouped
Toast.makeText(getApplicationContext(), "Grouped notification quantity: " + String.valueOf(result.notification.groupedNotifications.size()), Toast.LENGTH_LONG).show();
}
}
所以这个问题是不言自明的。我想知道点击的通知是分组通知还是单个通知。在此基础上,将推出Activity。
例如,如果通知被分组,我想启动 "All messages" Activity,如果没有,它只会启动聊天 Activity。
这是我当前的代码:
public class NotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
private Application application;
String message;
public NotificationOpenedHandler(Application application) {
this.application = application;
}
@Override
public void notificationOpened(OSNotificationOpenResult result) {
// Get custom data from notification
JSONObject data = result.notification.payload.additionalData;
message = data.optString("message");
startApp(message);
}
private void startApp(String text) {
Intent intent;
SharedPreferences sharedPreferences = application.getSharedPreferences("appdata", Context.MODE_PRIVATE);
if (sharedPreferences.getInt("pending_notifications", 1) > 1) {
intent = new Intent(application, DemoActivity.class);
} else {
intent = new Intent(application, ReadLetterActivity.class);
}
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("pending_notifications", 0);
editor.apply();
intent.putExtra("message", text);
startActivity(intent);
}
}
如您所见,我尝试使用 SharedPreferences,但效果不佳。
总结一下,我想知道是否有办法(本机或由 OneSignal 提供)知道通知是否已分组。谢谢
没关系。找到了出路。这可以完美区分单个通知和分组通知,即使来自组的通知是单独打开的。
@Override
public void notificationOpened(OSNotificationOpenResult result) {
if (result.notification.groupedNotifications == null) {
// if the clicked notification was single/clicked separately from a group
Toast.makeText(getApplicationContext(), "Single notification", Toast.LENGTH_LONG).show();
} else {
// if the clicked notification was grouped
Toast.makeText(getApplicationContext(), "Grouped notification quantity: " + String.valueOf(result.notification.groupedNotifications.size()), Toast.LENGTH_LONG).show();
}
}