{"_id":"571009b58245a40e008052e3","category":{"_id":"571006aa10300c0e007f6133","__v":0,"project":"54774d9af3736008009e9e0e","version":"56a0bd9e3697d80d002ac5e7","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-04-14T21:07:54.730Z","from_sync":false,"order":16,"slug":"push-notifications","title":"Push Notifications"},"project":"54774d9af3736008009e9e0e","user":"54774c47f3736008009e9e0c","version":{"_id":"56a0bd9e3697d80d002ac5e7","project":"54774d9af3736008009e9e0e","__v":20,"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"},"__v":36,"parentDoc":null,"updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-04-14T21:20:53.361Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":5,"body":"[block:callout]\n{\n  \"type\": \"warning\",\n  \"title\": \"BETA Feature\",\n  \"body\": \"Push Notifications are an experimental feature. If you find any issues, please write to us at [support:::at:::syncano.com](mailto:[email protected])\"\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Chapter Contents\"\n}\n[/block]\n1. [Overview](#overview)\n2. [ Sending Push Notification Messages to Android Devices](#sending-push-notification-messages-to-android-devi)\n3. [Sending Push Notification Messages to iOS Devices\n](#sending-push-notification-messages-to-ios-devices)\n4. [Sending Push Notification Messages to Many Devices](#sending-push-notification-messages-to-many-devices)\n5. [Listing Push Notification Messages](#listing-push-notification-messages)\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Overview\"\n}\n[/block]\nThis chapter will show you how to send notification messages to iOS and Android devices.\n[block:callout]\n{\n  \"type\": \"warning\",\n  \"title\": \"Configuration\",\n  \"body\": \"To be able to send push notifications to Android or iOS devices you'll need to configure corresponding Sockets first! You can read how to do this here:\\n- [GCM Socket Configuration (Android)](http://docs.syncano.io/v1.1/docs/push-notification-sockets-android)\\n- [APNs Socket Configuration (iOS)](http://docs.syncano.io/v1.1/docs/push-notification-sockets-ios)\"\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Sending Push Notification Messages to Android Devices\"\n}\n[/block]\n1. Select Android Devices from the left menu.\n2. Click on the list item dropdown and choose \"Send Message\" option. (Alternatively you can select multiple devices)\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/AagPjH2WSVyCL7AGIDTQ_gcm_syncano_send_00.png\",\n        \"gcm_syncano_send_00.png\",\n        \"2558\",\n        \"1308\",\n        \"#274a7b\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n3. In the Dialog select:\n    - The Certificate type. `Development` will send messages to your test devices and `Production` is reserved for published applications.\n    - Type a name of your app that should be displayed along with the message\n    - Type message content\n4. Click \"Confirm\" button\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/wCCpJdNAQnWs6Hijm1zL_gcm_syncano_send_01.png\",\n        \"gcm_syncano_send_01.png\",\n        \"2558\",\n        \"1308\",\n        \"#277ed3\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"Hint!\",\n  \"body\": \"If you want to send a message to a device that has our example app installed:\\n1. Toggle the `JSON message` field\\n2. Paste the below code to the editor:\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"{\\n  \\\"data\\\":\\n    {\\n      \\\"message\\\": \\\"this line will be displayed in logcat\\\"\\n    }\\n}\",\n      \"language\": \"json\"\n    }\n  ]\n}\n[/block]\nAlternatively, you can use a cURL or the Javascript library to send a notification message:\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/push_notifications/gcm/messages/\\\"\",\n      \"language\": \"curl\"\n    },\n    {\n      \"code\": \"var Syncano = require('syncano');\\n\\nvar connection = Syncano({ accountKey: 'ACCOUNT_KEY' });\\nvar GCMDevice = connection.GCMDevice;\\n\\nvar query = {\\n registration_id: 'REGISTRATION_ID',\\n instanceName: 'INSTANCE_NAME'\\n};\\n\\nvar message = {\\n  environment: 'development',\\n  data: {\\n    message: 'sample message'\\n  }\\n}\\n\\nGCMDevice.please().sendMessage(query, message).then(callback);\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n\nThat's it! If you are testing the workflow your message should show up in the logcat console!\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Sending Push Notification Messages to iOS Devices\"\n}\n[/block]\n1. Select iOS Devices from the left menu.\n2. Click on the list item dropdown and choose \"Send Message\" option. (Alternatively you can select multiple devices)\n\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/mJSqm4SETBibOF0jwJhO_apns_send_message_00.png\",\n        \"apns_send_message_00.png\",\n        \"1439\",\n        \"739\",\n        \"#264b7c\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n3. In the Dialog select:\n    - Sandbox toggle. If it's `on`, the messages will be sent to your test devices. Toggle it to `off` status will allow sending to real-life production devices. \n    - Type a name of your app that should be displayed along with the message\n    - Type message content\n4. Click \"Confirm\" button\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/AsSjKvQXS3uZagKfCE4f_apns_send_message_01.png\",\n        \"apns_send_message_01.png\",\n        \"1439\",\n        \"741\",\n        \"#336ba4\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\nYour message will be sent to the registered iOS devices.\n\nYou can also send messages to registered iOS devices using the Javascript library:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var Syncano = require('syncano');\\n\\nvar connection = Syncano({ accountKey: 'ACCOUNT_KEY' });\\nvar APNSDevice = connection.APNSDevice;\\n\\nvar query = {\\n registration_id: 'REGISTRATION_ID',\\n instanceName: 'INSTANCE_NAME'\\n};\\n\\nvar message = {\\n  environment: 'development',\\n  aps: {\\n    alert: 'sample message'\\n  }\\n}\\n\\nAPNSDevice.please().sendMessage(query, message).then(callback);\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Sending Push Notification Messages to Many Devices\"\n}\n[/block]\nIn case you'd like to send a message to all your devices, you can use the code below:\n1. Copy the code to a NodeJS or Python Snippet Script\n2. Change `INSTANCE_NAME` and `API_KEY` to your credentials\n3. Add the data you would like to send to `content` object\n4. Click 'Run' to execute the Script. Your message will be sent to all devices (GCM in this case).\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var Syncano = require('syncano');\\n\\nvar connection = Syncano({ \\n  accountKey: 'API_KEY',\\n  defaults: {\\n    instanceName: 'INSTANCE_NAME'\\n  }\\n});\\n\\nvar GCMDevice = connection.GCMDevice;\\n\\n// Temporary array for registration ids\\n\\nvar registration_ids = [];\\n\\n// Fetch all devices\\n\\nvar allDevices = GCMDevice.please().all();\\n\\n// Finished fetching\\n\\nallDevices.on('stop', function(devices) {  \\n\\n  // Populate the array with acutual reigstration ids\\n  registration_ids = devices.map(function(device) {\\n    return device.registration_id;\\n  });\\n\\n  // Send messages to devices\\n  GCMDevice.please().sendMessages({}, {\\n    environment: 'development',\\n    registration_ids: registration_ids,\\n    data: {\\n      title:'Title goes here',\\n      body:'Msg goes here'\\n    }\\n  })\\n  .then(callback);\\n});\",\n      \"language\": \"javascript\"\n    },\n    {\n      \"code\": \"from itertools import izip_longest\\n\\nimport syncano\\n\\nconnection = syncano.connect(instance_name='INSTANCE_NAME', api_key='API_KEY')\\nGCMDevice = connection.GCMDevice\\nGCMMessage = connection.GCMMessage\\n\\n\\ndef grouper(iterable, n, fillvalue=None):\\n    args = [iter(iterable)] * n\\n    return izip_longest(fillvalue=fillvalue, *args)\\n\\nfor devices in grouper(GCMDevice.please.all(), 1000):\\n    content = {\\n        'registration_ids': [device.registration_id for device in devices if device],\\n        'data': {\\n            'example_data_one': 1,\\n            'example_data_two': 2\\n        }\\n    }\\n\\n    message = GCMMessage(content=content)\\n    message.save()\",\n      \"language\": \"python\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Listing Push Notification Messages\"\n}\n[/block]\nCurrently there's no option to view the Push Notification history from the Dashboard but you can query the API Directly:\n- Go to API Reference and use \"Try it out\" feature\n[GCM Messages Endpoint](http://docs.syncano.io/v0.1.1/docs/gcm-messages-list)\n[APNS Messages Endpoint](http://docs.syncano.io/v0.1.1/docs/apns-messages-list)\n\n- You can also use cURL or the Javascript library:\n\nGCM\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"curl -X GET \\\\\\n-H \\\"X-API-KEY: ACCOUNT_KEY\\\" \\\\\\nhttps://api.syncano.io/v1/instances/INSTANCE_NAME/push_notifications/gcm/messages/ | python -m json.tool\",\n      \"language\": \"curl\"\n    },\n    {\n      \"code\": \"var Syncano = require('syncano');\\n\\nvar connection = Syncano({ accountKey: 'ACCOUNT_KEY' });\\nvar GCMMessage = connection.GCMMessage;\\n\\nvar query = {\\n instanceName: 'INSTANCE_NAME'\\n};\\n\\nGCMMessage.please().list(query).then(callback);\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nAPNS\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"curl -X GET \\\\\\n-H \\\"X-API-KEY: ACCOUNT_KEY\\\" \\\\\\nhttps://api.syncano.io/v1/instances/INSTANCE_NAME/push_notifications/apns/messages/ | python -m json.tool\",\n      \"language\": \"curl\"\n    },\n    {\n      \"code\": \"var Syncano = require('syncano');\\n\\nvar connection = Syncano({ accountKey: 'ACCOUNT_KEY' });\\nvar APNSMessage = connection.APNSMessage;\\n\\nvar query = {\\n instanceName: 'INSTANCE_NAME'\\n};\\n\\nAPNSMessage.please().list(query).then(callback);\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]","excerpt":"Send Push Notifications to Android and iOS devices using a Push Notification Socket","slug":"sending-push-notifications","type":"basic","title":"Sending Push Notifications"}

Sending Push Notifications

Send Push Notifications to Android and iOS devices using a Push Notification Socket

[block:callout] { "type": "warning", "title": "BETA Feature", "body": "Push Notifications are an experimental feature. If you find any issues, please write to us at [[email protected]](mailto:[email protected])" } [/block] [block:api-header] { "type": "basic", "title": "Chapter Contents" } [/block] 1. [Overview](#overview) 2. [ Sending Push Notification Messages to Android Devices](#sending-push-notification-messages-to-android-devi) 3. [Sending Push Notification Messages to iOS Devices ](#sending-push-notification-messages-to-ios-devices) 4. [Sending Push Notification Messages to Many Devices](#sending-push-notification-messages-to-many-devices) 5. [Listing Push Notification Messages](#listing-push-notification-messages) [block:api-header] { "type": "basic", "title": "Overview" } [/block] This chapter will show you how to send notification messages to iOS and Android devices. [block:callout] { "type": "warning", "title": "Configuration", "body": "To be able to send push notifications to Android or iOS devices you'll need to configure corresponding Sockets first! You can read how to do this here:\n- [GCM Socket Configuration (Android)](http://docs.syncano.io/v1.1/docs/push-notification-sockets-android)\n- [APNs Socket Configuration (iOS)](http://docs.syncano.io/v1.1/docs/push-notification-sockets-ios)" } [/block] [block:api-header] { "type": "basic", "title": "Sending Push Notification Messages to Android Devices" } [/block] 1. Select Android Devices from the left menu. 2. Click on the list item dropdown and choose "Send Message" option. (Alternatively you can select multiple devices) [block:image] { "images": [ { "image": [ "https://files.readme.io/AagPjH2WSVyCL7AGIDTQ_gcm_syncano_send_00.png", "gcm_syncano_send_00.png", "2558", "1308", "#274a7b", "" ] } ] } [/block] 3. In the Dialog select: - The Certificate type. `Development` will send messages to your test devices and `Production` is reserved for published applications. - Type a name of your app that should be displayed along with the message - Type message content 4. Click "Confirm" button [block:image] { "images": [ { "image": [ "https://files.readme.io/wCCpJdNAQnWs6Hijm1zL_gcm_syncano_send_01.png", "gcm_syncano_send_01.png", "2558", "1308", "#277ed3", "" ] } ] } [/block] [block:callout] { "type": "info", "title": "Hint!", "body": "If you want to send a message to a device that has our example app installed:\n1. Toggle the `JSON message` field\n2. Paste the below code to the editor:" } [/block] [block:code] { "codes": [ { "code": "{\n \"data\":\n {\n \"message\": \"this line will be displayed in logcat\"\n }\n}", "language": "json" } ] } [/block] Alternatively, you can use a cURL or the Javascript library to send a notification message: [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/push_notifications/gcm/messages/\"", "language": "curl" }, { "code": "var Syncano = require('syncano');\n\nvar connection = Syncano({ accountKey: 'ACCOUNT_KEY' });\nvar GCMDevice = connection.GCMDevice;\n\nvar query = {\n registration_id: 'REGISTRATION_ID',\n instanceName: 'INSTANCE_NAME'\n};\n\nvar message = {\n environment: 'development',\n data: {\n message: 'sample message'\n }\n}\n\nGCMDevice.please().sendMessage(query, message).then(callback);", "language": "javascript" } ] } [/block] That's it! If you are testing the workflow your message should show up in the logcat console! [block:api-header] { "type": "basic", "title": "Sending Push Notification Messages to iOS Devices" } [/block] 1. Select iOS Devices from the left menu. 2. Click on the list item dropdown and choose "Send Message" option. (Alternatively you can select multiple devices) [block:image] { "images": [ { "image": [ "https://files.readme.io/mJSqm4SETBibOF0jwJhO_apns_send_message_00.png", "apns_send_message_00.png", "1439", "739", "#264b7c", "" ] } ] } [/block] 3. In the Dialog select: - Sandbox toggle. If it's `on`, the messages will be sent to your test devices. Toggle it to `off` status will allow sending to real-life production devices. - Type a name of your app that should be displayed along with the message - Type message content 4. Click "Confirm" button [block:image] { "images": [ { "image": [ "https://files.readme.io/AsSjKvQXS3uZagKfCE4f_apns_send_message_01.png", "apns_send_message_01.png", "1439", "741", "#336ba4", "" ] } ] } [/block] Your message will be sent to the registered iOS devices. You can also send messages to registered iOS devices using the Javascript library: [block:code] { "codes": [ { "code": "var Syncano = require('syncano');\n\nvar connection = Syncano({ accountKey: 'ACCOUNT_KEY' });\nvar APNSDevice = connection.APNSDevice;\n\nvar query = {\n registration_id: 'REGISTRATION_ID',\n instanceName: 'INSTANCE_NAME'\n};\n\nvar message = {\n environment: 'development',\n aps: {\n alert: 'sample message'\n }\n}\n\nAPNSDevice.please().sendMessage(query, message).then(callback);", "language": "javascript" } ] } [/block] [block:api-header] { "type": "basic", "title": "Sending Push Notification Messages to Many Devices" } [/block] In case you'd like to send a message to all your devices, you can use the code below: 1. Copy the code to a NodeJS or Python Snippet Script 2. Change `INSTANCE_NAME` and `API_KEY` to your credentials 3. Add the data you would like to send to `content` object 4. Click 'Run' to execute the Script. Your message will be sent to all devices (GCM in this case). [block:code] { "codes": [ { "code": "var Syncano = require('syncano');\n\nvar connection = Syncano({ \n accountKey: 'API_KEY',\n defaults: {\n instanceName: 'INSTANCE_NAME'\n }\n});\n\nvar GCMDevice = connection.GCMDevice;\n\n// Temporary array for registration ids\n\nvar registration_ids = [];\n\n// Fetch all devices\n\nvar allDevices = GCMDevice.please().all();\n\n// Finished fetching\n\nallDevices.on('stop', function(devices) { \n\n // Populate the array with acutual reigstration ids\n registration_ids = devices.map(function(device) {\n return device.registration_id;\n });\n\n // Send messages to devices\n GCMDevice.please().sendMessages({}, {\n environment: 'development',\n registration_ids: registration_ids,\n data: {\n title:'Title goes here',\n body:'Msg goes here'\n }\n })\n .then(callback);\n});", "language": "javascript" }, { "code": "from itertools import izip_longest\n\nimport syncano\n\nconnection = syncano.connect(instance_name='INSTANCE_NAME', api_key='API_KEY')\nGCMDevice = connection.GCMDevice\nGCMMessage = connection.GCMMessage\n\n\ndef grouper(iterable, n, fillvalue=None):\n args = [iter(iterable)] * n\n return izip_longest(fillvalue=fillvalue, *args)\n\nfor devices in grouper(GCMDevice.please.all(), 1000):\n content = {\n 'registration_ids': [device.registration_id for device in devices if device],\n 'data': {\n 'example_data_one': 1,\n 'example_data_two': 2\n }\n }\n\n message = GCMMessage(content=content)\n message.save()", "language": "python" } ] } [/block] [block:api-header] { "type": "basic", "title": "Listing Push Notification Messages" } [/block] Currently there's no option to view the Push Notification history from the Dashboard but you can query the API Directly: - Go to API Reference and use "Try it out" feature [GCM Messages Endpoint](http://docs.syncano.io/v0.1.1/docs/gcm-messages-list) [APNS Messages Endpoint](http://docs.syncano.io/v0.1.1/docs/apns-messages-list) - You can also use cURL or the Javascript library: GCM [block:code] { "codes": [ { "code": "curl -X GET \\\n-H \"X-API-KEY: ACCOUNT_KEY\" \\\nhttps://api.syncano.io/v1/instances/INSTANCE_NAME/push_notifications/gcm/messages/ | python -m json.tool", "language": "curl" }, { "code": "var Syncano = require('syncano');\n\nvar connection = Syncano({ accountKey: 'ACCOUNT_KEY' });\nvar GCMMessage = connection.GCMMessage;\n\nvar query = {\n instanceName: 'INSTANCE_NAME'\n};\n\nGCMMessage.please().list(query).then(callback);", "language": "javascript" } ] } [/block] APNS [block:code] { "codes": [ { "code": "curl -X GET \\\n-H \"X-API-KEY: ACCOUNT_KEY\" \\\nhttps://api.syncano.io/v1/instances/INSTANCE_NAME/push_notifications/apns/messages/ | python -m json.tool", "language": "curl" }, { "code": "var Syncano = require('syncano');\n\nvar connection = Syncano({ accountKey: 'ACCOUNT_KEY' });\nvar APNSMessage = connection.APNSMessage;\n\nvar query = {\n instanceName: 'INSTANCE_NAME'\n};\n\nAPNSMessage.please().list(query).then(callback);", "language": "javascript" } ] } [/block]