urbanairship / android-samples

DEPRECATED - Use https://github.com/urbanairship/android-library instead
Other
33 stars 39 forks source link

Android: Application launches two time when click on push notification #21

Closed ShriKush closed 8 years ago

ShriKush commented 8 years ago

Hi,

We are using Urban Airship SDK for android, and using deep linking feature for the app. We have integrated SDK and able to send and receive push notifications via deep linking but when we click on push message our required activity is called according to our logic but when we click on back button our application launch again automatically.

NOTE: We have not used IntentReceiver class rather than using ParseDepLink activity for handling deep link push message.

Here is the code :

Code for manifest

 <activity android:name="com.nexgtv.kidstv.ParseDeepLinkActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>

                <!-- Handles any vnd.urbanairship.sample://deeplink URI's -->
                <data android:scheme="com.nexgtv.kidstv" android:host="deeplink" />

                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
            </intent-filter>
        </activity>

and code for ParseDeepLinkActivity:

public class ParseDeepLinkActivity extends Activity {
    public static final String PREFERENCE_DEEP_LINK = "/home/preferences";
    public static final String INBOX_DEEP_LINK = "/inbox/messages";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        if (intent == null || intent.getData() == null) {
            finish();
        }

        openDeepLink(intent.getData());

        // Finish this activity
        finish();
    }

    private void openDeepLink(Uri deepLink) {
        String path = deepLink.getPath();
        Log.i("path", path);

        if(path!=null && path.equalsIgnoreCase("/live")) {
//            List<String> list = deepLink.getPathSegments();
//            String message = getDeepLinkQueryParameter("channel_code");
//            String action = getDeepLinkQueryParameter("broadcast_type");
            String channel_code = getDeepLinkQueryParameter("channel_code");
            String broadcast_type = getDeepLinkQueryParameter("broadcast_type");
            String content_type = getDeepLinkQueryParameter("content_type");

            Bundle bundle = new Bundle();
            bundle.putString("message", "live");
            bundle.putString("action", "2");
            bundle.putString("value", channel_code);
            bundle.putString("ctype", broadcast_type);
            bundle.putString("play_st", content_type);

            Intent intent = new Intent(this, MainScreen.class);
//            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            intent.putExtras(bundle);
            startActivity(intent);
        }else if(path!=null && path.equalsIgnoreCase("/vod")){
            String channel_code = getDeepLinkQueryParameter("channel_code");
            String broadcast_type = getDeepLinkQueryParameter("broadcast_type");
            String content_type = getDeepLinkQueryParameter("content_type");

            Bundle bundle = new Bundle();
            bundle.putString("message", "vod");
            bundle.putString("action", "2");
            bundle.putString("value", channel_code);
            bundle.putString("ctype", broadcast_type);
            bundle.putString("play_st", content_type);

            Intent intent = new Intent(this, MainScreen.class);
//            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY);
            intent.putExtras(bundle);
            startActivity(intent);
        }

    }

    private String getDeepLinkQueryParameter(String key) {
        Intent intent = getIntent();
        if (intent != null && intent.getData() != null) {
            return intent.getData().getQueryParameter(key);
        }

        return null;
    }
}
rlepinski commented 8 years ago

Our actions framework run all actions after the SDK starts the launcher activity. To override that behavior you need to set an intent receiver and manually handle when to launch the activity or not. Thats the heavy handed solution, I think what you actually want is to just set Intent.FLAG_ACTIVITY_SINGLE_TOP on the intent you are starting in the deep link activity. That will not start the activity if its already at the top of the stack, instead it will just call the onNewIntent method.

rlepinski commented 8 years ago

I just tested this on the sample and it indeed launches the home activity twice. Moving finish() up in the parse deep link activity before i start the new activity fixes the issue. Maybe give that try?

ShriKush commented 8 years ago

Problem of relaunching occurs when i manually kill the application and then click on the push notification. Please suggest.

I tried finish() before start the new activity, but same problem occur.

rlepinski commented 8 years ago

Could you post updated code? What intent flags are you using when finishing the activity first?