{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","params":[],"results":{"codes":[]},"settings":"","apiSetting":null},"next":{"description":"","pages":[]},"title":"Building a Message Stream","type":"basic","slug":"developing-a-custom-message-stream","excerpt":"","body":"## Retrieving messages for a user\n\nSailthru Mobile Messages have several properties which you should familiarize yourself with before building a stream or displaying messages.\n[block:parameters]\n{\n  \"data\": {\n    \"h-0\": \"Property name\",\n    \"h-1\": \"Can be blank\",\n    \"h-2\": \"Description\",\n    \"0-0\": \"`title`\",\n    \"0-1\": \"No\",\n    \"0-2\": \"The title of the message\",\n    \"1-0\": \"`text`\",\n    \"1-1\": \"Yes\",\n    \"1-2\": \"The body of the message\",\n    \"2-0\": \"`htmlText`\",\n    \"2-1\": \"Yes\",\n    \"2-2\": \"The body of the message rendered into an HTML string\",\n    \"3-0\": \"`imageURL`\",\n    \"3-1\": \"Yes\",\n    \"3-2\": \"A URL pointing to an image\",\n    \"4-0\": \"`videoURL` (iOS)\\n`mediaURL` (Android)\",\n    \"4-1\": \"Yes\",\n    \"4-2\": \"A URL pointing to a video\",\n    \"5-0\": \"`URL` (iOS)\\n`contentURL` (Android)\",\n    \"5-1\": \"Yes\",\n    \"5-2\": \"A URL pointing to a generic page or resource\",\n    \"6-0\": \"`read`\",\n    \"6-1\": \"No\",\n    \"6-2\": \"Whether or not the message has been read\",\n    \"7-0\": \"`createdAt`\",\n    \"7-1\": \"No\",\n    \"7-2\": \"The date and time when the message was created\",\n    \"8-0\": \"`messageID`\",\n    \"8-1\": \"No\",\n    \"8-2\": \"An alphanumeric unique ID for the message\",\n    \"9-0\": \"`attributes`\",\n    \"9-1\": \"Yes\",\n    \"9-2\": \"A dictionary of string keys and values set on the message\",\n    \"h-3\": \"Plugin SDK name\",\n    \"0-3\": \"`title`\",\n    \"1-3\": \"`text`\",\n    \"2-3\": \"`html_text`\",\n    \"3-3\": \"`card_image_url`\",\n    \"4-3\": \"`card_media_url`\",\n    \"5-3\": \"`url`\",\n    \"6-3\": \"`is_read`\",\n    \"7-3\": \"`created_at`\",\n    \"8-3\": \"`id`\",\n    \"9-3\": \"`attributes`\"\n  },\n  \"cols\": 4,\n  \"rows\": 10\n}\n[/block]\nYou can use the `MessageStream` interface to asynchronously request an array of messages for that device.  Once returned you can use these objects to construct your own Message Stream interface.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"//On iOS, using Objective-C\\n\\n[[STMMessageStream new] messages:^(NSArray *messages, NSError *error) {\\n\\t//Do something with the array of messages\\n}];\\n\",\n      \"language\": \"objectivec\",\n      \"name\": \"iOS (Objective-C)\"\n    },\n    {\n      \"code\": \"STMMessageStream().messages { (theMessages, anError) -> Void in\\n\\t//Do something with the array of messages\\n}\",\n      \"language\": \"swift\",\n      \"name\": \"iOS (Swift)\"\n    },\n    {\n      \"code\": \"new MessageStream().getMessages(new MessageStream.MessagesHandler() {\\n  :::at:::Override\\n  public void onSuccess(ArrayList<Message> messages) {\\n    //Do something with the array of messages\\n  }\\n\\n  @Override\\n  public void onFailure(Error error) {\\n  }\\n});\",\n      \"language\": \"java\",\n      \"name\": \"Android (Java)\"\n    },\n    {\n      \"code\": \"MessageStream().getMessages(object : MessageStream.MessagesHandler {\\n    override fun onSuccess(messages: ArrayList<Message>?) {\\n      //Do something with the array of messages\\n    }\\n\\n    override fun onFailure(error: Error?) {\\n    }\\n})\",\n      \"language\": \"kotlin\",\n      \"name\": \"Android (Kotlin)\"\n    },\n    {\n      \"code\": \"SailthruMobile.getMessages().then(messages => {\\n  //Do something with the array of messages\\n}).catch(e => {\\n  //Handle error\\n});\",\n      \"language\": \"javascript\",\n      \"name\": \"React Native (JavaScript)\"\n    }\n  ]\n}\n[/block]\n## Showing full screen content\n\nAfter a user taps on message, you can can show the full screen content with the following method.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"[[STMMessageStream new] presentMessageDetailForMessage:self.latestMessage];\",\n      \"language\": \"objectivec\",\n      \"name\": \"iOS (Objective-C)\"\n    },\n    {\n      \"code\": \"STMMessageStream().presentDetailForMessage(message)\",\n      \"language\": \"swift\",\n      \"name\": \"iOS (Swift)\"\n    },\n    {\n      \"code\": \"Intent intent = MessageActivity.intentForMessage(context, bundle, message);\\nstartActivity(intent);\",\n      \"language\": \"java\",\n      \"name\": \"Android (Java)\"\n    },\n    {\n      \"code\": \"val intent = MessageActivity.intentForMessage(context, bundle, message)\\nstartActivity(intent)\",\n      \"language\": \"kotlin\",\n      \"name\": \"Android (Kotlin)\"\n    },\n    {\n      \"code\": \"SailthruMobile.presentMessageDetail(message);\",\n      \"language\": \"javascript\",\n      \"name\": \"React Native (JavaScript)\"\n    }\n  ]\n}\n[/block]\n## Supporting Impressions\n\nIn order to support impression analytics from your Message Stream you will need to ensure you register an impression event for relevant messages when your Message Stream is shown to the user.  \n\nSailthru Mobile supports tracking **three** types of impressions:\n\n* In App Notification Impression\n* Message Viewed in Stream Impression\n* Message Viewed in Detail Impression\n\nIf you didn't customize Sailthru Mobile's default in-app notification or full-screen detail user interface, there is no need to register an impression event as these will be captured automatically.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"[[STMMessageStream new] registerImpressionWithType:\\n    STMImpressionTypeInAppNotificationView forMessage:message];\\n\\n[[STMMessageStream new] registerImpressionWithType:\\n    STMImpressionTypeStreamView forMessage:message];\\n\\n[[STMMessageStream new] registerImpressionWithType:\\n    STMImpressionTypeDetailView forMessage:message];\",\n      \"language\": \"objectivec\",\n      \"name\": \"iOS (Objective-C)\"\n    },\n    {\n      \"code\": \"STMMessageStream().registerImpressionWithType(.inAppNotificationView, forMessage: message)\\n    \\nSTMMessageStream().registerImpressionWithType(.streamView, forMessage: message)        \\n    \\nSTMMessageStream().registerImpressionWithType(.detailView, forMessage: message)\",\n      \"language\": \"swift\",\n      \"name\": \"iOS (Swift)\"\n    },\n    {\n      \"code\": \"new MessageStream().registerMessageImpression(ImpressionType.IMPRESSION_TYPE_IN_APP_VIEW, message);\\n\\nnew MessageStream().registerMessageImpression(ImpressionType.IMPRESSION_TYPE_STREAM_VIEW, message);\\n\\nnew MessageStream().registerMessageImpression(ImpressionType.IMPRESSION_TYPE_DETAIL_VIEW, message);\",\n      \"language\": \"java\",\n      \"name\": \"Android (Java)\"\n    },\n    {\n      \"code\": \"MessageStream().registerMessageImpression(ImpressionType.IMPRESSION_TYPE_IN_APP_VIEW, message)\\n\\n       MessageStream().registerMessageImpression(ImpressionType.IMPRESSION_TYPE_STREAM_VIEW, message)\\n\\n       MessageStream().registerMessageImpression(ImpressionType.IMPRESSION_TYPE_DETAIL_VIEW, message)\",\n      \"language\": \"kotlin\",\n      \"name\": \"Android (Kotlin)\"\n    },\n    {\n      \"code\": \"SailthruMobile.registerMessageImpression(SailthruMobile.MessageImpressionType.InAppView, message);\\n\\nSailthruMobile.registerMessageImpression(SailthruMobile.MessageImpressionType.StreamView, message);\\n\\nSailthruMobile.registerMessageImpression(SailthruMobile.MessageImpressionType.DetailView, message);\",\n      \"language\": \"javascript\",\n      \"name\": \"React Native (JavaScript)\"\n    }\n  ]\n}\n[/block]\n## Marking As Read\n\nYou might also want to track which messages have been read or not with your Message Stream, so you could show a number badge or unread indicator. You can mark a collection or single message as read. \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"[[STMMessageStream new] markMessageAsRead:message withResponse:NULL];\\n// or\\n[[STMMessageStream new] markMessagesAsRead:@[message1, message2, message3] withResponse:NULL];\",\n      \"language\": \"objectivec\",\n      \"name\": \"iOS (Objective-C)\"\n    },\n    {\n      \"code\": \"STMMessageStream().markMessageAsRead(message, withResponse: nil)\\n//or \\nSTMMessageStream().markMessagesAsRead(messages, withResponse: nil)\",\n      \"language\": \"swift\",\n      \"name\": \"iOS (Swift)\"\n    },\n    {\n      \"code\": \"new MessageStream().setMessageRead(message, messageReadHandler);\\n// or \\nnew MessageStream().setMessagesRead(messageList, messageReadHandler);\",\n      \"language\": \"java\",\n      \"name\": \"Android (Java)\"\n    },\n    {\n      \"code\": \"MessageStream().setMessageRead(message, messageReadHandler)\\n// or \\nMessageStream().setMessagesRead(messageList, messageReadHandler)\",\n      \"language\": \"kotlin\",\n      \"name\": \"Android (Kotlin)\"\n    },\n    {\n      \"code\": \"SailthruMobile.markMessageAsRead(message).then(result => {\\n  // Handle success\\n}).catch(e => {\\n  // Handle error\\n});\",\n      \"language\": \"javascript\",\n      \"name\": \"React Native (JavaScript)\"\n    }\n  ]\n}\n[/block]\n## Deleting Messages\n\nMessages can be deleted from a user's stream. \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"[[STMMessageStream new] removeMessage:message withResonse:^(NSError *error) {\\n\\t//Do something with any errors\\n}];\",\n      \"language\": \"objectivec\",\n      \"name\": \"iOS (Objective-C)\"\n    },\n    {\n      \"code\": \"STMMessageStream().removeMessage(message, withResponse: nil)\",\n      \"language\": \"swift\",\n      \"name\": \"iOS (Swift)\"\n    },\n    {\n      \"code\": \"new MessageStream().deleteMessage(message, messageDeletedHandler);\",\n      \"language\": \"java\",\n      \"name\": \"Android (Java)\"\n    },\n    {\n      \"code\": \"MessageStream().deleteMessage(message, messageDeletedHandler)\",\n      \"language\": \"kotlin\",\n      \"name\": \"Android (Kotlin)\"\n    },\n    {\n      \"code\": \"SailthruMobile.removeMessage(message).then(result => {\\n  // Handle success\\n}).catch(e => {\\n  // Handle error\\n});\",\n      \"language\": \"javascript\",\n      \"name\": \"React Native (JavaScript)\"\n    }\n  ]\n}\n[/block]\n## Custom In-App Message Notifications\n\nYou can customize the look and feel of In-App Message Notifications by following the instructions [here](doc:customizing-in-app-notifications). \n[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"While testing, you can reinstall the app to get a fresh set of messages, as impressions and deletions do not persist between same-device installs.\"\n}\n[/block]\n## Custom Message Attributes\n\nYou can use a dictionary or hash of custom attributes to further customize the display of your message stream and messages. These may include: \n\n * Pinned messages\n * Categories on the Stream\n * Custom CTAs or card design\n\n## HTML Support\nSince you can include [markdown](https://getstarted.sailthru.com/sailthru-mobile/messages/using-markdown/) in messages sent via Sailthru Mobile, we expose this markdown rendered into HTML with the `htmlText` property. \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"bodyLabel.attributedText = [[NSAttributedString alloc] initWithData:[message.htmlText dataUsingEncoding:NSUTF8StringEncoding]\\n                            options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,\\n                                 NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}\\n                 documentAttributes:nil error:nil];\",\n      \"language\": \"objectivec\",\n      \"name\": \"iOS (Objective-C)\"\n    },\n    {\n      \"code\": \"do {\\n    try bodyLabel.attributedText = NSAttributedString.init(\\n        data: message.htmlText.dataUsingEncoding(NSUnicodeStringEncoding)!,\\n        options: [\\n            NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,\\n            NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding\\n        ],\\n        documentAttributes: nil);\\n} catch {\\n    \\n}\",\n      \"language\": \"swift\",\n      \"name\": \"iOS (Swift)\"\n    },\n    {\n      \"code\": \"TextView textView = ...;\\ntextView.setText(Html.fromHtml(message.getHtmlText()));\",\n      \"language\": \"java\",\n      \"name\": \"Android (Java)\"\n    },\n    {\n      \"code\": \"val textView = ...\\ntextView.text = Html.fromHtml(message.htmlText, Html.FROM_HTML_MODE_COMPACT)\",\n      \"language\": \"kotlin\",\n      \"name\": \"Android (Kotlin)\"\n    }\n  ]\n}\n[/block]\nHTML Text will always be populated, even if no markdown was used in the original message.\n\n## Unread messages count\nYou can get the number of unread messages from the SDK, without the need to request a message stream and iterate over each individual message.\n\nSailthru Mobile [will deliver an in-app message in two occasions:](https://docs.mobile.sailthru.com/docs/setting-up-in-app-messaging#section-in-app-notification-display) after launch with app is in foreground or if the user has the app open and the device has a valid push token. Therefore, you may want to obtain the unread message count when the user opens the app, or when it becomes active after being backgrounded.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"- (void)applicationDidBecomeActive:(UIApplication *)application\\n{\\n    [[STMMessageStream new] unreadCount:^(NSUInteger unreadCount, NSError * _Nullable error) {\\n        // Add your logic here\\n    }];\\n}\",\n      \"language\": \"objectivec\",\n      \"name\": \"iOS (Objective-C)\"\n    },\n    {\n      \"code\": \"func applicationDidBecomeActive(_ application: UIApplication) {\\n    STMMessageStream().unreadCount { (count, error) in\\n        // Add your logic here\\n    }\\n}\\n\",\n      \"language\": \"swift\",\n      \"name\": \"iOS (Swift)\"\n    },\n    {\n      \"code\": \"public class MainActivity extends AppCompatActivity {\\n    @Override\\n    protected void onResume() {\\n        super.onResume();\\n        new MessageStream().getUnreadMessageCount(new MessageStream.MessageStreamHandler<Integer>() {\\n            @Override\\n            public void onSuccess(Integer value) {\\n                // Handle count\\n            }\\n\\n            @Override\\n            public void onFailure(@Nullable Error error) {\\n\\t\\t\\t\\t\\t\\t\\t\\t// Handle error\\n            }\\n        });\\n\\n        // Add your logic here\\n    }\\n}\\n\",\n      \"language\": \"java\",\n      \"name\": \"Android (Java)\"\n    },\n    {\n      \"code\": \"class MainActivity : AppCompatActivity() {\\n    override fun onResume() {\\n        super.onResume()\\n        MessageStream().getUnreadMessageCount(object: MessageStream.MessageStreamHandler<Int?> {\\n            override fun onSuccess(count: Int?) {\\n                // Handle count\\n            }\\n\\n            override fun onFailure(error: Error?) {\\n               // Handle error \\n            }\\n        })\\n\\n        // Add your logic here\\n    }\\n}\",\n      \"language\": \"kotlin\",\n      \"name\": \"Android (Kotlin)\"\n    },\n    {\n      \"code\": \"SailthruMobile.getUnreadCount().then(function(count) {\\n\\t// Handle unread message count\\n}, function(e){\\n\\t// Handle error\\n});\",\n      \"language\": \"javascript\",\n      \"name\": \"React Native (JavaScript)\"\n    }\n  ]\n}\n[/block]","updates":["581781545ec82a0f00a093d5","58a64245fc922d0f00ce8302"],"order":1,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"5e6156bf5e4a51006dcd814c","__v":34,"category":{"sync":{"isSync":false,"url":""},"pages":[],"title":"Message Stream","slug":"message-stream","order":2,"from_sync":false,"reference":false,"_id":"5e6156bf5e4a51006dcd8126","createdAt":"2017-03-03T01:43:58.388Z","version":"5e6156bf5e4a51006dcd818c","__v":0,"project":"55e67aaa9cc7c62b00c4a1ea"},"project":"55e67aaa9cc7c62b00c4a1ea","version":{"version":"1.5","version_clean":"1.5.0","codename":"ST Rebrand","is_stable":true,"is_beta":false,"is_hidden":false,"is_deprecated":false,"categories":["5e6156bf5e4a51006dcd8120","5e6156bf5e4a51006dcd8121","5e6156bf5e4a51006dcd8122","5e6156bf5e4a51006dcd8123","5e6156bf5e4a51006dcd8124","561c61b4ad272c0d00a892df","586c014c0abf1d0f000d04d4","58991d2ad207df0f0002186b","5e6156bf5e4a51006dcd8125","5e6156bf5e4a51006dcd8126","5af0fe494ca2730003cbc98a","5af0fe55ec80af0003804ca2","5e69868cbd5dcb006b35867b","5e6986ca2c6652006791b6e8"],"_id":"5e6156bf5e4a51006dcd818c","project":"55e67aaa9cc7c62b00c4a1ea","__v":2,"forked_from":"5b720760c44b7600034b7a08","createdAt":"2015-09-02T04:27:23.612Z","releaseDate":"2015-09-02T04:27:23.612Z"},"githubsync":"","user":"55d2bd8e2463351700f67dd7","createdAt":"2015-09-16T23:39:52.522Z","parentDoc":null}

Building a Message Stream


## Retrieving messages for a user Sailthru Mobile Messages have several properties which you should familiarize yourself with before building a stream or displaying messages. [block:parameters] { "data": { "h-0": "Property name", "h-1": "Can be blank", "h-2": "Description", "0-0": "`title`", "0-1": "No", "0-2": "The title of the message", "1-0": "`text`", "1-1": "Yes", "1-2": "The body of the message", "2-0": "`htmlText`", "2-1": "Yes", "2-2": "The body of the message rendered into an HTML string", "3-0": "`imageURL`", "3-1": "Yes", "3-2": "A URL pointing to an image", "4-0": "`videoURL` (iOS)\n`mediaURL` (Android)", "4-1": "Yes", "4-2": "A URL pointing to a video", "5-0": "`URL` (iOS)\n`contentURL` (Android)", "5-1": "Yes", "5-2": "A URL pointing to a generic page or resource", "6-0": "`read`", "6-1": "No", "6-2": "Whether or not the message has been read", "7-0": "`createdAt`", "7-1": "No", "7-2": "The date and time when the message was created", "8-0": "`messageID`", "8-1": "No", "8-2": "An alphanumeric unique ID for the message", "9-0": "`attributes`", "9-1": "Yes", "9-2": "A dictionary of string keys and values set on the message", "h-3": "Plugin SDK name", "0-3": "`title`", "1-3": "`text`", "2-3": "`html_text`", "3-3": "`card_image_url`", "4-3": "`card_media_url`", "5-3": "`url`", "6-3": "`is_read`", "7-3": "`created_at`", "8-3": "`id`", "9-3": "`attributes`" }, "cols": 4, "rows": 10 } [/block] You can use the `MessageStream` interface to asynchronously request an array of messages for that device. Once returned you can use these objects to construct your own Message Stream interface. [block:code] { "codes": [ { "code": "//On iOS, using Objective-C\n\n[[STMMessageStream new] messages:^(NSArray *messages, NSError *error) {\n\t//Do something with the array of messages\n}];\n", "language": "objectivec", "name": "iOS (Objective-C)" }, { "code": "STMMessageStream().messages { (theMessages, anError) -> Void in\n\t//Do something with the array of messages\n}", "language": "swift", "name": "iOS (Swift)" }, { "code": "new MessageStream().getMessages(new MessageStream.MessagesHandler() {\n @Override\n public void onSuccess(ArrayList<Message> messages) {\n //Do something with the array of messages\n }\n\n @Override\n public void onFailure(Error error) {\n }\n});", "language": "java", "name": "Android (Java)" }, { "code": "MessageStream().getMessages(object : MessageStream.MessagesHandler {\n override fun onSuccess(messages: ArrayList<Message>?) {\n //Do something with the array of messages\n }\n\n override fun onFailure(error: Error?) {\n }\n})", "language": "kotlin", "name": "Android (Kotlin)" }, { "code": "SailthruMobile.getMessages().then(messages => {\n //Do something with the array of messages\n}).catch(e => {\n //Handle error\n});", "language": "javascript", "name": "React Native (JavaScript)" } ] } [/block] ## Showing full screen content After a user taps on message, you can can show the full screen content with the following method. [block:code] { "codes": [ { "code": "[[STMMessageStream new] presentMessageDetailForMessage:self.latestMessage];", "language": "objectivec", "name": "iOS (Objective-C)" }, { "code": "STMMessageStream().presentDetailForMessage(message)", "language": "swift", "name": "iOS (Swift)" }, { "code": "Intent intent = MessageActivity.intentForMessage(context, bundle, message);\nstartActivity(intent);", "language": "java", "name": "Android (Java)" }, { "code": "val intent = MessageActivity.intentForMessage(context, bundle, message)\nstartActivity(intent)", "language": "kotlin", "name": "Android (Kotlin)" }, { "code": "SailthruMobile.presentMessageDetail(message);", "language": "javascript", "name": "React Native (JavaScript)" } ] } [/block] ## Supporting Impressions In order to support impression analytics from your Message Stream you will need to ensure you register an impression event for relevant messages when your Message Stream is shown to the user. Sailthru Mobile supports tracking **three** types of impressions: * In App Notification Impression * Message Viewed in Stream Impression * Message Viewed in Detail Impression If you didn't customize Sailthru Mobile's default in-app notification or full-screen detail user interface, there is no need to register an impression event as these will be captured automatically. [block:code] { "codes": [ { "code": "[[STMMessageStream new] registerImpressionWithType:\n STMImpressionTypeInAppNotificationView forMessage:message];\n\n[[STMMessageStream new] registerImpressionWithType:\n STMImpressionTypeStreamView forMessage:message];\n\n[[STMMessageStream new] registerImpressionWithType:\n STMImpressionTypeDetailView forMessage:message];", "language": "objectivec", "name": "iOS (Objective-C)" }, { "code": "STMMessageStream().registerImpressionWithType(.inAppNotificationView, forMessage: message)\n \nSTMMessageStream().registerImpressionWithType(.streamView, forMessage: message) \n \nSTMMessageStream().registerImpressionWithType(.detailView, forMessage: message)", "language": "swift", "name": "iOS (Swift)" }, { "code": "new MessageStream().registerMessageImpression(ImpressionType.IMPRESSION_TYPE_IN_APP_VIEW, message);\n\nnew MessageStream().registerMessageImpression(ImpressionType.IMPRESSION_TYPE_STREAM_VIEW, message);\n\nnew MessageStream().registerMessageImpression(ImpressionType.IMPRESSION_TYPE_DETAIL_VIEW, message);", "language": "java", "name": "Android (Java)" }, { "code": "MessageStream().registerMessageImpression(ImpressionType.IMPRESSION_TYPE_IN_APP_VIEW, message)\n\n MessageStream().registerMessageImpression(ImpressionType.IMPRESSION_TYPE_STREAM_VIEW, message)\n\n MessageStream().registerMessageImpression(ImpressionType.IMPRESSION_TYPE_DETAIL_VIEW, message)", "language": "kotlin", "name": "Android (Kotlin)" }, { "code": "SailthruMobile.registerMessageImpression(SailthruMobile.MessageImpressionType.InAppView, message);\n\nSailthruMobile.registerMessageImpression(SailthruMobile.MessageImpressionType.StreamView, message);\n\nSailthruMobile.registerMessageImpression(SailthruMobile.MessageImpressionType.DetailView, message);", "language": "javascript", "name": "React Native (JavaScript)" } ] } [/block] ## Marking As Read You might also want to track which messages have been read or not with your Message Stream, so you could show a number badge or unread indicator. You can mark a collection or single message as read. [block:code] { "codes": [ { "code": "[[STMMessageStream new] markMessageAsRead:message withResponse:NULL];\n// or\n[[STMMessageStream new] markMessagesAsRead:@[message1, message2, message3] withResponse:NULL];", "language": "objectivec", "name": "iOS (Objective-C)" }, { "code": "STMMessageStream().markMessageAsRead(message, withResponse: nil)\n//or \nSTMMessageStream().markMessagesAsRead(messages, withResponse: nil)", "language": "swift", "name": "iOS (Swift)" }, { "code": "new MessageStream().setMessageRead(message, messageReadHandler);\n// or \nnew MessageStream().setMessagesRead(messageList, messageReadHandler);", "language": "java", "name": "Android (Java)" }, { "code": "MessageStream().setMessageRead(message, messageReadHandler)\n// or \nMessageStream().setMessagesRead(messageList, messageReadHandler)", "language": "kotlin", "name": "Android (Kotlin)" }, { "code": "SailthruMobile.markMessageAsRead(message).then(result => {\n // Handle success\n}).catch(e => {\n // Handle error\n});", "language": "javascript", "name": "React Native (JavaScript)" } ] } [/block] ## Deleting Messages Messages can be deleted from a user's stream. [block:code] { "codes": [ { "code": "[[STMMessageStream new] removeMessage:message withResonse:^(NSError *error) {\n\t//Do something with any errors\n}];", "language": "objectivec", "name": "iOS (Objective-C)" }, { "code": "STMMessageStream().removeMessage(message, withResponse: nil)", "language": "swift", "name": "iOS (Swift)" }, { "code": "new MessageStream().deleteMessage(message, messageDeletedHandler);", "language": "java", "name": "Android (Java)" }, { "code": "MessageStream().deleteMessage(message, messageDeletedHandler)", "language": "kotlin", "name": "Android (Kotlin)" }, { "code": "SailthruMobile.removeMessage(message).then(result => {\n // Handle success\n}).catch(e => {\n // Handle error\n});", "language": "javascript", "name": "React Native (JavaScript)" } ] } [/block] ## Custom In-App Message Notifications You can customize the look and feel of In-App Message Notifications by following the instructions [here](doc:customizing-in-app-notifications). [block:callout] { "type": "info", "body": "While testing, you can reinstall the app to get a fresh set of messages, as impressions and deletions do not persist between same-device installs." } [/block] ## Custom Message Attributes You can use a dictionary or hash of custom attributes to further customize the display of your message stream and messages. These may include: * Pinned messages * Categories on the Stream * Custom CTAs or card design ## HTML Support Since you can include [markdown](https://getstarted.sailthru.com/sailthru-mobile/messages/using-markdown/) in messages sent via Sailthru Mobile, we expose this markdown rendered into HTML with the `htmlText` property. [block:code] { "codes": [ { "code": "bodyLabel.attributedText = [[NSAttributedString alloc] initWithData:[message.htmlText dataUsingEncoding:NSUTF8StringEncoding]\n options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,\n NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}\n documentAttributes:nil error:nil];", "language": "objectivec", "name": "iOS (Objective-C)" }, { "code": "do {\n try bodyLabel.attributedText = NSAttributedString.init(\n data: message.htmlText.dataUsingEncoding(NSUnicodeStringEncoding)!,\n options: [\n NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,\n NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding\n ],\n documentAttributes: nil);\n} catch {\n \n}", "language": "swift", "name": "iOS (Swift)" }, { "code": "TextView textView = ...;\ntextView.setText(Html.fromHtml(message.getHtmlText()));", "language": "java", "name": "Android (Java)" }, { "code": "val textView = ...\ntextView.text = Html.fromHtml(message.htmlText, Html.FROM_HTML_MODE_COMPACT)", "language": "kotlin", "name": "Android (Kotlin)" } ] } [/block] HTML Text will always be populated, even if no markdown was used in the original message. ## Unread messages count You can get the number of unread messages from the SDK, without the need to request a message stream and iterate over each individual message. Sailthru Mobile [will deliver an in-app message in two occasions:](https://docs.mobile.sailthru.com/docs/setting-up-in-app-messaging#section-in-app-notification-display) after launch with app is in foreground or if the user has the app open and the device has a valid push token. Therefore, you may want to obtain the unread message count when the user opens the app, or when it becomes active after being backgrounded. [block:code] { "codes": [ { "code": "- (void)applicationDidBecomeActive:(UIApplication *)application\n{\n [[STMMessageStream new] unreadCount:^(NSUInteger unreadCount, NSError * _Nullable error) {\n // Add your logic here\n }];\n}", "language": "objectivec", "name": "iOS (Objective-C)" }, { "code": "func applicationDidBecomeActive(_ application: UIApplication) {\n STMMessageStream().unreadCount { (count, error) in\n // Add your logic here\n }\n}\n", "language": "swift", "name": "iOS (Swift)" }, { "code": "public class MainActivity extends AppCompatActivity {\n @Override\n protected void onResume() {\n super.onResume();\n new MessageStream().getUnreadMessageCount(new MessageStream.MessageStreamHandler<Integer>() {\n @Override\n public void onSuccess(Integer value) {\n // Handle count\n }\n\n @Override\n public void onFailure(@Nullable Error error) {\n\t\t\t\t\t\t\t\t// Handle error\n }\n });\n\n // Add your logic here\n }\n}\n", "language": "java", "name": "Android (Java)" }, { "code": "class MainActivity : AppCompatActivity() {\n override fun onResume() {\n super.onResume()\n MessageStream().getUnreadMessageCount(object: MessageStream.MessageStreamHandler<Int?> {\n override fun onSuccess(count: Int?) {\n // Handle count\n }\n\n override fun onFailure(error: Error?) {\n // Handle error \n }\n })\n\n // Add your logic here\n }\n}", "language": "kotlin", "name": "Android (Kotlin)" }, { "code": "SailthruMobile.getUnreadCount().then(function(count) {\n\t// Handle unread message count\n}, function(e){\n\t// Handle error\n});", "language": "javascript", "name": "React Native (JavaScript)" } ] } [/block]