Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

Push sent in-app not received when app has been closed

$
0
0

Hi,

I'm working with Appcelerator's cloud service and am having some issues with push notifications for iOS.

Sending a push to channel X using the web service (https://cloud.appcelerator.com/apps/...) works flawlessy; the units subscribed to channel X receive the push, regardless of whether they have the app open or closed.

However:

Push notifications sent in-app using notifyTokens are received only if the subscribed unit(s) haven't closed the app since they subscribed.

Examples

User A and User B open the app, are logged in (as admins) and subscribe to channel X.

Case 1

While they both still have the app open, user A triggers notifyTokens.

Result: User B receives a push. (Works)

Case 2:

User A and User B close the app. Some time later, User A opens the app again, is logged in (as admin) and triggers notifyTokens.

Result: no push is received. (Doesn't work)

Case 3:

User A and User B close the app. Some time later, someone sends a push using the webservice (https://cloud.appcelerator.com/apps/...)

Result: User A and User B receive the push. (Works)

Any ideas on how to make it so that a push sent in-app is received even though the subscribed unit(s)'s app is closed?

Code:

var deviceToken = null;
 
Ti.Network.registerForPushNotifications({
    // Specifies which notifications to receive
    types: [
        Ti.Network.NOTIFICATION_TYPE_BADGE,
        Ti.Network.NOTIFICATION_TYPE_ALERT,
        Ti.Network.NOTIFICATION_TYPE_SOUND
    ],
    success: deviceTokenSuccess,
    error: deviceTokenError,
    callback: receivePush
});
// Process incoming push notifications
function receivePush(e) {
    Titanium.Media.vibrate();
    alert('Received push: ' + JSON.stringify(e));
}
// Save the device token for subsequent API calls
function deviceTokenSuccess(e) {
    deviceToken = e.deviceToken;
}
 
function deviceTokenError(e) {
    alert('Failed to register for push notifications! ' + e.error);
}
 
// Require in the Cloud module
var Cloud = require("ti.cloud");
 
function cloudLogin () {
    Cloud.Users.login({
        login : 'testuser', //global admin that has already been created
        password : 'password'
    }, function(e) {
        if (e.success) {
            var user = e.users[0];
            alert('Success:\n' + 'id: ' + user.id);
        } else {
            alert('Error:\\n' + ((e.error && e.message) || JSON.stringify(e)));
        }
    });
}
 
 
function subscribeToChannel () {
    // Subscribes the device to the 'test' channel
    // Specify the push type as either 'android' for Android or 'ios' for iOS
    Cloud.PushNotifications.subscribeToken({
        device_token: deviceToken,
        channel: 'test',
        type: Ti.Platform.name == 'android' ? 'android' : 'ios'
    }, function (e) {
        if (e.success) {
            alert('Subscribed');
            cloudLogin();
        } else {
            alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
        }
    });
}
 
function sendNotification () {
    // Sends an 'This is a test.' alert to specified device if its subscribed to the 'test' channel.
    Cloud.PushNotifications.notifyTokens({
        to_tokens: 'everyone',
        channel: 'test',
        payload: 'Test-test, ' + new Date()
    }, function (e) {
        if (e.success) {
            alert('Push notification sent');
        } else {
            alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
        }
    });
}
 
var win = Ti.UI.createWindow({
    backgroundColor: 'white',
    //layout:'vertical',
    exitOnClose: true
});
 
var subscribe = Ti.UI.createButton({
    title:'Subscribe',
    font:{fontFamily: 'HelveticaNeue-Light'},
    top: 80    
});
subscribe.addEventListener('click', subscribeToChannel);
win.add(subscribe);
 
var notify = Ti.UI.createButton({
    title:'Notify',
    font:{fontFamily: 'HelveticaNeue-Light'},
    bottom: 80
});
notify.addEventListener('click', sendNotification);
win.add(notify);
 
win.open();
Platform: iOS 7 Titanium SDK: 3.2.0

Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>