Last active
February 17, 2017 22:25
-
-
Save MotiurRahman/68bb7aad307901805b5f to your computer and use it in GitHub Desktop.
PushTest
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
var Cloud = require("ti.cloud"); | |
var deviceToken = null; | |
if (Ti.Platform.osname === "android") { | |
var CloudPush = require('ti.cloudpush'); | |
// 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) { | |
Ti.API.info('Failed to register for push notifications! ' + e.error); | |
} | |
// Process incoming push notifications | |
CloudPush.addEventListener('callback', function(evt) { | |
Ti.API.info("Notification received: " + evt.payload); | |
}); | |
} else { | |
if ((Ti.Platform.osname === 'iphone' || Ti.Platform.osname === 'ipad') && parseInt(Ti.Platform.version.split(".")[0]) >= 8) { | |
// 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 iOS 7 and earlier | |
else { | |
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) { | |
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); | |
} | |
} | |
function subscribeToChannel() { | |
// Specify the push type as either 'android' for Android or 'ios' for iOS | |
Cloud.PushNotifications.subscribe({ | |
channel : 'friend_request', | |
device_token : deviceToken, | |
type : Ti.Platform.name == 'android' ? 'android' : 'ios' | |
}, function(e) { | |
if (e.success) { | |
alert('Success'); | |
} else { | |
alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e))); | |
} | |
}); | |
} | |
//For using the user login API, you have to create a user to the dashboard or here then login with that credential. | |
Cloud.Users.login({ | |
login : 'Email', | |
password : 'password' | |
}, function(e) { | |
if (e.success) { | |
var user = e.users[0]; ('Success:\n' + 'id: ' + user.id + '\n' + 'sessionId: ' + Cloud.sessionId + '\n' + 'first name: ' + user.first_name + '\n' + 'last name: ' + user.last_name); | |
subscribeToChannel(); | |
} else { | |
Ti.API.info('Error:\n' + ((e.error && e.message) || JSON.stringify(e))); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment