{"__v":40,"_id":"5718c0cf459f6f0e007505f0","category":{"project":"54774d9af3736008009e9e0e","version":"56a0bd9e3697d80d002ac5e7","_id":"571006aa10300c0e007f6133","__v":0,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-04-14T21:07:54.730Z","from_sync":false,"order":16,"slug":"push-notifications","title":"Push Notifications"},"parentDoc":null,"project":"54774d9af3736008009e9e0e","user":"54774c47f3736008009e9e0c","version":{"__v":20,"_id":"56a0bd9e3697d80d002ac5e7","project":"54774d9af3736008009e9e0e","createdAt":"2016-01-21T11:14:38.131Z","releaseDate":"2016-01-21T11:14:38.131Z","categories":["56a0bd9f3697d80d002ac5e8","56a0bd9f3697d80d002ac5e9","56a0bd9f3697d80d002ac5ea","56a0bd9f3697d80d002ac5eb","56b098a43a5b810d00745e3f","56b098da8f7a4f0d0029dd10","56d5a8ecf612b80b00fb69cd","570989853ab43c0e0072b2d6","571006aa10300c0e007f6133","5735a52431a73b1700887ca0","5744b0529e045c0e00b7a7a2","5744b1d0b56d431700d8a4fb","5744b1fb55d65a0e00b436e1","5744b218f9c7411700ce560e","5744b220f9c7411700ce560f","5744b2259e045c0e00b7a7a7","5744b22cf8b79f0e00de6a2d","5744b23155d65a0e00b436e4","5744b242c758290e00fbc235","5744b2569e045c0e00b7a7a8","5756a3f2bb92962900dafe3d","57ee23e81e42900e0014a42f","581c822c98676e0f00d240ef"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"Sockets","version_clean":"1.1.0","version":"1.1"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-04-21T12:00:15.289Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":2,"body":"[block:callout]\n{\n  \"type\": \"warning\",\n  \"body\": \"Push Notifications are an experimental feature. If you find any issues, please write to us at [support:::at:::syncano.com](mailto:support@syncano.com)\",\n  \"title\": \"BETA!\"\n}\n[/block]\nBefore you can start building and testing your app on device, you will need:\n- Syncano Account and Instance ([get started here](getting-started-with-syncano))\n- Google Cloud Messaging and Syncano's Push Notifications Socket configured ([learn how to do it here](push-notification-sockets-android))\n- `google-services.json` obtained from the steps done in the Config chapter\n\nIf you have all of that, proceed to our short tutorial!\n\n1. [Get Syncano API Instance name and API Key](#1-get-syncano-api-instance-name-and-api-key)\n2. [Configure your app](#2-configure-your-app)\n3. [Run your app](#3-run-your-app)\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"1. Get Syncano API Instance name and API Key\"\n}\n[/block]\n1. Click on your instance to enter it. \n2. Copy its name and note it down - we will need it later.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/tetBolRxRjS7rfZvyAb5_Instance_name.jpg\",\n        \"Instance_name.jpg\",\n        \"1230\",\n        \"1472\",\n        \"#274774\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n3. Create an API Key with `Ignore ACL` permissions. Go to [Authentication & API Keys](http://docs.syncano.io/docs/authentication#section-creating-an-api-key) to learn how to do it. Alternatively, use the below curl call to create it.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"curl -X POST \\\\\\n-H \\\"X-API-KEY: ACCOUNT_KEY\\\" \\\\\\n-H \\\"Content-type: application/json\\\" \\\\\\n-d '{\\\"description\\\": \\\"GCM example app api key\\\", \\\"allow_anonymous_read\\\": false, \\\"allow_user_create\\\": true, \\\"ignore_acl\\\": true}' \\\\\\n\\\"https://api.syncano.io/v1.1/instances/INSTANCE/api_keys/\\\"\",\n      \"language\": \"curl\"\n    }\n  ]\n}\n[/block]\nWhen your API Key is created, copy it and save somewhere - we will need it in a second.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"2. Configure your app\"\n}\n[/block]\nYou will need the application source first. You can get it from [syncano-gcm-example](https://github.com/Syncano/syncano-gcm-example) repository on Github or just copy the line below to your terminal and press enter:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"git clone git@github.com:Syncano/syncano-gcm-example.git\",\n      \"language\": \"shell\",\n      \"name\": \"ssh\"\n    },\n    {\n      \"code\": \"https://github.com/Syncano/syncano-gcm-example.git\",\n      \"language\": \"shell\",\n      \"name\": \"https\"\n    }\n  ]\n}\n[/block]\n1. Copy your google-services.json to your projects folder. If you used this code, you should override a file in app directory.\n2. Set your Syncano app API Key and Instance name in gradle.properties file.\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"syncano_api_key=\\\"INSTANCE_API_KEY\\\"\\nsyncano_instance=\\\"INSTANCE_NAME\\\"\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"3. Run your app\"\n}\n[/block]\n1. In the Android Studio, run the application. When run, the app will receive GCM registration id and register it on Syncano (You should see it in under **Push Devices** -> **Android Devices** in the Dashboard). It's also written to logcat.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/g7O5bGp2Qd2tPfIK1S08_gcm_devices_list.png\",\n        \"gcm_devices_list.png\",\n        \"2558\",\n        \"1310\",\n        \"#254a7c\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n2. Send GCM messages to chosen registration ids. You can learn how to do it in [Sending Push Notifications](sending-push-notifications) chapter. You can also send a message by making a curl call:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"curl -X POST \\\\\\n-H \\\"X-API-KEY: ACCOUNT_KEY\\\" \\\\\\n-H \\\"Content-Type: application/json\\\" \\\\\\n-d '{ \\\"content\\\": { \\\"environment\\\": \\\"development\\\", \\\"registration_ids\\\":[\\\"REGISTRATION_ID\\\"], \\\"data\\\": {\\\"message\\\":\\\"sample message\\\"} } }' \\\\\\n\\\"https://api.syncano.io/v1.1/instances/INSTANCE_NAME/push_notifications/gcm/messages/\\\" \",\n      \"language\": \"curl\"\n    }\n  ]\n}\n[/block]\n3. Messages should appear in your Android notifications bar.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Api keys used in this example\"\n}\n[/block]\n- Syncano Account API key - It is an administrator key, that can make any changes in his Syncano Instances.\n- Syncano Instance API key - It's configured api key, it is hardcoded to the app and allowed to do specific things.\n- GCM Server API key - Key that is necessary to communicate with GCM servers, used by Syncano, not an app.\n- Registration id / token - It's an id given by GCM to specific app on specific device. Using this ids you can send messages to chosen devices.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Syncano related code in the app\"\n}\n[/block]\nGradle files\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"syncano_api_key=\\\"INSTANCE_API_KEY\\\"\\nsyncano_instance=\\\"INSTANCE_NAME\\\"\",\n      \"language\": \"json\",\n      \"name\": \"gradle.properties\"\n    }\n  ]\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"dependencies {\\n    compile 'io.syncano:library:4.1.0'\\n}\",\n      \"language\": \"json\",\n      \"name\": \"build.gradle\"\n    }\n  ]\n}\n[/block]\nSampleApplication.java - Initiating Syncano singleton\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"new SyncanoBuilder().androidContext(getApplicationContext()).apiKey(BuildConfig.API_KEY).instanceName(BuildConfig.INSTANCE_NAME).setAsGlobalInstance(true).useLoggedUserStorage(true).build();\",\n      \"language\": \"java\",\n      \"name\": \"SampleApplication.java\"\n    }\n  ]\n}\n[/block]\nGetGCMTokenTask.java - Registering device registration id\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"Syncano.getInstance().registerPushDevice(new PushDevice(token)).send();\",\n      \"language\": \"java\",\n      \"name\": \"GetGCMTokenTask.java\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"More information\"\n}\n[/block]\n- [GCM app example from Google](https://github.com/googlesamples/google-services/tree/master/android/gcm)\n- For questions not related to Syncano, but to GCM itself, check the [Google Cloud Messaging Docs](https://developers.google.com/cloud-messaging/)\n- [Sending Push Notifications](sending-push-notifications)","excerpt":"Guide on using Android GCM Push Notifications","slug":"android-push-notifications-app","type":"basic","title":"Android - Push Notifications App"}

Android - Push Notifications App

Guide on using Android GCM Push Notifications

[block:callout] { "type": "warning", "body": "Push Notifications are an experimental feature. If you find any issues, please write to us at [support@syncano.com](mailto:support@syncano.com)", "title": "BETA!" } [/block] Before you can start building and testing your app on device, you will need: - Syncano Account and Instance ([get started here](getting-started-with-syncano)) - Google Cloud Messaging and Syncano's Push Notifications Socket configured ([learn how to do it here](push-notification-sockets-android)) - `google-services.json` obtained from the steps done in the Config chapter If you have all of that, proceed to our short tutorial! 1. [Get Syncano API Instance name and API Key](#1-get-syncano-api-instance-name-and-api-key) 2. [Configure your app](#2-configure-your-app) 3. [Run your app](#3-run-your-app) [block:api-header] { "type": "basic", "title": "1. Get Syncano API Instance name and API Key" } [/block] 1. Click on your instance to enter it. 2. Copy its name and note it down - we will need it later. [block:image] { "images": [ { "image": [ "https://files.readme.io/tetBolRxRjS7rfZvyAb5_Instance_name.jpg", "Instance_name.jpg", "1230", "1472", "#274774", "" ] } ] } [/block] 3. Create an API Key with `Ignore ACL` permissions. Go to [Authentication & API Keys](http://docs.syncano.io/docs/authentication#section-creating-an-api-key) to learn how to do it. Alternatively, use the below curl call to create it. [block:code] { "codes": [ { "code": "curl -X POST \\\n-H \"X-API-KEY: ACCOUNT_KEY\" \\\n-H \"Content-type: application/json\" \\\n-d '{\"description\": \"GCM example app api key\", \"allow_anonymous_read\": false, \"allow_user_create\": true, \"ignore_acl\": true}' \\\n\"https://api.syncano.io/v1.1/instances/INSTANCE/api_keys/\"", "language": "curl" } ] } [/block] When your API Key is created, copy it and save somewhere - we will need it in a second. [block:api-header] { "type": "basic", "title": "2. Configure your app" } [/block] You will need the application source first. You can get it from [syncano-gcm-example](https://github.com/Syncano/syncano-gcm-example) repository on Github or just copy the line below to your terminal and press enter: [block:code] { "codes": [ { "code": "git clone git@github.com:Syncano/syncano-gcm-example.git", "language": "shell", "name": "ssh" }, { "code": "https://github.com/Syncano/syncano-gcm-example.git", "language": "shell", "name": "https" } ] } [/block] 1. Copy your google-services.json to your projects folder. If you used this code, you should override a file in app directory. 2. Set your Syncano app API Key and Instance name in gradle.properties file. [block:code] { "codes": [ { "code": "syncano_api_key=\"INSTANCE_API_KEY\"\nsyncano_instance=\"INSTANCE_NAME\"", "language": "java" } ] } [/block] [block:api-header] { "type": "basic", "title": "3. Run your app" } [/block] 1. In the Android Studio, run the application. When run, the app will receive GCM registration id and register it on Syncano (You should see it in under **Push Devices** -> **Android Devices** in the Dashboard). It's also written to logcat. [block:image] { "images": [ { "image": [ "https://files.readme.io/g7O5bGp2Qd2tPfIK1S08_gcm_devices_list.png", "gcm_devices_list.png", "2558", "1310", "#254a7c", "" ] } ] } [/block] 2. Send GCM messages to chosen registration ids. You can learn how to do it in [Sending Push Notifications](sending-push-notifications) chapter. You can also send a message by making a curl call: [block:code] { "codes": [ { "code": "curl -X POST \\\n-H \"X-API-KEY: ACCOUNT_KEY\" \\\n-H \"Content-Type: application/json\" \\\n-d '{ \"content\": { \"environment\": \"development\", \"registration_ids\":[\"REGISTRATION_ID\"], \"data\": {\"message\":\"sample message\"} } }' \\\n\"https://api.syncano.io/v1.1/instances/INSTANCE_NAME/push_notifications/gcm/messages/\" ", "language": "curl" } ] } [/block] 3. Messages should appear in your Android notifications bar. [block:api-header] { "type": "basic", "title": "Api keys used in this example" } [/block] - Syncano Account API key - It is an administrator key, that can make any changes in his Syncano Instances. - Syncano Instance API key - It's configured api key, it is hardcoded to the app and allowed to do specific things. - GCM Server API key - Key that is necessary to communicate with GCM servers, used by Syncano, not an app. - Registration id / token - It's an id given by GCM to specific app on specific device. Using this ids you can send messages to chosen devices. [block:api-header] { "type": "basic", "title": "Syncano related code in the app" } [/block] Gradle files [block:code] { "codes": [ { "code": "syncano_api_key=\"INSTANCE_API_KEY\"\nsyncano_instance=\"INSTANCE_NAME\"", "language": "json", "name": "gradle.properties" } ] } [/block] [block:code] { "codes": [ { "code": "dependencies {\n compile 'io.syncano:library:4.1.0'\n}", "language": "json", "name": "build.gradle" } ] } [/block] SampleApplication.java - Initiating Syncano singleton [block:code] { "codes": [ { "code": "new SyncanoBuilder().androidContext(getApplicationContext()).apiKey(BuildConfig.API_KEY).instanceName(BuildConfig.INSTANCE_NAME).setAsGlobalInstance(true).useLoggedUserStorage(true).build();", "language": "java", "name": "SampleApplication.java" } ] } [/block] GetGCMTokenTask.java - Registering device registration id [block:code] { "codes": [ { "code": "Syncano.getInstance().registerPushDevice(new PushDevice(token)).send();", "language": "java", "name": "GetGCMTokenTask.java" } ] } [/block] [block:api-header] { "type": "basic", "title": "More information" } [/block] - [GCM app example from Google](https://github.com/googlesamples/google-services/tree/master/android/gcm) - For questions not related to Syncano, but to GCM itself, check the [Google Cloud Messaging Docs](https://developers.google.com/cloud-messaging/) - [Sending Push Notifications](sending-push-notifications)