Last active
April 19, 2016 09:36
-
-
Save rakhimoni/010e64fed4385f553055 to your computer and use it in GitHub Desktop.
PustNotification with ti.cloudpush module 3.4.1
This file contains hidden or 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
| var win = Ti.UI.createWindow({ | |
| backgroundColor : '#ccc', | |
| title : 'Android Cloud Push Notification' | |
| }); | |
| var CloudPush = require('ti.cloudpush'); | |
| CloudPush.debug = true; | |
| CloudPush.enabled = true; | |
| CloudPush.showTrayNotificationsWhenFocused = true; | |
| CloudPush.showTrayNotification = true; | |
| CloudPush.focusAppOnPush = false; | |
| var deviceToken = null; | |
| var Cloud = require('ti.cloud'); | |
| Cloud.debug = true; | |
| var submit = Ti.UI.createButton({ | |
| title : 'Register For Push Notification', | |
| color : '#000', | |
| height : 60, | |
| width : 200, | |
| top : 100, | |
| }); | |
| win.add(submit); | |
| submit.addEventListener('click', function(e) { | |
| loginDefault(); | |
| }); | |
| var submit1 = Ti.UI.createButton({ | |
| title : 'Subscribe Device', | |
| color : '#000', | |
| height : 60, | |
| width : 200, | |
| top : 200, | |
| }); | |
| win.add(submit1); | |
| submit1.addEventListener('click', function(e) { | |
| defaultSubscribe(); | |
| }); | |
| CloudPush.retrieveDeviceToken({ | |
| success : deviceTokenSuccess, | |
| error : deviceTokenError | |
| }); | |
| function deviceTokenSuccess(e) { | |
| deviceToken = e.deviceToken; | |
| } | |
| function deviceTokenError(e) { | |
| alert('Failed to register for push notifications! ' + e.error); | |
| } | |
| CloudPush.addEventListener('callback', function(evt) { | |
| alert("Notification received: " + evt.payload); | |
| }); | |
| CloudPush.addEventListener('trayClickLaunchedApp', function(evt) { | |
| Ti.API.info('Tray Click Launched App (app was not running)'); | |
| }); | |
| CloudPush.addEventListener('trayClickFocusedApp', function(evt) { | |
| Ti.API.info('Tray Click Focused App (app was already running)'); | |
| }); | |
| function loginDefault(e) { | |
| Cloud.Users.login({ | |
| login : 'rmonicse@gmail.com',// you can set your credentials here | |
| password : '123456' | |
| }, function(e) { | |
| if (e.success) { | |
| alert("Successfully Loged In"); | |
| } else { | |
| alert('Error: ' + ((e.error && e.message) || JSON.stringify(e))); | |
| } | |
| }); | |
| } | |
| function defaultSubscribe() { | |
| Cloud.PushNotifications.subscribe({ | |
| channel : 'alert', | |
| device_token : deviceToken, | |
| type : 'android' | |
| }, function(e) { | |
| if (e.success) { | |
| alert('Device Subscribed!'); | |
| //you can set your tokens here for testing purpose | |
| Cloud.PushNotifications.notifyTokens({ | |
| channel: 'alert', | |
| to_tokens:'APA91bHIYlPjc983uCKAJ7jXAnX2Dyn69yaGTomUT1NyRBcvS2azVaKjRSrKX03lHPfOjrA-TFh8Ojjce95IsL3bQuSLu8MSKoackOoa-MIEZyA8MLET8P5FGmLfHi3FcAqN_tTJhNxU' , | |
| payload: 'Welcome to push notifications rakhi' | |
| }, function (e) { | |
| if (e.success) { | |
| alert('Success'); | |
| } else { | |
| alert('Error:\n' + | |
| ((e.error && e.message) || JSON.stringify(e))); | |
| } | |
| }); | |
| } else { | |
| alert('Error:' + ((e.error && e.message) || JSON.stringify(e))); | |
| } | |
| }); | |
| } | |
| Console logs: | |
| /* | |
| [INFO] : data: {"channel":"alert","to_tokens":"APA91bHIYlPjc983uCKAJ7jXAnX2Dyn69yaGTomUT1NyRBcvS2azVaKjRSrKX03lHPfOjrA-TFh8Ojjce95IsL3bQuSLu8MSKoackOoa-MIEZyA8MLET8P5FGmLfHi3FcAqN_tTJhNxU","payload":"Welcome to push notifications rakhi","suppress_response_codes":"true","ti_analytics":"{\"id\":\"b0fbdae2-1c01-4c94-a07c-4a3d4ae00607\",\"mid\":\"1faea858889c8973\",\"aguid\":\"a2c7ee95-37a3-4628-9777-d272a5c7b3be\",\"event\":\"cloud.push_notification.notify_tokens\",\"deploytype\":\"test\",\"sid\":\"1a64d4eb-7601-417d-b9e9-8dad2250c1bc\"}"} | |
| [INFO] : AppCompatDelegate: The Activity's LayoutInflater already has a Factory installed so we can not install AppCompat's | |
| [DEBUG] : HTTPClient: The persistent handle is disposed. | |
| [INFO] : {"success":true,"error":false,"meta":{"status":"ok","code":200,"method_name":"NotifyTokens"}} | |
| [INFO] : ALERT: (KrollRuntimeThread) [2342,102929] Success | |
| [DEBUG] : HTTPClient: The persistent handle is disposed. | |
| [INFO] : AppCompatDelegate: The Activity's LayoutInflater already has a Factory installed so we can not install AppCompat's | |
| [INFO] : APSCloudPush: receivePayload: {"android":{"alert":"Welcome to push notifications rakhi"}} | |
| [INFO] : APSCloudPush: background: true | |
| [INFO] : APSCloudPush: queuePayload: {"android":{"alert":"Welcome to push notifications rakhi"}} | |
| [INFO] : APSCloudPush: showTrayNotification | |
| [INFO] : APSCloudPush: getTrayPendingIntent | |
| [INFO] : APSCloudPush: getFocusIntent payload: {"android":{"alert":"Welcome to push notifications rakhi"}} | |
| [INFO] : APSCloudPush: processCallbackIfInstantiated payload: {"android":{"alert":"Welcome to push notifications rakhi"}} | |
| [INFO] : APSCloudPush: processQueuedCallback | |
| [INFO] : ALERT: (KrollRuntimeThread) [2381,105310] Notification received: {"android":{"alert":"Welcome to push notifications rakhi"}} | |
| [INFO] : AppCompatDelegate: The Activity's LayoutInflater already has a Factory installed so we can not install AppCompat's | |
| /* | |
| win.open(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment