Search code examples
androidtitaniumpubnubcommonjsandroid-push-notification

App in background doesn't receive message from pubnub for android in Titanium


I am using pubnub for push message from server for android. I get the message when app is in foreground. But i didn't get message if app is inactive in background mode or in locked mode. How can I get the pubnub message if the app is in background or not in the recent app tray ? Please help me out. Thanks.

var pubnub = new PubNub({
        subscribeKey : 'sub-key',
        publishKey : 'pub-key'
    });

    pubnub.addListener({
        status : function(st) {
            if (st.category === "PNConnectedCategory") {

            }
        },
        message : function(m) {

            var pushStatus = m.message;
            console.log("Show Notification");

        },
        presence : function(ps) {
            console.log(ps);
        }
    });

    pubnub.subscribe({
        channels : ['Channel']
    });

Solution

  • PubNub Android Background Use Case

    When in background you do not have connection. It used to be possible to just run your app in a background service that never gets destroyed with some additional config with the Wake Lock and permission from the device owner, but I believe this is not possible with non-native apps (perhaps using React-Native but probably not Titanium, but I am not certain of that).

    The typical solution is to use mobile push notifications (FCM, formerly known as GCM) when the app is background or not running at all (app has been force killed by user or system). You can provide an FCM payload (and APNS for iOS clients) when you publish a message.

    See full docs for PubNub Titanium SDK Mobile Push Notifications. Each SDK has this same section for anyone that needs same for other languages/platforms.