How to open Mainactivity when receiving a Push Notification from Onesignal?

Asked

Viewed 23 times

0

Good morning community, first I wanted to thank the space and ask for your help with the following problem:

I have a function of a class that is called every time a notification is received which is the CordovaNotificationReceivedHandler and she is instantiated by Onesignal’s init according to the code below.

OneSignal.init(this.cordova.getActivity(),
              googleProjectNumber,
              appId,
              new CordovaNotificationOpenedHandler(notifOpenedCallbackContext),
              new CordovaNotificationReceivedHandler(notifReceivedCallbackContext)

      );

Trying to get my application reordered and displayed, even if the user is in another application, I made the following modifications:

OneSignal.init(this.cordova.getActivity(),
              googleProjectNumber,
              appId,
              new CordovaNotificationOpenedHandler(notifOpenedCallbackContext),
              new CordovaNotificationReceivedHandler(this.cordova.getActivity(), notifReceivedCallbackContext)

      );

In the body of the function that is called when receiving a push I added the following code:

Intent it = new Intent("intent.my.action");
      it.setComponent(new ComponentName(contextActivy.getPackageName(), MainActivity.class.getName()));
      it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      contextActivy.getApplicationContext().startActivity(it);
      this.handleActivityLifecycleHandler();

How the class stood after the modification:

private class CordovaNotificationReceivedHandler implements NotificationReceivedHandler {

    private CallbackContext jsNotificationReceivedCallBack;
    private Context contextActivy;

    public CordovaNotificationReceivedHandler(Context contextActivy, CallbackContext inCallbackContext) {
      jsNotificationReceivedCallBack = inCallbackContext;
      this.contextActivy = contextActivy;
    }

    @Override
    public void notificationReceived(OSNotification notification) {
      Log.w("Chegou","Aqui");

      Intent it = new Intent("intent.my.action");
      it.setComponent(new ComponentName(contextActivy.getPackageName(), MainActivity.class.getName()));
      it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      contextActivy.getApplicationContext().startActivity(it);
      this.handleActivityLifecycleHandler();

      try {
        CallbackHelper.callbackSuccess(jsNotificationReceivedCallBack, new JSONObject(notification.stringify()));
      }
      catch (Throwable t) {
        t.printStackTrace();
      }
    }
  }

The problem is that the application is not reordered to be displayed or shown or opened. Important to remember that I am assuming that the application will always be open.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.