Push Notifications: iOS 8 + Pushwoosh + Appcelerator’s Titanium

With Apple introducing a new method affecting push notifications, this led to Appcelerator introducing a new API call to handle this. Which in turn…you guessed it, rendered some of my code useless when interfacing with Pushwoosh. Essentially I could not send or receive device-to-device push notifications because my device was not registered.

Another thing to note, the way I use device-to-device push notifications with Pushwoosh is to send the device ID to my database under a unique userid for the app. Then when a user interacts with another user, I hit the device table to send the device id to pushwoosh, and so on. I couldn’t even get the id to save it. Huge problem.

Thankfully Appcelerator’s documentation is thorough and I found the solution:

        // Check if the device is running iOS 8 or later
	if (Ti.Platform.name == "iPhone OS" && 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
	    });
	}
	
	function deviceTokenError(e) {
	    alert('Failed to register for push notifications! ' + e.error);
	};
	
	function deviceTokenSuccess(e){
                //store deviceToken in variable
		app.DEVICE_ID = e.deviceToken;
    	        //this is where I store my device id in the database too
                //show the deviceToken for testing
    	        //alert(e.deviceToken);

                //Pushwoosh register() function goes here in the success function
    	        PushWoosh.register(function(data) {
                     Ti.API.info("PushWoosh register success: " + JSON.stringify(data));
                },function(e)
                {
      	             Ti.API.info("Couldn't register with PushWoosh: " + JSON.stringify(e));
                });
	};
	
	function receivePush(e){
	     //do something with the data from Pushwoosh
             var parsedContent = JSON.parse(e.data.u);
	};

This seems to be working for me. Official Appcelerator documentation is here. I just added in Pushwoosh stuff. You still need the Pushwoosh file in your Resources folder but the code just changes a little. Happy coding.

Leave a Reply

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