Inviare notifiche PUSH da un server verso altri device registrati al servizio. È richiesto che la notifica arrivi anche se il ricevente non è UP.
Preventivamente, ogni device che desidera ricevere le notifiche dovrà essere registrato al servizio di notifiche push su una ACS APP tramite il device_token inviato al server da una APP OS nativa installata nel device stesso. La registrazione comprende:
La push fatta dal server si divide in 2 parti:
NOTA: È possibile mandare push alla App ACS sia in modalità Development che in modalità Production (hanno due cloud link diversi). In questo caso, è importante considerare che app android firmate con una debug key possono ricevere push solo di tipo development, mentre app android firmate con release key possono ricevere push solo di tipo production . Questo ovviamente previa iscrizione del token
Ad ogni utente possono essere registrati diversi device, anche gli stessi su piu canali.
Appcelerator ACS, cURL
E’ necessario installare cURL: [http://curl.haxx.se/dev/source.html]
NOTA: in questo documento viene mostrato come inviare notifiche ad un device Android. Tuttavia il processo è parallelo per device IOS, con i dovuti accorgimenti riportati nei link alla documentazione di Appcelerator presenti nelle prossime sezioni.
Le stesse informazioni sono inserite nel tiapp.xml file, insieme anche anche alle chiavi oauth
NOTA: E’ necessario utilizzare un utente admin affinchè possa fare le push per tutti i device su un canale.
curl -i -X POST -F “type=<TYPE>” -F “device_token=<TOKEN>” -F “channel=<CHANNEL>” “http://api.cloud.appcelerator.com/v1/push_notification/subscribe_token.json?key=<APP_KEY>&pretty_json=true”
curl -i -X POST -F “type=android” -F “device_token=APA91bHF_AHTplu57dY115i9h5qntVeLlOwKfM8YbkwM3THhhgXEB_vIxec9kBu_eNAAY4UAstU_jQc6dUTIVdvwukP-w16bIyTFEJnOCAbX5nMfLx7l3ea4XCXRLnY6xSqGnWfZKPc0rNWfWwTaB5NEzKqdfDzgio0w1v4_pFII-rcU_5VhNrU” -F “channel=push.notification” “http://api.cloud.appcelerator.com/v1/push_noti fication/subscribe_token.json?key=XC6OWeoFQGEgP6Xtfb9V0mbF9oqgicjT&pretty_json=true”
In [http://docs.appcelerator.com/cloud/latest/#!/api/PushNotifications] è possibile trovare una lista dei possibili parametri da passare a una Push
Importante: ACS richiede un’autenticazione prima di procedere a una push.
NOTA: controllare che l’autenticazione sia effettivamente fatta tramite i cookie, e non sia a livello di sessione, altrimenti ACS non potrà controllare che l’utente sia un admin
Il device deve essere precedentemente registrato.
curl -b cookies.txt -c cookies.txt [ [-F “param=value”] ] "https://api.cloud.appcelerator.com/v1/users/login.json?key=<APP KEY>"
OPPURE
curl -b cookies.txt -c cookies.txt -X POST -F "channel=<channel>" -F "to_tokens=[token,][everyone]" -F "payload=<payload>" "https://api.cloud.appcelerator.com/v1/push_notification/notify_tokens.json?key=OpSVzgLtcqPn5tDP96djYVHQNjEAOROU&pretty_json=true"
RESPONSE: {
"meta": { "status": "ok", "code": 200, "method_name": "NotifyTokens" }
}
curl -b cookies.txt -c cookies.txt -F "login=admin" -F "password=admin" "https://api.cloud.appcelerator.com/v1/users/login.json?key=OpSVzgLtcqPn5t
DP96djYVHQNjEAOROU&pretty_json=true“
{"meta": {"code":200, "status":"ok", "method_name":"loginUser", "session_id":"dw-GBX9liki_YNu8OTchKIwO__M" }, "response":{ "users":[ { "id":"54579b36f13f3a442f003079", "first_name":"admin", "last_name":"admin", "tags":["admin"], "created_at":"2014-11-03T15:11:50+0000", "updated_at":"2014-11-03T16:16:37+0000", "external_accounts":[], “confirmed_at":"2014-11-03T15:11:50+0000", "username":"admin", "role":"admin", "admin":"true"," stats":{ "photos":{"total_count":0}, "storage":{"used":0}}, "email":"prova1@prova.it", "friend_counts":{"requests":0,"friends":0} } ] } }
“login=” richiede il nome utente “password=” richiede la password “key=” richiede l’APP KEY della ACS APP sul cloud appcelerator
Ad una lista (in questo caso uno) di tokens
curl -b cookies.txt -c cookies.txt -X POST -F "channel=welfare" -F "to_tokens=APA91bHF_AHTplu57dY115i9h5qntVeLlOwKfM8YbkwM3THhhgXEB_vIxec9kBu_eNAAY4UAstU_jQc6dUTIVdvwukP-w16bIyTFEJnOCAbX5nMfLx7l3ea4XCXRLnY6xSqGnWfZKPc0rNWfWwTaB5NEzKqdfDzgio0w1v4_pFII-rcU_5VhNrU" -F "payload=prova_acs" "https://api.cloud.appcelerator.com/v1/push_notification/notify_tokens.json?key=OpSVzgLtcqPn5tDP96djYVHQNjEAOROU&pretty_json=true"
{ "meta": { "status": "ok", "code": 200, "method_name": "NotifyTokens" } }
A tutti
curl -b cookies.txt -c cookies.txt -X POST -F "channel=welfare" -F "to_tokens=everyone" -F "payload=prova_acs" https://api.cloud.appcelerator.com/v1/push_notification/notify_tokens.json?key=OpSVzgLtcqPn5tDP96djYVHQNjEAOROU&pretty_json=true"
{
"meta": { "status": "ok", "code": 200, "method_name": "NotifyTokens" }
}
“channel=” è il canale su cui viene pushata la notifica “to_tokens=” contiene una lista di token relativi ai device iscritti al canale “payload=” contiene il testo della notifica “key=” richiede l’APP KEY della ACS APP sul cloud appcelerator
NOTA: altre proprietà possono essere settate, come i Badges. Inoltre ci sono alcune proprietà settabili solo per device Android.
NOTA: il parametro payload puo essere settato in due diversi modi, come stringa o come Json:
payload : String/Hash required
Payload to send with the push notification.
For a string, it will be sent as an alert (message notification).
For a JSON payload, you can specify the following fields as well as custom fields:
alert (String): Notification message to be displayed.
badge (String): Number to set as the badge on the application's icon. Specify postive and negative numbers with the + and - symbols to increment or decrement the current badge number, respectively.
sound (String): Filename minus the extension of the sound to play.
For native Android applications, place the file in the /assets/sound directory.
For native iOS applications, place the file in the main bundle.
For Android applications built with Titanium, place the file in the /Resources/sound directory.
For iOS applications built with Titanium, place the file in the /Resources directory.
icon (String): Available on Android only. Filename minus the extension of the icon to display in the notification center. Place the file in /res/drawable for native Android applications or /Resources for Titanium applications. Uses the app's icon by default.
title (String): Available on Android only. Title of the notification.
vibrate (Boolean): Available on Android only. If true, the device vibrates for one second.
For example:
{
“atras”: “your_user_id”,
“tags”: [
“tag1”,
“tag2”
],
“badge”: ”+2“,
“sound”: “default”,
“alert” : “Push Notification Test”
}
In the above example, the atras and tags keys are custom fields, “badge” : ”+2“ increments the current badge value by two, “sound” : “default” plays a default system sound and “alert” : “Push Notification Test” pops up a alert window with text “Push Notification Test”.
Installare le oracle JDK (da 6 a 8 ) Installare Node.js Installare Android SDK (per la versione, vedere http://docs.appcelerator.com/titanium/3.0/#!/guide/Installing_the_Android_SDK
ti setup quick , oppure ti setup (avanzato)
I device Android riceventi devono usare il pacchetto ti.cloudpush per registrarsi all’ACS sotto una applicazione ACS. In seguito ricevereanno le push notifications inviate loro sui canali corrispondenti.
I device IOS riceventi devono usare il pacchetto ti.cloud per registrarsi all’ACS sotto una applicazione ACS. In seguito ricevereanno le push notifications inviate loro sui canali corrispondenti.
Esempio di DEVICE_TOKEN android = APA91bHF_AHTplu57dY115i9h5qntVeLlOwKfM8YbkwM3THhhgXEB_vIxec9kBu_eNAAY4UAstU_jQc6dUTIVdvwukP-w16bIyTFEJnOCAbX5nMfLx7l3ea4XCXRLnY6xSqGnWfZKPc0rNWfWwTaB5NEzKqdfDzgio0w1v4_pFII-rcU_5VhNrU
Luca
CIOE' MI VUOI DIRE CHE LA APP1 NON DICE NIENTE AD APPCELERATOR, DA' IL TOKEN A NOI, NOI LO REGISTRIAMO SU APP2 E POI POSSIAMO INVIARE LE NOTIFICHE AI CELL CON APP1 TRAMITE IL TOKEN REGISTRATO SU APP2 ? :gift_heart:?
Matteo
se appcelerator non cambia la sua politica nelle prossime 12 ore…. si ! :tada: ho appositamente deregistrato la (mia) app1, che avevo registrato da smartphone ed ora riceve comunque le notifiche inviate utilizzando l'api_key dell'app2.
Può ritornare un HTTP 400 “Subscription Not Found”
installazione smartphone app su device collegata ad app ACS e registrazione: v
deregistrazione da app ACS con successiva registrazione da curl: v
deregistrazione da app ACS con successiva registrazione da curl verso una app ACS diversa: v