Skip to content

Instantly share code, notes, and snippets.

@kaosf
Created December 1, 2012 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaosf/4182185 to your computer and use it in GitHub Desktop.
Save kaosf/4182185 to your computer and use it in GitHub Desktop.
rewrite PushNotification.js by CoffeeScript
# rewrite this https://github.com/Guti/Google-Cloud-Messaging--Titanium-/blob/6ac1270026f3cc266f37812b64ef40f2ab1b9055/example/PushNotification.js
exports.pushNotification = ->
hostURL = 'http://YOUR_HOST_URL/register?'
gcm = require 'com.activate.gcm'
Ti.API.info "module gcm is => #{gcm}"
Ti.API.info 'Registering...'
gcm.registerC2dm
success: (e) ->
Ti.API.info "JS registration success event: #{e.registrationId}"
alert "Yeah JS registration success event: #{e.registrationId}"
alert "Sender ID:#{gcm.getSenderId()}"
alert "Registration ID:#{gcm.getRegistrationId()}"
appName = Ti.App.name
appVersion = Ti.App.version
deviceUUID = Ti.Platform.macaddress # Ti.Network.remoteDeviceUUID
deviceName = Ti.Platform.username
deviceModel = Ti.Platform.model
deviceSystemVersion = Ti.Platform.version
deviceToken = e.deviceToken
regId = e.registrationId
# deviceUUID = deviceUUID.replace /-/ig, ''
# deviceToken = deviceToken.replace /<>/ig, ''
host = hostURL
urlString = host
# urlString += "&appname=#{appName}"
# urlString += "&appversion=#{appVersion}"
# urlString += "&deviceuid=#{deviceUUID}"
# urlString += "&devicetoken=#{deviceToken}"
# urlString += "&devicename=#{deviceName}"
# urlString += "&devicemodel=#{deviceModel}"
# urlString += "&deviceversion=#{deviceSystemVersion}"
# urlString += "regId=#{regId}"
loader = Ti.Network.createHTTPClient()
loader.setTimeout 60000
loader.onload = (evt) ->
alert evt
return
loader.open 'GET', urlString, false
loader.send()
alert urlString
return
error: (e) ->
Ti.API.error "Error during registration : #{e.error}"
alert "Error during registration : #{e.error}"
if e.error is 'ACCOUNT_MISSING'
message = 'No Google account found; you will need to add on in order to activate notifications'
(Titanium.UI.createAlertDialog
title: 'Push Notification Setup'
message: message
buttonNames: ['OK']
).show()
return
callback: (e) -> # called when a push notification is received
Ti.API.info "JS message event: #{JSON.stringify e.data}"
alert "JS message event: #{JSON.stringify e.data}"
# same as e.data
# data = Ti.App.Properties.getString 'com.activate.gcm.last_data', ''
# data = JSON.parse data
# Ti.App.Properties.removeProperty 'com.activate.gcm.last_data'
# Ti.App.Properties.hasProperty 'com.activate.gcm.last_data'
# Ti.Android.NotificationManager.cancelAll()
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment