Last active
December 30, 2015 08:19
-
-
Save MotiurRahman/7802287 to your computer and use it in GitHub Desktop.
Android and IOS: Push Notification without user login
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
/* Hi Here is the push notification sample code if you installing this apps code in your device when send any push | |
notification message from ACS web it will show your notification bar if you loggedin in your apps*/ | |
var Cloud = require("ti.cloud"); | |
function loginUser() { | |
// Log in to ACS | |
Cloud.Users.login({ | |
login : '[email protected]', | |
password : '1234' | |
}, function(e) { | |
if (e.success) { | |
alert('Login successful'); | |
} else { | |
alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e))); | |
} | |
}); | |
}; | |
loginUser(); | |
var win = Ti.UI.createWindow({ | |
backgroundColor : 'white', | |
layout : 'vertical', | |
exitOnClose : true | |
}); | |
var CloudPush = null; | |
var deviceToken = null; | |
var CloudPush = require('ti.cloudpush'); | |
var subscribe = Ti.UI.createButton({ | |
title : 'Subscribe', | |
top : 20, | |
height : Ti.UI.SIZE, | |
width : 160 | |
}); | |
subscribe.addEventListener('click', subscribeToChannel); | |
win.add(subscribe); | |
var notify = Ti.UI.createButton({ | |
title : 'Notify', | |
top : 20, | |
height : Ti.UI.SIZE, | |
width : 160 | |
}); | |
notify.addEventListener('click', sendTestNotification); | |
win.add(notify); | |
var unsubscribe = Ti.UI.createButton({ | |
title : 'Unsubscribe', | |
top : 20, | |
height : Ti.UI.SIZE, | |
width : 160 | |
}); | |
unsubscribe.addEventListener('click', unsubscribeToChannel); | |
win.add(unsubscribe); | |
var isIos = (Ti.Platform.osname === 'iphone' || Ti.Platform.osname === 'ipad'); | |
if (isIos) { | |
Ti.Network.registerForPushNotifications({ | |
// Specifies which notifications to receive | |
types : [Ti.Network.NOTIFICATION_TYPE_BADGE, Ti.Network.NOTIFICATION_TYPE_ALERT, Ti.Network.NOTIFICATION_TYPE_SOUND], | |
success : deviceTokenSuccess, | |
error : deviceTokenError, | |
callback : receivePush | |
}); | |
// Process incoming push notifications | |
function receivePush(e) { | |
Ti.API.info('Received push: ' + JSON.stringify(e)); | |
} | |
} else { | |
CloudPush.retrieveDeviceToken({ | |
success : deviceTokenSuccess, | |
error : deviceTokenError | |
}); | |
// Process incoming push notifications | |
CloudPush.addEventListener('callback', function(evt) { | |
Ti.API.info(evt.payload); | |
}); | |
// Triggered when the push notifications is in the tray when the app is not running | |
CloudPush.addEventListener('trayClickLaunchedApp', function(evt) { | |
Ti.API.info('Tray Click Launched App (app was not running)'); | |
}); | |
// Triggered when the push notifications is in the tray when the app is running | |
CloudPush.addEventListener('trayClickFocusedApp', function(evt) { | |
Ti.API.info('Tray Click Focused App (app was already running)'); | |
}); | |
} | |
// Enable push notifications for this device | |
// Save the device token for subsequent API calls | |
function deviceTokenSuccess(e) { | |
deviceToken = e.deviceToken; | |
Ti.API.info('Sucess ' + e.deviceToken); | |
//alert('this is device tolen='+e.deviceToken); | |
} | |
function deviceTokenError(e) { | |
alert('Failed to register for push notifications! ' + e.error); | |
} | |
function subscribeToChannel() { | |
// Subscribe the user and device to the 'test' channel | |
// Specify the push type as either 'android' for Android or 'ios' for iOS | |
Cloud.PushNotifications.subscribe({ | |
channel : 'test', | |
device_token : deviceToken, | |
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 sendTestNotification() { | |
// Sends 'This is a test.' alert to any device subscribed to the 'test' channel. | |
Cloud.PushNotifications.notify({ | |
channel : 'test', | |
payload : 'This is a test.' | |
}, function(e) { | |
if (e.success) { | |
alert('Push notification sent'); | |
} else { | |
alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e))); | |
} | |
}); | |
}; | |
function unsubscribeToChannel() { | |
// Unsubscribes the user and device from the 'test' channel | |
Cloud.PushNotifications.unsubscribe({ | |
channel : 'test', | |
device_token : deviceToken, | |
}, function(e) { | |
if (e.success) { | |
alert('Unsubscribed'); | |
} else { | |
alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e))); | |
} | |
}); | |
} | |
win.open(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment