Skip to content

Instantly share code, notes, and snippets.

@samueleastdev
Last active November 28, 2017 23:57
Show Gist options
  • Select an option

  • Save samueleastdev/e05fc226b9adcdd84698aee017194e9f to your computer and use it in GitHub Desktop.

Select an option

Save samueleastdev/e05fc226b9adcdd84698aee017194e9f to your computer and use it in GitHub Desktop.
Appcelerator push notifications IOS & Android
/*
* Setup push notification for Appc
* callback
*/
// Android go to firebase https://console.firebase.google.com
// Create project
// Add android app
// Enter SHA1 key run: keytool -exportcert -keystore keystore -list -v
// Click project cog -> project settings and then cloud messaging
// Copy and paste details into Appc cloud
// Sender ID will be 12 digits and Server key will be huge
// Easy when you know how with old outdated docs and 6 hours later
/* Example advanced push json
{
"alert": "Breaking: North Korea fires ballistic missile #WhatsTheWorldSaying",
"badge": 1,
"icon": "appicon",
"title": "North Korea",
"vibrate": true,
"time_to_live": 3600,
"priority": false,
"search": {
"query": "fun"
}
}
*/
exports.pushNotification = function(callback) {
// Run Android setup
if (OS_ANDROID) {
var CloudPush = require('ti.cloudpush');
CloudPush.retrieveDeviceToken({
success : deviceTokenSuccess,
error : deviceTokenError
});
// Process incoming push notifications
/*CloudPush.addEventListener('callback', function(e) {
var payload = JSON.parse(e.payload);
if (payload) {
callback({
error : false,
data : payload
});
}
});*/
CloudPush.addEventListener('trayClickLaunchedApp', function(e) {
var payload = JSON.parse(e.payload);
if (payload) {
callback({
error : false,
data : payload
});
}
});
/*CloudPush.addEventListener('trayClickFocusedApp', function(e) {
var payload = JSON.parse(e.payload);
if (payload) {
callback({
error : false,
data : payload
});
}
});*/
}
// Run ios only setup
if (OS_IOS) {
if (Ti.Platform.name === "iPhone OS" && 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 : receiveIOSPush
});
});
// 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 : receiveIOSPush
});
}
function receiveIOSPush(e) {
callback({
error : false,
data : e.data
});
}
}
// Save the device token for subsequent API calls
function deviceTokenSuccess(e) {
// Include this needs to be in this function due to the callback
var Cloud = require("ti.cloud");
// Grab the device token
var deviceToken = e.deviceToken;
console.log("SOBYTES PUSH: Setting the device token: " + deviceToken);
Titanium.App.Properties.setString('deviceToken', deviceToken);
Cloud.PushNotifications.subscribeToken({
device_token : Titanium.App.Properties.getString('deviceToken'),
channel : 'wnt',
type : (OS_IOS) ? 'ios' : 'android'
}, function(e) {
console.log('SOBYTES PUSH SUBSCRIBED: ' + JSON.stringify(e));
});
// Reset the badge if needed
Cloud.PushNotifications.resetBadge({
device_token : Titanium.App.Properties.getString('deviceToken')
}, function(e) {
console.log('SOBYTES PUSH BADGE: ' + JSON.stringify(e));
});
}
function deviceTokenError(e) {
callback({
error : true,
message : e.error
});
}
};
/*
* run after post layout
*/
function postWindow() {
// Setup push
var enablePush = require('enablePush');
enablePush.pushNotification(function(res) {
if (res.error) {
console.log('SOBYTES IOS PUSH ERROR: ', res.message);
return;
}
if (res.data.search) {
var data = {
value : res.data.search.query
};
searchArticles(data);
// Run the search function
}
});
}
<Alloy>
<Window onPostlayout="postWindow" exitOnClose="true">
</Window>
</Alloy>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment