Skip to content

Instantly share code, notes, and snippets.

@MotiurRahman
Last active January 1, 2020 17:26
Show Gist options
  • Save MotiurRahman/ba16a6f734815cb3565f54f15df30f78 to your computer and use it in GitHub Desktop.
Save MotiurRahman/ba16a6f734815cb3565f54f15df30f78 to your computer and use it in GitHub Desktop.
Push Test
// 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