{"_id":"566e1ee5972a290d00552084","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"},"__v":18,"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"},"project":"55e67aaa9cc7c62b00c4a1ea","parentDoc":null,"user":"56035f358d58900d0051e976","updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-12-14T01:44:05.193Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":10,"body":"This method will give you the most control over how your application reacts to incoming push notifications. It is useful for when you want functionality such as the following:\n\n- If you want to work with the bundle data or collaborate with your app before posting a notification to the user.\n- If you want to silently receive data from a push notification without posting any notification to the user whatsoever.\n- If you want to action something before posting a notification, for example downloading something before telling the user there is new data available.\n- If you want to take advantage of specific notification utilities above the default ones Carnival provides.\n- Or any combination of anything you can think of! Its entirely up to you.\n[block:callout]\n{\n  \"type\": \"success\",\n  \"title\": \"Building your own\",\n  \"body\": \"Detailed instructions on how to build your own notifications can be found in the official [Android Notification documentation](http://developer.android.com/guide/topics/ui/notifiers/notifications.html).\"\n}\n[/block]\n## Create a Carnival Message Listener\n\nIn order to handle your own notification creation or push handling, create a new class that extends CarnivalMessageListener and implement the new onMessageReceived() method.\n\n`onMessageReceived()` gives you direct access to the Carnival Message related to that push notification, and is Parcelable if you want to include it in your Intent bundles.\n\nIf you are publishing your own notification here, return `true` to tell carnival that this push has been consumed, otherwise return `false` to allow Carnival to generate the notification for you.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"public class MyMessageReceiver extends CarnivalMessageListener {\\n    :::at:::Override\\n    public boolean onMessageReceived(Context context, Bundle bundle, Message message) {\\n        return false;\\n    }\\n}\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"`onMessageReceived()` is not called when publishing messages or pushes containing the `_u` deep linking attribute as these are handled specially by Carnival.\"\n}\n[/block]\n### Bundle Data\n\nThe bundle will contain all data that was attached to the push and can be used in building your notification.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/qsfGI6OTJSrapaZRlhwK_Screen%20Shot%202016-03-16%20at%204.17.23%20pm.png\",\n        \"Screen Shot 2016-03-16 at 4.17.23 pm.png\",\n        \"640\",\n        \"240\",\n        \"#ec4c7c\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\nThe above push notification would result in a bundle containing the following data:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"{\\n  \\\"alert\\\": \\\"<push message>\\\", \\n  \\\"badge\\\": \\\"5\\\", \\n  \\\"sound\\\": \\\"<sound name>\\\", \\n  \\\"<custom key>\\\": \\\"<custom value>\\\"\\n}\",\n      \"language\": \"json\"\n    }\n  ]\n}\n[/block]\n## Setting the Listener\n\nTell Carnival about your receiver by calling `setMessageReceivedListener()` and passing in the class reference of your receiver. Carnival will instantiate your message receiver when it receives a push notification, providing you with the relevant Context, Bundle, and Message data.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"Carnival.setMessageReceivedListener(MyMessageReceiver.class);\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]","excerpt":"Overriding Carnival's default notification responder with your own","slug":"android-custom-notification-handling","type":"basic","title":"Android Notification Handling"}

Android Notification Handling

Overriding Carnival's default notification responder with your own

This method will give you the most control over how your application reacts to incoming push notifications. It is useful for when you want functionality such as the following: - If you want to work with the bundle data or collaborate with your app before posting a notification to the user. - If you want to silently receive data from a push notification without posting any notification to the user whatsoever. - If you want to action something before posting a notification, for example downloading something before telling the user there is new data available. - If you want to take advantage of specific notification utilities above the default ones Carnival provides. - Or any combination of anything you can think of! Its entirely up to you. [block:callout] { "type": "success", "title": "Building your own", "body": "Detailed instructions on how to build your own notifications can be found in the official [Android Notification documentation](http://developer.android.com/guide/topics/ui/notifiers/notifications.html)." } [/block] ## Create a Carnival Message Listener In order to handle your own notification creation or push handling, create a new class that extends CarnivalMessageListener and implement the new onMessageReceived() method. `onMessageReceived()` gives you direct access to the Carnival Message related to that push notification, and is Parcelable if you want to include it in your Intent bundles. If you are publishing your own notification here, return `true` to tell carnival that this push has been consumed, otherwise return `false` to allow Carnival to generate the notification for you. [block:code] { "codes": [ { "code": "public class MyMessageReceiver extends CarnivalMessageListener {\n @Override\n public boolean onMessageReceived(Context context, Bundle bundle, Message message) {\n return false;\n }\n}", "language": "java" } ] } [/block] [block:callout] { "type": "info", "body": "`onMessageReceived()` is not called when publishing messages or pushes containing the `_u` deep linking attribute as these are handled specially by Carnival." } [/block] ### Bundle Data The bundle will contain all data that was attached to the push and can be used in building your notification. [block:image] { "images": [ { "image": [ "https://files.readme.io/qsfGI6OTJSrapaZRlhwK_Screen%20Shot%202016-03-16%20at%204.17.23%20pm.png", "Screen Shot 2016-03-16 at 4.17.23 pm.png", "640", "240", "#ec4c7c", "" ] } ] } [/block] The above push notification would result in a bundle containing the following data: [block:code] { "codes": [ { "code": "{\n \"alert\": \"<push message>\", \n \"badge\": \"5\", \n \"sound\": \"<sound name>\", \n \"<custom key>\": \"<custom value>\"\n}", "language": "json" } ] } [/block] ## Setting the Listener Tell Carnival about your receiver by calling `setMessageReceivedListener()` and passing in the class reference of your receiver. Carnival will instantiate your message receiver when it receives a push notification, providing you with the relevant Context, Bundle, and Message data. [block:code] { "codes": [ { "code": "Carnival.setMessageReceivedListener(MyMessageReceiver.class);", "language": "java" } ] } [/block]