{"_id":"59d298bcbc292f0030b13792","project":"55e67aaa9cc7c62b00c4a1ea","version":{"_id":"55e67aab9cc7c62b00c4a1ed","project":"55e67aaa9cc7c62b00c4a1ea","__v":10,"createdAt":"2015-09-02T04:27:23.612Z","releaseDate":"2015-09-02T04:27:23.612Z","categories":["55e67aac9cc7c62b00c4a1ee","55e67b5556007d23005fee7d","55e67b5dde6fef23009480ca","55e680efde6fef23009480db","55e6829485a9741900314e99","561c61b4ad272c0d00a892df","586c014c0abf1d0f000d04d4","58991d2ad207df0f0002186b","589b8e1fdbb7cd190026732c","58b8ca5e3265d70f001788d4"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"category":{"_id":"589b8e1fdbb7cd190026732c","project":"55e67aaa9cc7c62b00c4a1ea","__v":0,"version":"55e67aab9cc7c62b00c4a1ed","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2017-02-08T21:31:11.878Z","from_sync":false,"order":6,"slug":"advanced-techniques","title":"Advanced Techniques"},"user":"587fed3d9efedf3b00200366","__v":0,"updates":[],"next":{"pages":[],"description":""},"createdAt":"2017-10-02T19:51:24.057Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"settings":"","results":{"codes":[]},"auth":"required","params":[],"url":""},"isReference":false,"order":999,"body":"[block:api-header]\n{\n  \"title\": \"Customizing your default channel\"\n}\n[/block]\nIf you want to provide custom behavior for your notifications - eg. Notification lights, vibrate patterns, custom sound effects, etc, then on devices using Android 8.0 you should do this through the `NotificationChannel` rather than through Carnival's `NotificationConfig` class. If you're targeting versions below 8.0 (API Level 26), then you should probably provide fallback configuration using `NotificationConfig`. This can be done like so:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"NotificationConfig config = new NotificationConfig();\\nif (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {\\n  \\tNotificationChannel channel = new NotificationChannel(\\\"notifications_default\\\", \\\"Carnival Notifications\\\", NotificationManager.IMPORTANCE_DEFAULT);\\n  \\tchannel.enableLights(true);\\n  \\tchannel.setLightColor(Color.RED);\\n  \\tchannel.enableVibration(true);\\n  \\tchannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});\\n  \\tconfig.setDefaultNotificationChannel(channel);\\n}\\n\\nconfig.setLights(Color.RED, 500, 500);\\nconfig.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});\\n\\nCarnival.setNotificationConfig(config);\\nCarnival.startEngine(getApplicationContext(), \\\"SDK_KEY\\\");\",\n      \"language\": \"java\",\n      \"name\": \"Android (Java)\"\n    }\n  ]\n}\n[/block]\nThis means that notifications will have the same behavior on both pre- and post-Oreo devices.\n\nNote that the nature of channels is that developers can only set presets - this is how your channel will start when your user installs the app. After that, they'll be able to change any and all of the settings that you've set above.\n[block:api-header]\n{\n  \"title\": \"Using multiple channels\"\n}\n[/block]\nThe addition of channels in Android Oreo means that it's easier for you to split notifications out into categories, and to alert users in specific ways for specific reasons.\n\nFor example, a news app might create channels for breaking news, political news, and weather. These can all be set up differently so that the user gets different kinds of notifications for each channel, as illustrated below\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {\\n  NotificationChannel breaking = new NotificationChannel(\\\"breaking\\\", \\\"Breaking News\\\", NotificationManager.IMPORTANCE_HIGH);\\n  breaking.enableLights(true);\\n  breaking.enableVibration(true);\\n  breaking.setLightColor(Color.WHITE);\\n  breaking.setVibrationPattern(new long[]{100, 200, 100, 200, 100, 200, 100});\\n\\n  config.setDefaultNotificationChannel(breaking);\\n\\n  NotificationChannel politics = new NotificationChannel(\\\"politics\\\", \\\"Political News\\\", NotificationManager.IMPORTANCE_DEFAULT);\\n  politics.enableLights(true);\\n  politics.enableVibration(true);\\n  politics.setLightColor(Color.BLUE);\\n  politics.setVibrationPattern(new long[]{100, 200, 100, 200, 100});\\n\\n  NotificationChannel weather = new NotificationChannel(\\\"weather\\\", \\\"Weather Updates\\\", NotificationManager.IMPORTANCE_DEFAULT);\\n  weather.enableLights(true);\\n  weather.enableVibration(true);\\n  weather.setLightColor(Color.GREEN);\\n  weather.setVibrationPattern(new long[]{100, 200, 100, 150, 100});\\n\\n  NotificationManager notificationManager = (NotificationManager)getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);\\n  notificationManager.createNotificationChannel(breaking);\\n  notificationManager.createNotificationChannel(politics);\\n  notificationManager.createNotificationChannel(weather);\\n}\",\n      \"language\": \"java\",\n      \"name\": \"Android (Java)\"\n    }\n  ]\n}\n[/block]\nNote that we've set the `breaking` channel as the Carnival default one on line 9. This means that any messages sent without channel IDs, or with ones that don't match those on the device being sent to, will be sent through the `breaking` channel.\n[block:api-header]\n{\n  \"title\": \"Sending to specific channels\"\n}\n[/block]\nAs mentioned above, if you send a message without a channel ID specified, it'll go through your default channel. If you'd like to send to a specific channel, however, it can be done easily through both the Carnival UI and APIs.\n\nIn the Carnival UI, when creating a push notification, simply click on \"Add Fields\", then \"Custom Key/Value\". The key should be set to `_channel_id`, and the value to the ID of the channel you wish to send to. For example, to send to the `breaking` channel, your push would look like this in the UI:\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/85023b3-Screen_Shot_2017-09-27_at_11.39.03_AM.png\",\n        \"Screen Shot 2017-09-27 at 11.39.03 AM.png\",\n        473,\n        219,\n        \"#e5edf3\"\n      ]\n    }\n  ]\n}\n[/block]\nUsing channels with using Carnival's Notifications API is similarly straightforward:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"curl -X POST -u :API_KEY -H \\\"Content-type: application/json\\\" -H 'Accept: application/json' https://api.carnivalmobile.com/v5/notifications -d ' {\\n  \\\"notification\\\": {\\n    \\\"to\\\":  \\\"*\\\",\\n    \\\"payload\\\": {\\n      \\\"alert\\\": \\\"Man amends previous comment, says 'All dogs are good'\\\",\\n      \\\"_channel_id\\\": \\\"breaking\\\" // Channel\\n    }\\n  }\\n}'\",\n      \"language\": \"curl\"\n    }\n  ]\n}\n[/block]\nMore information on Carnival's Notifications API can be found [here](https://docs.carnival.io/v1.0/docs/notifications).","excerpt":"","slug":"android-notification-channels-1","type":"basic","title":"Android Notification Channels"}

Android Notification Channels


[block:api-header] { "title": "Customizing your default channel" } [/block] If you want to provide custom behavior for your notifications - eg. Notification lights, vibrate patterns, custom sound effects, etc, then on devices using Android 8.0 you should do this through the `NotificationChannel` rather than through Carnival's `NotificationConfig` class. If you're targeting versions below 8.0 (API Level 26), then you should probably provide fallback configuration using `NotificationConfig`. This can be done like so: [block:code] { "codes": [ { "code": "NotificationConfig config = new NotificationConfig();\nif (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {\n \tNotificationChannel channel = new NotificationChannel(\"notifications_default\", \"Carnival Notifications\", NotificationManager.IMPORTANCE_DEFAULT);\n \tchannel.enableLights(true);\n \tchannel.setLightColor(Color.RED);\n \tchannel.enableVibration(true);\n \tchannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});\n \tconfig.setDefaultNotificationChannel(channel);\n}\n\nconfig.setLights(Color.RED, 500, 500);\nconfig.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});\n\nCarnival.setNotificationConfig(config);\nCarnival.startEngine(getApplicationContext(), \"SDK_KEY\");", "language": "java", "name": "Android (Java)" } ] } [/block] This means that notifications will have the same behavior on both pre- and post-Oreo devices. Note that the nature of channels is that developers can only set presets - this is how your channel will start when your user installs the app. After that, they'll be able to change any and all of the settings that you've set above. [block:api-header] { "title": "Using multiple channels" } [/block] The addition of channels in Android Oreo means that it's easier for you to split notifications out into categories, and to alert users in specific ways for specific reasons. For example, a news app might create channels for breaking news, political news, and weather. These can all be set up differently so that the user gets different kinds of notifications for each channel, as illustrated below [block:code] { "codes": [ { "code": "if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {\n NotificationChannel breaking = new NotificationChannel(\"breaking\", \"Breaking News\", NotificationManager.IMPORTANCE_HIGH);\n breaking.enableLights(true);\n breaking.enableVibration(true);\n breaking.setLightColor(Color.WHITE);\n breaking.setVibrationPattern(new long[]{100, 200, 100, 200, 100, 200, 100});\n\n config.setDefaultNotificationChannel(breaking);\n\n NotificationChannel politics = new NotificationChannel(\"politics\", \"Political News\", NotificationManager.IMPORTANCE_DEFAULT);\n politics.enableLights(true);\n politics.enableVibration(true);\n politics.setLightColor(Color.BLUE);\n politics.setVibrationPattern(new long[]{100, 200, 100, 200, 100});\n\n NotificationChannel weather = new NotificationChannel(\"weather\", \"Weather Updates\", NotificationManager.IMPORTANCE_DEFAULT);\n weather.enableLights(true);\n weather.enableVibration(true);\n weather.setLightColor(Color.GREEN);\n weather.setVibrationPattern(new long[]{100, 200, 100, 150, 100});\n\n NotificationManager notificationManager = (NotificationManager)getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);\n notificationManager.createNotificationChannel(breaking);\n notificationManager.createNotificationChannel(politics);\n notificationManager.createNotificationChannel(weather);\n}", "language": "java", "name": "Android (Java)" } ] } [/block] Note that we've set the `breaking` channel as the Carnival default one on line 9. This means that any messages sent without channel IDs, or with ones that don't match those on the device being sent to, will be sent through the `breaking` channel. [block:api-header] { "title": "Sending to specific channels" } [/block] As mentioned above, if you send a message without a channel ID specified, it'll go through your default channel. If you'd like to send to a specific channel, however, it can be done easily through both the Carnival UI and APIs. In the Carnival UI, when creating a push notification, simply click on "Add Fields", then "Custom Key/Value". The key should be set to `_channel_id`, and the value to the ID of the channel you wish to send to. For example, to send to the `breaking` channel, your push would look like this in the UI: [block:image] { "images": [ { "image": [ "https://files.readme.io/85023b3-Screen_Shot_2017-09-27_at_11.39.03_AM.png", "Screen Shot 2017-09-27 at 11.39.03 AM.png", 473, 219, "#e5edf3" ] } ] } [/block] Using channels with using Carnival's Notifications API is similarly straightforward: [block:code] { "codes": [ { "code": "curl -X POST -u :API_KEY -H \"Content-type: application/json\" -H 'Accept: application/json' https://api.carnivalmobile.com/v5/notifications -d ' {\n \"notification\": {\n \"to\": \"*\",\n \"payload\": {\n \"alert\": \"Man amends previous comment, says 'All dogs are good'\",\n \"_channel_id\": \"breaking\" // Channel\n }\n }\n}'", "language": "curl" } ] } [/block] More information on Carnival's Notifications API can be found [here](https://docs.carnival.io/v1.0/docs/notifications).