1
votes

I'm using Push notification in my Worklight test app. I have made authentication via Worklight Adapter. But in this way, I always receive response from PushAdapter "No subscription found for user:: [sample_username]"

Device is authenticated:

enter image description here

And my test Android device is subscribed for notification.

enter image description here

My simple AuthAdapter:

function onAuthRequired(headers, errorMessage) {
    errorMessage =  errorMessage ? errorMessage : null;
    return {
        authRequired: true,
        errorMessage: errorMessage
    };
}
function submitAuthentication(username, password) {
    var userIdentity = {
            userId: username,
            displayName: username,
            attributes: {
                foo: "bar"
            }
    };
    WL.Server.setActiveUser("AdapterAuthRealm", null);
    WL.Server.setActiveUser("AdapterAuthRealm", userIdentity);

    return {
        authRequired: false
    };
}

Maybe, my code for Push notification service will be useful for help (Angular JS):

    console.log(services.service('PushService'));
    if (WL.Client.Push){

        WL.Client.Push.onReadyToSubscribe = function(){

            WL.Client.Push.registerEventSourceCallback(
                "myPush",
                "PushAdapter",
                "PushEventSource",
                angular.element(document.body).injector().get('PushService').pushNotificationReceived);
        };

    }

    services.service('PushService', function($q) {

        var self = this;
        self.send = function(userID, text) {
            console.info("PushService:: send ",userID, text);
            var deferred = $q.defer();
            if (typeof WL !== 'undefined') {
                WL.Client.invokeProcedure({
                    adapter : 'PushAdapter',
                    procedure : 'submitNotification',
                    parameters : [userID, text]
                },{
                    onSuccess: function(response) {
                        deferred.resolve(response);
                        console.log("PushService:: push response: ",response);
                    },
                    onFailure: function(response) {
                        deferred.reject();
                    }
                });
            } else {
                deferred.reject();
                console.warn("WL is undefined");
            }
            return deferred.promise;
        };

        self.isSupported = function() {
            var isSupported = false;
            if (WL.Client.Push){
                console.log(WL.Client.Push);
                isSupported = WL.Client.Push.isPushSupported();
            }
            return isSupported;
        };
        self.isSubscribed = function() {
            var isSubscribed = false;
            if (WL.Client.Push){
                isSubscribed = WL.Client.Push.isSubscribed('myPush');
            }
            return isSubscribed;
        };
        self.subscribe = function() {
            var deferred = $q.defer();
            WL.Client.Push.subscribe("myPush", {
                onSuccess: function(response) { console.info("Push:: subscribe success"); deferred.resolve(response); },
                onFailure: function() { console.warn("Push:: subscribe failture"); deferred.reject(); }
            });
            return deferred.promise;
        };
        self.unsubscribe = function() {
            var deferred = $q.defer();
            WL.Client.Push.unsubscribe("myPush", {
                onSuccess: function(response) { console.info("Push:: unsubscribe success"); deferred.resolve(response); },
                onFailure: function() { console.warn("Push:: unsubscribe failture"); deferred.reject(); }
            });
            return deferred.promise;
        };
        self.pushNotificationReceived = function(props, payload) {
            console.info("pushNotificationReceived invoked:: ", JSON.stringify(props), JSON.stringify(payload));
        };

        return self;
    });
1

1 Answers

0
votes

One possible scenario for your problem is that you are trying to send a push to user that the session had ended. For example, if you have the following scenario:

"serverSessionTimeout=3" at "conf\server\worklight.properties" (Session will be ended in 3 minutes)

And "apps\""\common\js\initOptions.js":

// # How often heartbeat request will be sent to Worklight Server
heartBeatIntervalInSecs: -1,

The mobile app will not send a heartbeat, so after your server timeout(Session will end) is like you were logout out of your app in the server-side. So, you become unsubscribed. This could be the cause of the fail in your push notifications.

A possible solution is to enable the heartbeat or increase the server-side session timeout.


It would be helpful if you could share also your configuration files: conf\server\worklight.properties and "apps\\common\js\initOptions.js" to check if is this the situation. Anyway, if this is the case, the first PUSH will after the login for 3 minutes and then after you would start to get the error.