Van Coding
Van Coding

Reputation: 24534

Why doesn't callback get called when the app is in background?

I'm developing a titanium app that needs to display a Banner Message under iOS when a push notification comes in. Therefore I used the following code to register on incoming push notifications:

var callbacks = {
    types: [
        Titanium.Network.NOTIFICATION_TYPE_BADGE,
        Titanium.Network.NOTIFICATION_TYPE_SOUND,
        Titanium.Network.NOTIFICATION_TYPE_ALERT
    ],
    success:function(e){
        console.log("success");
    },
    error:function(e){
        console.log("error");
    },
    callback: function(e){
        console.log("new push notification")
        //code for displaying banner message would go here!
    }
};

if(Ti.App.iOS.registerUserNotificationSettings){ //iOS 8 +
    function onUserNotificationSettings(){
        delete callbacks.types;
        Ti.Network.registerForPushNotifications(callbacks);
        Ti.App.iOS.removeEventListener("usernotificationsettings",onUserNotificationSettings);
    }
    Ti.App.iOS.addEventListener("usernotificationsettings",onUserNotificationSettings)
    Ti.App.iOS.registerUserNotificationSettings(callbacks)
}else{ //up to iOS 7
    Ti.Network.registerForPushNotifications(callbacks)
}

But the callback function does not get called when the app is in background. So, I also can't display the banner message there, since the code won't get executed.

What could be the reason why the callback does not get called when the app is in background? When it is in foreground, it works perfectly. Is it normal? If yes, where else would I put my code to display the banner message?

I'm using SDK version 3.4.0 on an iPhone 5 with iOS 8.1.1

Please note that sending the banner text through the apn-payload is not the solution. There are other usecases. For example, when the server needs to tell the client that there is new content to sync, where the user does not even need to get notified for. The client should just download the new content in background just when the notification arrives.

Upvotes: 0

Views: 1039

Answers (2)

Van Coding
Van Coding

Reputation: 24534

I've found out how to do it! The callback will get called when the app is in background. All I had to do for it was to add the following to my tiapp.xml in ti:app/ios/plist/dict:

<key>UIBackgroundModes</key>
<array>
    <string>remote-notification</string>
</array>

After that, everything works fine!

Upvotes: 1

Mike Gottlieb
Mike Gottlieb

Reputation: 966

You need to register for the remote-notification background mode. This will wake up your app and give you execution time when you send the notifications.

For the record this is in the Appcelerator docs here

Upvotes: 3

Related Questions