Yahya Uddin
Yahya Uddin

Reputation: 28841

Titanium: Check iOS notifications upon opening the app without clicking the notification

I am trying to handle push notifications on iOS.

My simple code looks something similar to this:

var Cloud = require("ti.cloud");
var deviceToken = null;

var deviceToken = Ti.App.Properties.getString('deviceToken');

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
    ]
});

Ti.App.iOS.addEventListener('usernotificationsettings', function registerForPush() {
    Ti.App.iOS.removeEventListener('usernotificationsettings', registerForPush); 

    Ti.Network.registerForPushNotifications({
        success: function(e) {
            if (e.deviceToken !== Ti.App.Properties.getString('deviceToken', null)) {
                deviceToken = e.deviceToken;
                Ti.App.Properties.setString('deviceToken', deviceToken)
                subscribeToChannel();
            } else {
                Ti.API.info('Already registered for push notifications!');
            }
        },
        error: function(e) {
            Ti.API.error('Failed to register for push notifications: ' + e.error);
        },
        callback: receivePush
    });

});

function subscribeToChannel () {
    Cloud.PushNotifications.subscribeToken({
        device_token: deviceToken,
        channel: 'general',
        type: Ti.Platform.name == 'android' ? 'android' : 'ios'
    }, function (e) {
          alert(e.success === true ? 'Subscribed' : 'Error!');
    });
}

// When receieve interactive remote notification
Ti.App.iOS.addEventListener('remotenotificationaction', function(e) {
    alert('remotenotificationaction: ' + JSON.stringify(e));
});

// When receieve interactive notification in the background
Ti.App.iOS.addEventListener('localnotificationaction', function(e) {
    alert('localnotificationaction');
});

// When receieve interactive notification in the foreground 
Ti.App.iOS.addEventListener('notification', function(e) { 
    alert('notification');
});

function receivePush(e) {
    alert('receivePush');
}

For the most part everything works fine. The following happens when I send a remote push notification:

How can I make sure an event is triggered for the last case.

Upvotes: 0

Views: 243

Answers (1)

Carlos Zinato
Carlos Zinato

Reputation: 591

I don't think this is possible since your callback function is assigned with a notification behavior, not app starting. This is not a Titanium problem but a workflow misunderstanding if you know what I mean. I think for you is best to always check something when the app starts, not related to notifications.

Upvotes: 1

Related Questions