Search code examples
javascriptangularjsionic-frameworkngcordovaonesignal

Routing to a state in app on notification click using OneSignal plugin in ionic


Here is my config file

(function () {
    'use strict';
    angular
    .module('app.core')
    .config(['$ionicConfigProvider', '$httpProvider', '$compileProvider', function ($ionicConfigProvider, $httpProvider, $compileProvider) {

        $ionicConfigProvider.tabs.position('bottom'); // other values: top
        $compileProvider.debugInfoEnabled(false); // remove while debugging

        $httpProvider.interceptors.push('authInterceptorService');
        document.addEventListener('deviceready', function ($state) {

            var notificationOpenedCallback = function (jsonData) {
                console.log('didReceiveRemoteNotificationCallBack: ' + JSON.stringify(jsonData));
            };
            window.plugins.OneSignal.init("xxxxx-xxxxxx-xxxxxx-xxxx",
            { googleProjectNumber: "xxxxxxxxx" },
            notificationOpenedCallback);


            window.plugins.OneSignal.getIds(function (ids) {
                var deviceId = ids;
                console.log(deviceId);
                localStorage.setItem('deviceId', deviceId.userId);
            });

            // Show an alert box if a notification comes in when the user is in your app.
            window.plugins.OneSignal.enableInAppAlertNotification(true);
        }, false);

    }]);

})();

The notificationOpenedCallback happens on click of a push message stacked on the device's tab .so i want to go to a particular route rather than just open the app which it does right now . In my notificationOpenedCallback function i want to be able to route to a particular state like

 .state('tabs.answered', {
        url: "/answered",
        views: {
            'tab-answered': {
                templateUrl: "app/answeredfeed/answered.html",
                controller: 'AnsweredCtrl',
                controllerAs: 'vm'
            }
        },
        authRequired: true
    });

Normally i would do a

$state.go('tabs.answered');

but the problem is i am unable to inject $state or $stateprovider so that i can route to tabs.answered . Is it even possible to route from config or have i got it all wrong ? Can this functionality be done some other way ?


Solution

  • Changed angular.config to angular.run and i was able to inject $injector and hence could do a

    var state = $injector.get($state);
    state.go('desiredstate');