Last active
November 12, 2016 06:22
-
-
Save boydlee/fc6955cbd4a7ba1d302df2a4bc93bb06 to your computer and use it in GitHub Desktop.
OneSignal library and basic example for push notifications using Appcelerator Titanium See https://gist.github.com/boydlee/2e5bbcf6c8795fd821d9e231841a776d for a basic usage example with tags. This code uses the GCM library "nl.vanvianen.android.gcm" which is on Github at https://github.com/morinel/gcmpush
This file contains hidden or 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 gcm; | |
var androidSettings = { | |
/* The Sender ID from Google Developers Console, see https://console.developers.google.com/project/XXXXXXXX/apiui/credential */ | |
/* It's the same as your project id */ | |
senderId: Alloy.CFG.gcm_project_id ? Alloy.CFG.gcm_project_id : "<your gcm project id --- xxxxxxxxxxxx>", | |
notificationSettings: { | |
//sound: 'mysound.mp3', /* Place sound file in platform/android/res/raw/mysound.mp3 */ | |
smallIcon: 'trayicon.png', /* Place icon in platform/android/res/drawable/notification_icon.png */ | |
largeIcon: 'appicon.png', /* Same */ | |
vibrate: true, /* Whether the phone should vibrate */ | |
insistent: true, /* Whether the notification should be insistent */ | |
//group: 'Happtitude', /* Name of group to group similar notifications together */ | |
//localOnly: true, /* Whether this notification should be bridged to other devices */ | |
priority: +2, /* Notification priority, from -2 to +2 */ | |
bigText: false, | |
/* You can also set a static value for title, message, or ticker. If you set a value here, the key will be ignored. */ | |
//title: '', | |
messageKey: 'alert', | |
//message: 'Have you checked in today?', | |
// ticker: '' | |
/* Add LED flashing */ | |
//ledOn: 200, | |
//ledOff: 300 | |
}, | |
eventCallback: null | |
}; | |
var iosSettings = { | |
eventCallback: null | |
}; | |
/* | |
* | |
* | |
*/ | |
var OneSignal = { | |
onPush: function(_callback) { | |
iosSettings.eventCallback = _callback; | |
androidSettings.eventCallback = _callback; | |
}, | |
updateTags: function(onesignal_user_id, tags, _callback) { | |
var loader = Titanium.Network.createHTTPClient(); | |
loader.onload = function(){ | |
var response = JSON.parse(this.responseText); | |
if(_callback) _callback(response); | |
}; | |
loader.onerror = function(){ | |
Ti.API.error(this.responseText); | |
if(_callback) _callback({success: false}); | |
}; | |
loader.open("PUT", 'https://onesignal.com/api/v1/players/'+onesignal_user_id); | |
loader.setRequestHeader("Content-Type","application/json"); | |
loader.send(JSON.stringify({tags: tags})); | |
}, | |
subscribe: function(registrationId, _callback){ | |
var subscription_data = { | |
app_id: Alloy.CFG.onesignal_app_id, | |
identifier: registrationId, | |
device_type: (OS_IOS) ? 0 : 1, | |
language: 'en' | |
}; | |
subscription_data.app_id = (Alloy.CFG.onesignal_app_id) ? Alloy.CFG.onesignal_app_id : "your onesignal app id - e.g xxxxxx-xxxx-xxx-205951ae569b"; | |
var loader = Titanium.Network.createHTTPClient(); | |
loader.onload = function(){ | |
var response = JSON.parse(this.responseText); | |
_callback(response); | |
}; | |
loader.onerror = function(){ | |
Ti.API.error(this.responseText); | |
_callback(false); | |
}; | |
loader.open("POST", 'https://onesignal.com/api/v1/players'); | |
loader.setRequestHeader("Content-Type","application/json"); | |
loader.send(JSON.stringify(subscription_data)); | |
}, | |
register: function(_callback) { | |
Ti.API.warn("---------------- beginning onesignal push registration ---------------"); | |
if(OS_ANDROID) { | |
gcm = require("nl.vanvianen.android.gcm"); | |
gcm.registerPush({ | |
senderId: androidSettings.senderId, | |
notificationSettings: androidSettings.notificationSettings, | |
success: function (event) { | |
Ti.API.warn("-----------------"); | |
Ti.API.info("Push registration success: " + JSON.stringify(event)); | |
if(event.success) { | |
var registrationId = event.registrationId; | |
OneSignal.subscribe(registrationId, _callback); | |
} | |
else { | |
_callback(event); | |
} | |
/* Add code to send event.registrationId to your server */ | |
}, | |
error: function (event) { | |
Ti.API.info("Push registration error: " + JSON.stringify(event)); | |
//alert(event.error); | |
}, | |
callback: function (event) { | |
Ti.API.info("Push callback = " + JSON.stringify(event)); | |
/* Called when a notification is received and the app is in the foreground */ | |
if(androidSettings.eventCallback) { | |
androidSettings.eventCallback(event.data); | |
} | |
/*var dialog = Ti.UI.createAlertDialog({ | |
title: 'Push received', | |
message: JSON.stringify(event.data), | |
buttonNames: ['View','Cancel'], | |
cancel: 1 | |
}); | |
dialog.addEventListener("click", function(event) { | |
dialog.hide(); | |
if (event.index == 0) { | |
} | |
}); | |
dialog.show(); */ | |
} | |
}); | |
} | |
/* | |
* need to treat | |
*/ | |
else if(OS_IOS) { | |
var eventSuccess = function(e) { | |
// *MUST* pass the received token to the module | |
Ti.API.info(JSON.stringify(e)); | |
var devicetoken = e.deviceToken; | |
OneSignal.subscribe(devicetoken, _callback); | |
}; | |
var eventError = function(e) { | |
//alert(JSON.stringify(e)); | |
console.error('Error:' + e.error); | |
_callback({success: false}); | |
}; | |
var eventCallback = function(e) { | |
Ti.API.info(JSON.stringify(e)); | |
if(iosSettings.eventCallback) { | |
iosSettings.eventCallback(e); | |
} | |
}; | |
//you have to treat ios7 and ios8+ differently | |
if (parseInt(Ti.Platform.version.split(".")[0]) >= 8) { | |
Ti.App.iOS.addEventListener('usernotificationsettings', e = function() { | |
Ti.App.iOS.removeEventListener('usernotificationsettings', e); | |
Ti.Network.registerForPushNotifications({ | |
success: eventSuccess, | |
error: eventError, | |
callback: eventCallback | |
}); | |
}); | |
Ti.App.iOS.registerUserNotificationSettings({ | |
types: [ | |
Ti.App.iOS.USER_NOTIFICATION_TYPE_BADGE, | |
Ti.App.iOS.USER_NOTIFICATION_TYPE_ALERT, | |
Ti.App.iOS.USER_NOTIFICATION_TYPE_SOUND | |
] | |
}); | |
} else { | |
//is ios7 or older | |
Ti.Network.registerForPushNotifications({ | |
types: [ | |
Ti.Network.NOTIFICATION_TYPE_BADGE, | |
Ti.Network.NOTIFICATION_TYPE_ALERT, | |
Ti.Network.NOTIFICATION_TYPE_SOUND | |
], | |
success: eventSuccess, | |
error: eventError, | |
callback: eventCallback | |
}); | |
} | |
} | |
}, | |
checkBackgroundNotification: function() { | |
if(OS_ANDROID) { | |
gcm = require("nl.vanvianen.android.gcm"); | |
var lastData = gcm.getLastData(); | |
if (lastData) { | |
Ti.API.info("Last notification received " + JSON.stringify(lastData)); | |
gcm.clearLastData(); | |
} | |
} | |
}, | |
}; | |
module.exports = OneSignal; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment