2016-06-12 7 views
0

OpenSignalからプッシュ通知をタップするとメインアクティビティを開く方法を教えてください。私は、Appがアクティブだったときに何らかの問題を引き起こしていたデフォルトの動作を上書きしたいと思っていました。アプリが閉じている場合DOCあたりOneSignal SDK:ユーザーが通知をタップした後にMainActivityを開く方法

<meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="DISABLE" /> 

は今、どのように私はMainActivityを開き、それがNotificationOpenedHandlerを実行させることができますように私は次の行を追加しました。

ありがとうございます。

答えて

3

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クラスのNotificationOpenedHandlerOneSignal.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を参照してください。

関連する問題