0

The following code serves as my handler when the user taps a push notification. The code is working properly and launchURL is accurately recorded in my log. However, as a next step I would like to direct the user to an activity called ViewPushLink. I have attempted to follow similar instructions found elsewhere on S.O., but as a newbie, I would appreciate some specific help.

class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
     public static String launchURL;

    @Override
    public void notificationOpened(OSNotificationOpenResult result) {
        OSNotificationAction.ActionType actionType = result.action.type;
        JSONObject data = result.notification.payload.additionalData;

        if (data != null) {
            launchURL = data.optString("launchURL");
            if (launchURL != null) {
                Log.i("OneSignalExample", "launchURL value: " + launchURL);

            }
        }

        }

}

EDIT: ExampleNotificationOpenedHandler is called from my Main Activity as show below:

public class MainActivity extends AppCompatActivity {


       private ProgressDialog progress;

    public static boolean isNetworkStatusAvailable (Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null)
        {
            NetworkInfo netInfos = connectivityManager.getActiveNetworkInfo();
            if(netInfos != null)
                if(netInfos.isConnected())
                    return true;
        }
        return false;
    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
        if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        } else setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        // OneSignal Initialization
        OneSignal.startInit(this)
                .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
                .unsubscribeWhenNotificationsAreDisabled(true)
                .setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
                .init();
jumpingmaniac
  • 107
  • 2
  • 13
  • https://stackoverflow.com/questions/41863680/onesignal-android-notificationopenedhandler-start-activity check this out. – NIKHIL MAURYA Oct 26 '18 at 17:08

2 Answers2

0

Change your Activity to this; Surely it will help you.

class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
     public static String launchURL;

    @Override
    public void notificationOpened(OSNotificationOpenResult result) {
        OSNotificationAction.ActionType actionType = result.action.type;
        JSONObject data = result.notification.payload.additionalData;

        if (data != null) {
            launchURL = data.optString("launchURL");
            if (launchURL != null) {
                Log.i("OneSignalExample", "launchURL value: " + launchURL);
                Intent intent = new Intent(getApplicationContext, ViewPushLink.class)
                startActivity(intent);
            }
        }

        }

}
0

Put your code in the Application Class of your project.
For push notification handling using the OneSignal SDK, three things need to be considered.{if your project has multiple fragments and one Main Activity(Navigation Drawer Activity) and rest of other activities get called from the fragments of MainActivity}

  1. Target Activity
  2. Related Actions like, which part of your activity, which fragment you want to open, differentiate which each with some action that you can find in the jsonObject Data
  3. Intent Flags, an example you want to open an Activity, whose instance is already in backStack, so you may want to clear the back stack and open the new instance of the activity or may open the same instance.

Example:

public XYZApplication extends Application{

class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
 public static String launchURL;
@Override
public void notificationOpened(OSNotificationOpenResult result) {
    OSNotificationAction.ActionType actionType = result.action.type;
    JSONObject data = result.notification.payload.additionalData;

        if (data != null) {
            String target = data.optString("target");
            String action = "";
            switch (target) {
                case "xyz":
                    action = data.optString("action");
                    Utils.setTarget(target);
                    if (action.equalsIgnoreCase("xyz")
                            || action.equalsIgnoreCase("abc")) {
                        Intent xyzIntent = new Intent(getApplicationContext(), XYZActivity.class);
                        homeIntent.putExtra("action_key", "action_xyz");
                        homeIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(xyzIntent);
}

for more details refer to one signal SDK Documentation https://documentation.onesignal.com/docs

Kousei
  • 1,291
  • 1
  • 9
  • 16