OneSignal通知をタップするときにランチャー/メインアクティビティを開く/再開したい場合は、次のコードをアクティビティに追加してください。
private static boolean activityStarted;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ( activityStarted
&& getIntent() != null
&& (getIntent().getFlags() & Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
finish();
return;
}
activityStarted = true;
}
詳しくはResume last Activity when opening a Notificationの説明を参照してください。
さらにカスタムを行う必要がある場合は、上記のマニフェストエントリを保持し、Application
クラスのNotificationOpenedHandler
をOneSignal.startInit
に追加します。
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) {
// The following can be used to open an Activity of your choice.
/*
Intent intent = new Intent(getApplication(), YourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
*/
// Follow the instructions in the link below to prevent the launcher Activity from starting.
// https://documentation.onesignal.com/docs/android-notification-customizations#changing-the-open-action-of-a-notification
}
}
このコールバックの詳細については、4. Add Optional NotificationOpenedHandlerを参照してください。