-
-
Save milon120203/ed49e49ef9318f91084cf903b8e8b040 to your computer and use it in GitHub Desktop.
Push Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Require the module | |
var CloudPush = require('ti.cloudpush'); | |
var deviceToken = null; | |
// Initialize the module | |
CloudPush.retrieveDeviceToken({ | |
success: deviceTokenSuccess, | |
error: deviceTokenError | |
}); | |
// Enable push notifications for this device | |
// 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); | |
} | |
// Process incoming push notifications | |
CloudPush.addEventListener('callback', function (evt) { | |
alert("Notification received: " + evt.payload); | |
}); | |
var Cloud = require("ti.cloud"); | |
function subscribeToChannel () { | |
// Subscribes the device to the 'news_alerts' channel | |
// Specify the push type as either 'android' for Android or 'ios' for iOS | |
Cloud.PushNotifications.subscribeToken({ | |
device_token: deviceToken, | |
channel: 'news_alerts', | |
type: Ti.Platform.name == 'android' ? 'android' : 'ios' | |
}, function (e) { | |
if (e.success) { | |
alert('Subscribed'); | |
} else { | |
alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e))); | |
} | |
}); | |
} | |
function unsubscribeToChannel () { | |
// Unsubscribes the device from the 'test' channel | |
Cloud.PushNotifications.unsubscribeToken({ | |
device_token: deviceToken, | |
channel: 'news_alerts', | |
}, function (e) { | |
if (e.success) { | |
alert('Unsubscribed'); | |
} else { | |
alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e))); | |
} | |
}); | |
} | |
var win = Ti.UI.createWindow({ | |
backgroundColor: '#000', | |
layout:'vertical', | |
exitOnClose: true | |
}); | |
var subscribe = Ti.UI.createButton({title:'Subscribe', top: 20}); | |
subscribe.addEventListener('click', subscribeToChannel); | |
win.add(subscribe); | |
var unsubscribe = Ti.UI.createButton({title:'Unsubscribe', top: 20}); | |
unsubscribe.addEventListener('click', unsubscribeToChannel); | |
win.add(unsubscribe); | |
win.open(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment