Using Appcelerator Cloud Serives Push Notification Part I

Posted By : Pawanpreet Singh | 31-Mar-2015

I will show you how to you can implement push notification services provided by appcelerator cloup services. Here we will try to send notification to an iOS device which is subscribed to a channel. Before starting you must check that you have TitaniumStudio with latest SDK installed.

Lets get started:-

 

Handling Client Side

  1. Open TitaniumStudio -> New -> Mobile App Project

    New Dialog will appear, Here don't change Alloy from the side menu then select “Default Alloy Project” and click next

    Enter Project name, App Id and just select iPhone in deployment targets and you must select the “cloud-enable this application” checkbox. As shown in the figure below:

  2. add ti.cloud and ti.cloudpush to your project in modules as shown in the following figure
  3. add the following code in acsPush.js under lib folder.
    var Cloud = require("ti.cloud");
    var osname = Ti.Platform.osname;
    var channel = "";
    
    var receivePush = function(e) {
    	Ti.API.info('got pushCallback');
    	Ti.API.debug(JSON.stringify(e));
    	if (osname != "android") {
    		// Do Something here?
    		alert("Notification received: " + JSON.stringify(e));
    	} else {
    		//var extras = e.extras;
    		setTimeout(function() {
    			// Do Something here?
    			alert("Notification received: " + JSON.stringify(e));
    		}, 750);
    	}
    };
    
    function deviceTokenError(e) {
    	Ti.API.error('Failed to register for push notifications! ' + e.error);
    }
    
    function deviceTokenSuccess(e) {
    	Ti.API.debug('got retrieveDeviceToken success');
    	Ti.API.debug('e=' + JSON.stringify(e));
    	Ti.API.debug('channel=' + channel);
    	subscribeToChannel(e.deviceToken);
    }
    
    function subscribeToChannel(token) {
    	Cloud.PushNotifications.subscribeToken({
    		device_token : token,
    		channel : channel,
    		type : Ti.Platform.name == 'android' ? 'android' : 'ios'
    	}, function(e) {
    		if (e.success) {
    			Ti.API.info('subscribed');
    			Ti.App.fireEvent('push:register', {
    				token : token
    			});
    
    		} else {
    			Ti.API.error('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
    		}
    	});
    }
    
    if (osname != "android") {
    
    	exports.registerForPush = function(username) {
    		channel = username;
    
    		Ti.API.info('registering for PUSH');
    		Ti.API.info('platform=' + Ti.Platform.version.split(".")[0]);
    
    		// Check if the device is running iOS 8 or later
    		if (parseInt(Ti.Platform.version.split(".")[0]) >= 8) {
    			function registerForPush() {
    				Ti.Network.registerForPushNotifications({
    					success : deviceTokenSuccess,
    					error : deviceTokenError,
    					callback : receivePush
    				});
    				// Remove event listener once registered for push notifications
    				Ti.App.iOS.removeEventListener('usernotificationsettings', registerForPush);
    			};
    
    			// Wait for user settings to be registered before registering for push notifications
    			Ti.App.iOS.addEventListener('usernotificationsettings', registerForPush);
    
    			// 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]
    			});
    
    		} else {
    			// For iOS 7 and earlier
    			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
    			});
    		}
    
    	};
    
    } else  {
    
    	var CloudPush = require('ti.cloudpush');
    
    	exports.registerForPush = function(username) {
    		channel = username;
    		Ti.API.info('registering for PUSH');
    		CloudPush.retrieveDeviceToken({
    			success : deviceTokenSuccess,
    			error : deviceTokenError
    		});
    		CloudPush.showTrayNotificationsWhenFocused = true;
    		CloudPush.addEventListener('callback', receivePush);
    
    	};
    
    }
    
    
  4. In index.js file under controller folder add the following code.
    var acs = require('/js/acsPush');
    	acs.registerForPush("library");

Before continuing you must have the following files ready

 

  • you must created a iOS development certificate.
  • Apple Id with push notification enabled. (must be same as your appId)
  • Apple Push Notification certificate.

Please refer to second part of this tutorial to know how to handle server side thingz...

About Author

Author Image
Pawanpreet Singh

Pawanpreet is an seasoned Project Manager with a wealth of knowledge in software development, specializing in frontend and mobile applications. He possesses a strong command of project management tools, including Jira, Trello, and others. With a proven track record, he has successfully overseen the delivery of multiple software development projects, managing budgets and large teams. Notable projects he has contributed to include TimeForge, Yogyata, Kairos, Veto, Inspirien App, and more. Pawanpreet excels in developing and maintaining project plans, schedules, and budgets, ensuring timely delivery while staying within allocated resources. He collaborates closely with clients to define project scope and requirements, establish timelines and milestones, and effectively manage expectations. Regular project status meetings are conducted by him, providing clients and stakeholders with consistent updates on project progress, risks, and issues. Additionally, he coaches and mentors project leads, offering guidance on project management best practices and supporting their professional development.

Request for Proposal

Name is required

Comment is required

Sending message..