Last active
August 29, 2015 14:23
-
-
Save katopz/d690d05bab8668810f7d to your computer and use it in GitHub Desktop.
Will subscribe to all push notification via parse.com with pure as3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
import flash.desktop.InvokeEventReason; | |
import flash.desktop.NativeApplication; | |
import flash.display.Sprite; | |
import flash.events.Event; | |
import flash.events.HTTPStatusEvent; | |
import flash.events.IOErrorEvent; | |
import flash.events.InvokeEvent; | |
import flash.events.ProgressEvent; | |
import flash.events.RemoteNotificationEvent; | |
import flash.events.SecurityErrorEvent; | |
import flash.events.StatusEvent; | |
import flash.net.URLLoader; | |
import flash.net.URLRequest; | |
import flash.net.URLRequestHeader; | |
import flash.net.URLRequestMethod; | |
import flash.notifications.NotificationStyle; | |
import flash.notifications.RemoteNotifier; | |
import flash.notifications.RemoteNotifierSubscribeOptions; | |
import flash.text.TextField; | |
/** | |
* Will subscribe to all push notification via parse.com | |
* @author katopz | |
* | |
*/ | |
public class Main extends Sprite | |
{ | |
private var tt:TextField; | |
private var preferredStyles:Vector.<String> = new Vector.<String>(); | |
private var subscribeOptions:RemoteNotifierSubscribeOptions = new RemoteNotifierSubscribeOptions(); | |
private var remoteNot:RemoteNotifier = new RemoteNotifier(); | |
public function Main() | |
{ | |
// debug | |
tt = new TextField; | |
addChild(tt); | |
tt.autoSize = "left"; | |
// Subscribe to all three styles of push notifications: | |
// ALERT, BADGE, and SOUND. | |
preferredStyles.push(NotificationStyle.ALERT, NotificationStyle.BADGE, NotificationStyle.SOUND); | |
subscribeOptions.notificationStyles = preferredStyles; | |
// watch for noti while App running or app in the background | |
remoteNot.addEventListener(RemoteNotificationEvent.TOKEN, tokenHandler); | |
remoteNot.addEventListener(RemoteNotificationEvent.NOTIFICATION, notificationHandler); | |
remoteNot.addEventListener(StatusEvent.STATUS, statusHandler); | |
/* | |
RemoteNotificationEvent.NOTIFICATION event is dispatched only in the following 2 cases: | |
The application was in foreground when remote notification was received. | |
The application was not in foreground when notification was received and as a result of the presented notification, the user taps the action button of the alert. | |
this event will NEVER be fired if application was in background(sleeping/dead). | |
*/ | |
NativeApplication.nativeApplication.addEventListener(InvokeEventReason.NOTIFICATION, onInvokeEvent); | |
NativeApplication.nativeApplication.addEventListener(InvokeEventReason.OPEN_URL, onInvokeEvent); | |
NativeApplication.nativeApplication.addEventListener(InvokeEventReason.STANDARD, onInvokeEvent); | |
// sub! | |
remoteNot.subscribe(subscribeOptions); | |
} | |
protected function onInvokeEvent(event:InvokeEvent):void | |
{ | |
doTrace("Invokehandler called .... \n"); | |
doTrace("reason: " + event.reason + "\n"); | |
if (event.reason == InvokeEventReason.NOTIFICATION) | |
{ | |
var payload:Object = Object(event.arguments[0]); | |
for (var i:String in payload) | |
{ | |
doTrace("Key:value pair " + i + ":" + payload[i] + "\n"); | |
} | |
// TODO: DO THE NEEDFUL ON RECIEVING A NOTIFICATION HERE | |
} | |
} | |
// Receive notification payload data and use it in your app | |
public function notificationHandler(e:RemoteNotificationEvent):void | |
{ | |
doTrace("notificationHandler:" + e); | |
doTrace("\nRemoteNotificationEvent type: " + e.type + | |
"\nbubbles: " + e.bubbles + "\ncancelable " + e.cancelable); | |
for (var x:String in e.data) | |
{ | |
tt.text += "\n" + x + ": " + e.data[x]; | |
} | |
} | |
// If the subscribe() request succeeds, a RemoteNotificationEvent of | |
// type TOKEN is received, from which you retrieve e.tokenId, | |
// which you use to register with the server provider (urbanairship, in | |
// this example. | |
public function tokenHandler(e:RemoteNotificationEvent):void | |
{ | |
doTrace("tokenHandler:" + e); | |
install(e.tokenId.toLowerCase()); | |
} | |
// If the subscription request fails, StatusEvent is dispatched with | |
// error level and code. | |
public function statusHandler(e:StatusEvent):void | |
{ | |
doTrace("\n statusHandler"); | |
doTrace("event Level" + e.level + "\nevent code " + | |
e.code + "\ne.currentTarget: " + e.currentTarget.toString()); | |
} | |
private function install(tokenId:String):void | |
{ | |
// install | |
var _urlRequest:URLRequest = new URLRequest("https://api.parse.com:443/1/installations"); | |
_urlRequest.method = URLRequestMethod.POST; | |
_urlRequest.requestHeaders = [new URLRequestHeader("X-Parse-Application-Id", APP_ID), new URLRequestHeader("X-Parse-REST-API-Key", API_KEY)]; | |
_urlRequest.contentType = "application/json"; | |
_urlRequest.data = JSON.stringify({deviceType: "ios", deviceToken: tokenId /*"fdb75886e16ae3a25a946a223e79a8248cf4a02b7cbc036e7d59c3acfa800373"*/, channels: [""]}); | |
doTrace(_urlRequest.data); | |
// {"channels":[""],"deviceToken":"9D555600-27B6-4191-A5C2-A072191F4551","deviceType":"ios"} | |
var _loader:URLLoader = new URLLoader(); | |
_loader.load(_urlRequest); | |
_loader.addEventListener(Event.COMPLETE, eventHandler); | |
_loader.addEventListener(ProgressEvent.PROGRESS, eventHandler); | |
_loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, eventHandler); | |
_loader.addEventListener(IOErrorEvent.IO_ERROR, eventHandler); | |
_loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, eventHandler); | |
} | |
private function eventHandler(event:Event):void | |
{ | |
doTrace(event); | |
doTrace(event.target.data); | |
/* | |
{"channels":[""],"deviceToken":"fdb75886e16ae3a25a946a223e79a8248cf4a02b7cbc036e7d59c3acfa800373","deviceType":"ios","createdAt":"2014-02-10T06:05:48.696Z","updatedAt":"2014-02-10T06:10:19.615Z","objectId":"4frtzC82BA"} | |
*/ | |
doTrace(String(event) + "\n"); | |
} | |
private function doTrace(... _):void | |
{ | |
tt.text += "\n" + _; | |
trace(_); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment