Appcelerator Setting Up Push Notifications

Nov 28, 2017 by admin Category: Appcelerator, Blog 0 comments

Android open 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"
  }
}

Create a enablePush.js in your lib folder.

exports.pushNotification = function(callback) {

	// Require the module
	var Cloud = require("ti.cloud");
	var CloudPush = require('ti.cloudpush');
	var deviceToken = null;

	if (OS_ANDROID) {

		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
				});

			}

		});*/
		
		

	}

	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) {

		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
		});

	}

};

In your post window call run the code.

// 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

		}

	});

Leave a Comment!

Your email address will not be published. Required fields are marked *