Skip to content

Instantly share code, notes, and snippets.

@milon120203
Forked from MotiurRahman/index.js
Created September 10, 2018 17:53
Show Gist options
  • Save milon120203/a62f5b701b5f31badc5b689ca4c02bb0 to your computer and use it in GitHub Desktop.
Save milon120203/a62f5b701b5f31badc5b689ca4c02bb0 to your computer and use it in GitHub Desktop.
Push Notification
var Cloud = require("ti.cloud");
var deviceToken = null;
// Places your already created user id credential
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(e) {
alert("Notification received: " + e.payload);
});
} else {
// Check if the device is running iOS 8 or later
if ((Ti.Platform.osname == 'iphone' || Ti.Platform.osname == 'ipad' || Ti.Platform.osname == 'ipod') && 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) {
var alertString = e.data.alert;
Cloud.PushNotifications.resetBadge({
device_token : deviceToken
}, function(e) {
if (e.success) {
Ti.UI.iOS.setAppBadge(0);
} else {
//Error callback
Ti.UI.iOS.setAppBadge(0);
}
});
}
// 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
function doClick() {
Cloud.PushNotifications.subscribeToken({
device_token : deviceToken,
channel : "hello",
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 onAppResume() {
Ti.API.info("inside App resume");
Titanium.App.iOS.UserNotificationCenter.removeDeliveredNotifications();
}
if(OS_IOS)
{
Ti.App.addEventListener("resume", onAppResume);
}
$.index.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment