0
votes

I've tried to use setTimeout in Worklight adapter procedure. It doesn't work.

WLSE0099E: An error occurred while invoking procedure  [project BusinessBank]PushAdapter/submitNotificationFWLSE0100E:  parameters: [project BusinessBank]
ReferenceError: "setTimeout" is not defined. (PushAdapter-impl.js#37)

I need to hold sending the push notification after invoking adapter procedure. It's need for the demo. My code example:

WL.Server.createEventSource({
    name: 'PushEventSource',
    onDeviceSubscribe: 'deviceSubscribeFunc',
    onDeviceUnsubscribe: 'deviceUnsubscribeFunc',
    securityTest:'AngularStarter-strong-mobile-securityTest'
});

function deviceSubscribeFunc(userSubscription, deviceSubscription){}
function deviceUnsubscribeFunc(userSubscription, deviceSubscription){}

function submitNotification(userId, notificationText) {
    var userSubscription = WL.Server.getUserNotificationSubscription('PushAdapter.PushEventSource', userId);

    if (userSubscription == null) {
        return { result: "No subscription found for user :: " + userId };
    }

    var badgeDigit = 1,
        notification = WL.Server.createDefaultNotification(notificationText, badgeDigit, {custom: "data"});

    setTimeout(function() {
        WL.Logger.debug("submitNotification >> userId :: " + userId + ", text :: " + notificationText);
        WL.Server.notifyAllDevices(userSubscription, notification);
    },5000);

    return {
        result: "Notification sent to user :: " + userId
    };
}
2

2 Answers

1
votes

If you're actually about about JavaScript method setTimeout, it will help if you'll add some code example of what exactly you're trying to do, given this is a programming Q&A website and all.

If you're demoing your app using Worklight Studio, there is no need to implement a timeout.
Open the app, login, subscribe to notifications, close the app. Then, right-click on the adapter and select Run As > Invoke Worklight procedure and add a username in text (for example: "myuser","mytext"). And that's it... the notification will be sent. Whenever you want it to be sent.

Otherwise,
There is no such thing as setTimeout for an adapter procedure...

See here: How to increase the adapter procedure timeout value in Worklight?

To set a timeout to a procedure, in the adapter XML file:

<procedure name="nameHere" requestTimeoutInSeconds="valueHere"/>

Please review the IBM Worklight Knowledge Center and training modules.

0
votes

setTimeout API belongs to a global window object. basically using it the way you do is a shortcut to window.setTimeout().

Since adapter is not a browser but a server runnable code it doesn't have a global window object, therefore you have no setTimeout API.