Skip to content

Instantly share code, notes, and snippets.

@hansemannn
Last active November 12, 2018 18:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hansemannn/62b67b384915d7e64c0a711b4afb1b73 to your computer and use it in GitHub Desktop.
Save hansemannn/62b67b384915d7e64c0a711b4afb1b73 to your computer and use it in GitHub Desktop.
Validate if the remote notifications dialog was accepted or not (workaround for a known Apple bug). More: https://jira.appcelerator.org/browse/TIMOB-25793
var pushRequestInitiated = false;
var deviceToken = null;
Ti.App.addEventListener('resume', function(e) {
Ti.API.warn('RESUME');
});
Ti.App.addEventListener('resumed', function(e) {
Ti.API.warn('RESUMED');
if (pushRequestInitiated) {
pushRequestInitiated = false
Ti.API.info('App moved to foreground again, checking if push declined silently!');
if (Ti.Network.remoteNotificationsEnabled) {
Ti.API.info('Push granted!')
} else {
Ti.API.error('Push not granted!')
}
}
});
Ti.App.addEventListener('pause', function(e) {
Ti.API.warn('PAUSE');
});
Ti.App.addEventListener('paused', function(e) {
Ti.API.warn('PAUSED');
if (pushRequestInitiated) {
Ti.API.info('App moved to background, probably showing auth dialog right now!');
}
});
var win = Ti.UI.createWindow({
backgroundColor: '#fff'
});
var btn = Ti.UI.createButton({
title: 'Trigger'
});
btn.addEventListener('click', checkPush);
win.add(btn);
win.open();
function checkPush() {
pushRequestInitiated = true;
// 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
]
});
// Process incoming push notifications
function receivePush(e) {
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); //this is the function that never gets thrown.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment