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

Push Notification Callback not firing when app is opened.

$
0
0
  • OS X 10.10.3
  • Appcelerator Studio 4.0.1.201506022004
  • iOS 8.3
  • Ti SDK 4.0.0RC2

I am trying to display push notifications when the app is opened but nothing is happening. I do receive push notifications when the app is closed and in the background.

I have setup the below push notification register code on my login screen. When the user registers and opt's in for communication this flags that they might want to receive push notifications. When they login for the 1st time after registering then they are prompted to allow push notifications.

You can see that the receivePush function is included.

var deviceToken = null;
 
// Check if the device is running iOS 8 or later
if (Ti.Platform.name == "iPhone OS" && parseInt(Ti.Platform.version.split(".")[0]) >= 8) {
 
    // Wait for user settings to be registered before registering for push notifications
    Ti.App.iOS.addEventListener('usernotificationsettings', function registerForPush() {
 
        // Remove event listener once registered for push notifications
        Ti.App.iOS.removeEventListener('usernotificationsettings', registerForPush); 
 
        Ti.Network.registerForPushNotifications({
            success: deviceTokenSuccess,
            error: deviceTokenError,
            callback: receivePush
        });
    });
 
    // Register notification types to use
    Ti.App.iOS.registerUserNotificationSettings({
        types: [
            Ti.App.iOS.USER_NOTIFICATION_TYPE_ALERT,
            Ti.App.iOS.USER_NOTIFICATION_TYPE_SOUND,
            Ti.App.iOS.USER_NOTIFICATION_TYPE_BADGE
        ]
    });
}
 
// For iOS 7 and earlier
else {
    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) {
    alert('Received push: ' + JSON.stringify(e));
}
 
// Enable push notifications for this device
// Save the device token for subsequent API calls
function deviceTokenSuccess(e) {
    alert('Got Device Token : '+e.deviceToken);
    deviceToken = e.deviceToken;
    Ti.App.Properties.setString('dev_token', deviceToken);
    Ti.App.Properties.setString('pushNotificationsList', '1');
    subscribeToChannel();
}
function deviceTokenError(e) {
    alert('Failed to register for push notifications! ' + e.error);
}
I've also added the receivePush function shown below to my app.js file to catch incoming pushes when the app is open. I placed similar code here in my android app and this worked. I added receivePush(); as this called the function because nothing was happening before I added this. Now when the app starts it displays this alert alert('Received push: ' + JSON.stringify(e)); stating its undefined but nothing happens when I send a push.
var json;
 
// Process incoming push notifications
function receivePush(e) {
        //alert('Received push: ' + JSON.stringify(e)); 
    json = JSON.stringify(e);
    if (json != undefined) {
        Titanium.Media.vibrate([0, 1000]);
        var sound = Titanium.Media.createSound();
        sound.url = 'Horn.mp3';
        sound.play();   
        var push_msg = Ti.UI.createAlertDialog({
            title: json.aps.title,
            message:json.aps.alert,
            buttonNames: ['OK']
        });
        push_msg.show();
        Ti.API.info('Push Function Read');              
    }
    Ti.API.info('Push Function Not Read');      
}
receivePush();
Anybody got any ideas why I am not receiving them while the app is open?

Viewing all articles
Browse latest Browse all 8068

Trending Articles



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