OneSignal push notification handle in onsen ui controller



  • i have problem with handling OneSignal pushNotification in onsen ui app. My purpose is open display page from any controllers, every page has specific controller. Whenever notification come will do same thing, open display page. My code is here

    index.js

    function onDeviceReady() {
      document.removeEventListener('deviceready', onDeviceReady, false);
      var notificationOpenedCallback = function(jsonData) {
      };
    
      window.plugins.OneSignal
        .startInit("xxxxxxxxxxxxxxxxxxx", "xxxxxxxxxxxxx")
        .handleNotificationOpened(notificationOpenedCallback)
        .endInit();           
    }
    

    stratcontroller.js

    app.controller('startController', function ($scope, $http) {
    
     var notificationOpenedCallback = function (jsonData) {
        $scope.myNavigator.pushPage('form/displaypage.html'); // this is what i want
    
     };
    
     window.plugins.OneSignal //get error : Uncaught TypeError: Cannot read property 'OneSignal' of undefined
        .startInit("xxxxxxxxxxxxxxxxxxx", "xxxxxxxxxxxxx)
        .handleNotificationOpened(notificationOpenedCallback)
        .endInit();
    });
    

    how to handle notification from any controllers?thank you



  • @lapisanlangit The error is saying that it can’t find the plugin. Are you using Monaca debugger or are you actually building the package for use on the device?



  • thank you @munsterlander, I actually building the package for use on real the device. that’s eror because I use chrome browser, I don’t know what is the true error when I used device… but the page has used startController is not display well. if I put this code in another page with another controller for example calendarController

    app.controller('calendarController', function ($scope, $http) {
    
     var notificationOpenedCallback = function (jsonData) {
        $scope.myNavigator.pushPage('form/displaypage.html'); // this is what i want
    
     };
    
     window.plugins.OneSignal 
        .startInit("xxxxxxxxxxxxxxxxxxx", "xxxxxxxxxxxxx)
        .handleNotificationOpened(notificationOpenedCallback)
        .endInit();
    });
    

    the calendar page also not display well (e.g. button is missing etc). What is the best practice to handle notfication using cordova?



  • @lapisanlangit Do you want local notifications or remote?



  • i don’t understand…local/remote? are you have example?



  • @lapisanlangit What I mean is, the best Cordova plugin for local (non-server initiated) notifications is Katzer’s: https://github.com/katzer/cordova-plugin-local-notifications

    If you want remote notifications, meaning push notifications from a server, why not use Monaca? https://docs.monaca.io/en/reference/monaca_api/cloud_management/push/#push-send-send-push-notification


  • Onsen UI

    If you always want to open the same page with the same navigator then you don’t need to do it in every controller. myNavigator is also global so you can do it this way:

    ons.ready(function() {
        window.plugins.OneSignal 
            .startInit("xxxxxxxxxxxxxxxxxxx", "xxxxxxxxxxxxx)
            .handleNotificationOpened(function() {
                window.myNavigator.pushPage('form/displaypage.html');
            })
            .endInit();
    });


  • @Fran-Diox wow your answer is perfect. I don’t know about myNavigator before, i think must be placed in every controller page. thank you very much…


Log in to reply