× Requests and support related to jBackend.

jBackend notification issue - need help

  • jail_10
  • Topic Author
  • Offline
  • New Member
  • New Member
More
8 years 9 months ago #5044 by jail_10
Greetings. So I have a Joomla 3.x server with the jBackend plugin installed.
I have created an Ionic - Cordova application that reads via REST from the Joomla database.
I want to add GCM notifications support ; basically I want to send a notification to all users that have registered their device when a new article has been created. (I know that I will need to develop my own functionality for this, since jBackend does not support it yet).
The problem is, I can't seem to be able to make the notifications work. This is what I did.

Created new google project :

1) I have created a new project in the google console.
2) Have enabled cloud messaging.
3) Have added credentials, and created a new server api key.

In the jBackend admin area :

1) I have created a new application - filled the following fields :
  • Code : simpleString
  • Description : simpleString
  • Project ID : the project id that google gave me after I created the project
  • Project number : the project number that google gave me after I created the project
  • Api key : the server api key that I generated from google credentials
  • SSL certificate : left empty
  • Password : left empty

(I assume the last two are for Apple only ?)

2) In the plugin settings, at notifications

Under plugin tab:
  • Auto app registration: NO
  • JSON register : YES
  • Batch size : 10

Under Android tab:
  • GCM URL : gcm-http.googleapis.com/gcm/send (I think it's the default)
  • API key : The same API key I added in the 'Api key' field when I created the app in the jBackend component view

The application itslef :

1) Added the following plugin : phonegap-plugin-push, using this command :
phonegap plugin add phonegap-plugin-push --variable SENDER_ID="XXXXXXX", where sender_id is the Project ID. (At least I think it is; that's what I found out from my research).

2) In the run phase (angular), added the following code

(Will display only the code related to the notifications)
     var acieeApp = angular.module('starter', ['ionic', 'starter.services', 'starter.controllers'])

    .run(runFunction)

    .constant('APP_URL', production ? productionAPIEndpoint : developmentAPIEndpoint)

    .constant('APP_CODE', APP_CODE)

    .config(configFunction);

In the runFunction :
[...]
$ionicPlatform.ready(function () {
        // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
        // for form inputs)
        if (window.cordova && window.cordova.plugins.Keyboard) {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        }
        if (window.StatusBar) {
            StatusBar.styleDefault();
        }

        // # Push notifications registering
        var push = PushNotification.init({
            "android": {"senderID": "3170366007"}
        });
        push.on('registration', function (data) {
            // data.registrationId
            if(data.registrationId && typeof data.registrationId == 'string') {
                GcmService.registerClient(data.registrationId, function(data) {
                    // # Calling the scheduler function and see how things go.
                    triggerSchedulerInterval();
                }, function(status) {
                    console.error('Something happened while trying to register the application via GCM !', status, new Date());
                });
            }
        });
        push.on('notification', function (data) {
            // # So after calling the interval on a regular basis, if the server has some data to be sent,
            // # this event will catch said data.
            console.warn('RECEIVED NOTIFICATION MOFOOOOO !', data);
        });

        push.on('error', function (e) {
            // e.message
        });
    });
[...]

The GCM service :
(function() {

    angular.module('starter.services').factory('GcmService', GcmService);

    GcmService.$inject = ['$http', 'APP_URL', 'APP_CODE'];

    function GcmService($http, APP_URL, APP_CODE) {

        var schedulerUrl = APP_URL + '/get/push/scheduler';

        return {

            registerClient : function(token, successCallback, errorCallback) {
                $http.get(APP_URL + '/put/push/register?token=' + token + '&appcode=' + APP_CODE + '&platform=android')
                    .success(function (data, status, headers, config) {
                        console.info('Registered application GCM', data);
                        successCallback(data);
                    })
                    .error(function (data, status, headers, config) {
                        errorCallback(status);
                    });
            },

            triggerScheduler : function(successCallback, errorCallback) {
                $http.get(schedulerUrl)
                    .success(function (data, status, headers, config) {
                        console.info('Scheduler triggered successfully', data);
                        successCallback(data);
                    })
                    .error(function (data, status, headers, config) {
                        errorCallback(status);
                    });
            }

        }

    }

}).call(this);

Basically, the code appears to work. After the ionic application is done loading, I initialize PushNotification and store it in the push variable. The on registration event fires, and I receive a JSON similar to this :
{  
   "status":"ok",
   "token":"cdcO3HxffCg:APA91bH95os9ndAnYrSeMXwkMcYUzp66820klAx7EFZ4M0lF6A_xqnF_41SrfbiAHFFaCrJW9jLbzUuhUlXiGcv3iXGgttPeoePvpYHEIVBbo9sDBr888Y24IrpwI7NS6BV_CaazZ-_E",
   "appcode":"aciee001",
   "platform":"android",
   "platform_code":1,
   "app_id":"2",
   "device_id":11,
   "ios_alert":0,
   "ios_badge":0,
   "ios_sound":0
}

I use the token field to register the application using the register route. (see registerClient function in the GCM service), and the application does register. I can see it listed in the jbackend.

A question here : the documentation seems to be a bit ambiguous here (at least for me, sorry):

To start processing the pending notifications (with status scheduled and running) it is necessary to call a "trigger" function. The Push Module has a scheduler function that must be called on a regular basis (e.g. from a crontab) to trigger the sending of scheduled push notifications.


Basically I need to call a function from the client, that makes a call to the schedule route once every n seconds / minutes ? Basically I need to poll the server, if I understood correctly ?

Then I go back to joomla, and I create a new notification. In the console, I can see I receive
{status: "ok", batch_size: 10, sent: 1, success: 0, failure: 1}
. Been struggling with this for quite a while. I do not see where I am making a mistake.
Any help would be greatly appreciated. Thank you !

Please Log in or Create an account to join the conversation.

More
8 years 9 months ago - 8 years 9 months ago #5047 by admin
Replied by admin on topic jBackend notification issue - need help
It is all right except the part you put the "scheduler" call into the app. Absolutely don't do it. But first let me explain what the scheduler is for, and it will be clear that you don't neet to put it in the app, but in a crontab on the serve side or similar (e.g. an online crontab service).

When you create a notification, this is ready to go out, but is still in "pending" status. To start the sending process you must "push a button", and the button is the schedule call. This is the reason for:

To start processing the pending notifications (with status scheduled and running) it is necessary to call a "trigger" function. The Push Module has a scheduler function that must be called on a regular basis (e.g. from a crontab) to trigger the sending of scheduled push notifications.


So if you call the scheduler, as example, every 5 minutes, you will receive pending notifications within 5 minutes from their creation.

The sending process is a batch job, and you need to call this batch from time to time (e.g. with a crontab). This is a cron example which calls scheduler every 10 minutes:
0,10,20,30,40,50 * * * * wget http://www.yoursite.com/endpoint/get/push/scheduler?api_key=123456

Or every for 5 minutes:
*/5 * * * * wget http://www.yoursite.com/endpoint/get/push/scheduler?api_key=123456

It could be a good idea to protect the call with an api key enabled endpoint.

Kind regards,
Luigi
Last edit: 8 years 9 months ago by admin.
The following user(s) said Thank You: jail10

Please Log in or Create an account to join the conversation.

More
8 years 9 months ago - 8 years 9 months ago #5048 by jail10
Replied by jail10 on topic jBackend notification issue - need help
Greetings Luigi !
I do understand now how it works; thank you for the clear explanation.
However, I still don't understand one thing : sure, it is not a good idea to call the scheduler from the mobile application - and now that I think about it , it was quite stupid of me to do that sort of thing - however, the fact still stands : the call to the scheduler was successful (even if called from within a browser) - and jBackend reacted to it; sending me a result back; even though it was a failed one. Does it have some sort of a condition that rejects calls made from a browser, hence sending the failed message ? If so, would it not be a good idea to send the reason of it failing ? (just a small suggestion - even though, if I think about it, it would be quite a security issue, giving useful information back to the caller).

Thanks again ! Will try to do make an daemon with a cron job, calling the scheduler once every 5 minutes

(* GOD ! If I had 5000 clients making calls to the same api call every 10 seconds the server would have gone insane ! So dumb of me ! But still, the above question stands ^_^)
Last edit: 8 years 9 months ago by jail10.

Please Log in or Create an account to join the conversation.

More
8 years 9 months ago #5049 by jail10
Replied by jail10 on topic jBackend notification issue - need help
Ok, I have made the call from the server, via a crontab - once every 5 minutes, as you said. In the jbackend, I can see the notification I create changes status to `Completed`, however, no notification (and no error or any other sign of life) on the mobile app. I wonder what I am doing wrong.

Please Log in or Create an account to join the conversation.

More
8 years 9 months ago #5050 by jail10
Replied by jail10 on topic jBackend notification issue - need help
Ohhh ! After further struggling, and a couple of logs on the server, I realised that the call made via curl in the schedule function received `401 Unauthorised`.
After validating the domain on google console, jBackend was able to make a successful call on the server. So I do receive successes and not failures.

The problem is , I still do not receive the notifications. I suppose it has to do with the fact that GCM cannot map my application, so it doesn't know where to send it. Weird, because I added the project id code in the application. Hmm.

Please Log in or Create an account to join the conversation.

More
8 years 9 months ago #5051 by jail10
Replied by jail10 on topic jBackend notification issue - need help
Yeah, it's working now after the last changes. Maybe it will help someone.

Thanks Luigi !

Please Log in or Create an account to join the conversation.

Time to create page: 0.184 seconds