Detect if Appcelerator app was opened from a push notification

192 views Asked by At

I am trying to determine the best way to detect if my app was opened by tapping on a push notification. I have found some different articles online that suggest different way to do this, but none seem to work in a consistent manner across app platforms and states.

Is there an official Appcelerator way to do this?

1

There are 1 answers

0
Sebastian On

Yes, there is an official way.

if (Ti.Platform.getOsname() !== 'android') {

    // 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 Android
else {
    var CloudPush = require('ti.cloudpush');
    var deviceToken = null;

// Initialize the module
    CloudPush.retrieveDeviceToken({
        success: deviceTokenSuccess,
        error: deviceTokenError
    });

    // Process incoming push notifications
    CloudPush.addEventListener('callback', function (evt) {
        receivePush(evt);
    });
}

// Process incoming push notifications
function receivePush(e) {
    // alert(e.data);
}