Open Specific Activity in OneSignal NOtification Click in Latest SDK Version

  android, android-studio, java, onesignal

I am a newbie in the android app development. I had recently upgraded my OneSignal SDK to latest Version which have a lot of code changes in it. I have fixed OneSignal initialization but my ApplicationClass is not working to open specific Activity when additional data is sent from OneSignal. Even the App is now not opening on Notification Click.

Here Below I have pasted my non working Code

import android.app.Application;
import android.content.Intent;
import android.net.Uri;
import android.os.CountDownTimer;
import android.widget.Toast;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.onesignal.OSNotificationAction;
import com.onesignal.OSNotificationOpenedResult;
import com.onesignal.OneSignal;

import org.json.JSONException;
import org.json.JSONObject;

public class ApplicationClass extends Application {
    String homeNative = "", mainNative = "", mainTopBanner = "", mainBottomBanner = "", mainIntersititial ="",teamIntersititial="",teamViewTopBanner="",teamViewBottomBanner="",newsTopBanner="",newsBottomBanner="",newsIntersititial="";
    Boolean showHomeNative = false, showMainNative = false, showMainTopBanner = false, showMainBottomBanner = false,showTeamIntersititial = false,showMainIntersititial=false,showTeamViewTopBanner=false,showTeamViewBottomBanner=false,showNewsTopBanner=false,showNewsBottomBanner=false,showNewsIntersititial=false;
    String type;
    String link,title;
    private static final String ONESIGNAL_APP_ID = "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx";
    RequestQueue requestQueue;
    @Override
    public void onCreate() {
        super.onCreate();
    // OneSignal Initialization
    OneSignal.initWithContext(this);
    OneSignal.setAppId(ONESIGNAL_APP_ID);
    OneSignal.setNotificationOpenedHandler(new NotificationOpenedHandler());
    Toast.makeText(ApplicationClass.this, "code run till here", Toast.LENGTH_SHORT).show();


}
public class NotificationOpenedHandler implements OneSignal.OSNotificationOpenedHandler
{
    @Override
    public void notificationOpened(OSNotificationOpenedResult result)
    {

        OSNotificationAction.ActionType actionType = result.getAction().getType();
        JSONObject data = result.getNotification().getAdditionalData();
        String launchURL = null;
        launchURL = result.getNotification().getLaunchURL();


        if (data != null)
        {
            type = data.optString("type", null);
            link = data.optString("link","https://google.com");
            title = data.optString("title","-");
            if (type != null && type.equals("News"))
            {
                getAdsDataFromAPI();
            }else if(launchURL != null){
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(launchURL));
                browserIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                browserIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);
                startActivity(browserIntent);

            }else{
                Intent intent = new Intent(ApplicationClass.this, SplashActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);
                startActivity(intent);
            }
        }else if(launchURL != null){
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(launchURL));
            browserIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            browserIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);
            startActivity(browserIntent);

        }else{
            Intent intent = new Intent(ApplicationClass.this, SplashActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);
            startActivity(intent);
        }
    }
}

Source: Android Questions

LEAVE A COMMENT