|
| 1 | +--- |
| 2 | +id: ionic-guide |
| 3 | +title: Mobile app with Ionic |
| 4 | +--- |
| 5 | + |
| 6 | +import Screenshot1 from "../assets/ionic-screenshot1.png"; |
| 7 | + |
| 8 | +This guide shows you how to create a Stream chat mobile app with [Ionic + Capacitor](https://ionicframework.com/docs/). The guide is based on the [Ionic + Angular tutorial](https://ionicframework.com/docs/angular/your-first-app). |
| 9 | + |
| 10 | +You can find the [required tools](https://ionicframework.com/docs/angular/your-first-app#download-required-tools) in the Ionic guide. |
| 11 | + |
| 12 | +## Install ionic tooling |
| 13 | + |
| 14 | +```bash |
| 15 | +npm install -g @ionic/cli native-run cordova-res |
| 16 | +``` |
| 17 | + |
| 18 | +## Create the app |
| 19 | + |
| 20 | +Next, create an Ionic Angular app using the "sidemenu" template and add Capacitor for native functionality: |
| 21 | + |
| 22 | +```bash |
| 23 | +ionic start chat-app sidemenu --type=angular --capacitor |
| 24 | +``` |
| 25 | + |
| 26 | +Next, change into the app folder: |
| 27 | + |
| 28 | +```bash |
| 29 | +cd chat-app |
| 30 | +``` |
| 31 | + |
| 32 | +You can start the app with the following command: |
| 33 | + |
| 34 | +```bash |
| 35 | +npm start |
| 36 | +``` |
| 37 | + |
| 38 | +## Create the chat UI |
| 39 | + |
| 40 | +Install the necessary dependencies: |
| 41 | + |
| 42 | +``` |
| 43 | +npm install stream-chat-angular stream-chat @ngx-translate/core |
| 44 | +``` |
| 45 | + |
| 46 | +Add this to your `tsconfig.json` file to the `compilerOptions` part: |
| 47 | + |
| 48 | +``` |
| 49 | +"allowSyntheticDefaultImports": true |
| 50 | +``` |
| 51 | + |
| 52 | +Import CSS in your `global.scss` file: |
| 53 | + |
| 54 | +``` |
| 55 | +@import "~stream-chat-angular/src/assets/styles/v2/scss/index.scss"; |
| 56 | +``` |
| 57 | + |
| 58 | +Add the module imports required by `stream-chat-angular` to the `app.module.ts` file: |
| 59 | + |
| 60 | +```typescript |
| 61 | +import { NgModule } from "@angular/core"; |
| 62 | +import { BrowserModule } from "@angular/platform-browser"; |
| 63 | +import { RouteReuseStrategy } from "@angular/router"; |
| 64 | + |
| 65 | +import { IonicModule, IonicRouteStrategy } from "@ionic/angular"; |
| 66 | + |
| 67 | +import { AppComponent } from "./app.component"; |
| 68 | +import { AppRoutingModule } from "./app-routing.module"; |
| 69 | +import { TranslateModule } from "@ngx-translate/core"; |
| 70 | +import { |
| 71 | + StreamAutocompleteTextareaModule, |
| 72 | + StreamChatModule, |
| 73 | +} from "stream-chat-angular"; |
| 74 | + |
| 75 | +@NgModule({ |
| 76 | + declarations: [AppComponent], |
| 77 | + imports: [ |
| 78 | + BrowserModule, |
| 79 | + IonicModule.forRoot(), |
| 80 | + AppRoutingModule, |
| 81 | + TranslateModule.forRoot(), |
| 82 | + StreamAutocompleteTextareaModule, |
| 83 | + StreamChatModule, |
| 84 | + ], |
| 85 | + providers: [{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }], |
| 86 | + bootstrap: [AppComponent], |
| 87 | +}) |
| 88 | +export class AppModule {} |
| 89 | +``` |
| 90 | + |
| 91 | +Connect to your Stream chat application by replacing the content of the `app.component.ts` file (add the necessary credentials to your `environment.ts` file): |
| 92 | + |
| 93 | +If you don't already have a Stream account you can [start your free trial](https://getstream.io/try-for-free/). |
| 94 | + |
| 95 | +```typescript |
| 96 | +import { Component, OnInit } from "@angular/core"; |
| 97 | +import { environment } from "src/environments/environment"; |
| 98 | +import { |
| 99 | + ChatClientService, |
| 100 | + ChannelService, |
| 101 | + StreamI18nService, |
| 102 | +} from "stream-chat-angular"; |
| 103 | + |
| 104 | +@Component({ |
| 105 | + selector: "app-root", |
| 106 | + templateUrl: "./app.component.html", |
| 107 | + styleUrls: ["./app.component.scss"], |
| 108 | +}) |
| 109 | +export class AppComponent implements OnInit { |
| 110 | + constructor( |
| 111 | + private chatService: ChatClientService, |
| 112 | + private channelService: ChannelService, |
| 113 | + private streamI18nService: StreamI18nService |
| 114 | + ) { |
| 115 | + const apiKey = environment.apiKey; |
| 116 | + const userId = environment.userId; |
| 117 | + const userToken = environment.userToken; |
| 118 | + this.chatService.init(apiKey, userId, userToken); |
| 119 | + this.streamI18nService.setTranslation(); |
| 120 | + } |
| 121 | + |
| 122 | + async ngOnInit() { |
| 123 | + this.channelService.init({ |
| 124 | + type: "messaging", |
| 125 | + members: { $in: [environment.userId] }, |
| 126 | + }); |
| 127 | + } |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | +Display the channel list in your `app.component.html`: |
| 132 | + |
| 133 | +```html |
| 134 | +<ion-app> |
| 135 | + <ion-split-pane contentId="main-content"> |
| 136 | + <ion-menu contentId="main-content" type="overlay"> |
| 137 | + <ion-content> |
| 138 | + <ion-menu-toggle auto-hide="false"> |
| 139 | + <ion-list> |
| 140 | + <stream-channel-list></stream-channel-list> |
| 141 | + </ion-list> |
| 142 | + </ion-menu-toggle> |
| 143 | + </ion-content> |
| 144 | + </ion-menu> |
| 145 | + <ion-router-outlet id="main-content"></ion-router-outlet> |
| 146 | + </ion-split-pane> |
| 147 | +</ion-app> |
| 148 | +``` |
| 149 | + |
| 150 | +Extend the `folder.module.ts` with the necessary module imports: |
| 151 | + |
| 152 | +```typescript |
| 153 | +import { NgModule } from "@angular/core"; |
| 154 | +import { CommonModule } from "@angular/common"; |
| 155 | +import { FormsModule } from "@angular/forms"; |
| 156 | + |
| 157 | +import { IonicModule } from "@ionic/angular"; |
| 158 | + |
| 159 | +import { FolderPageRoutingModule } from "./folder-routing.module"; |
| 160 | + |
| 161 | +import { FolderPage } from "./folder.page"; |
| 162 | +import { StreamChatModule } from "stream-chat-angular"; |
| 163 | + |
| 164 | +@NgModule({ |
| 165 | + imports: [ |
| 166 | + CommonModule, |
| 167 | + FormsModule, |
| 168 | + IonicModule, |
| 169 | + FolderPageRoutingModule, |
| 170 | + StreamChatModule, |
| 171 | + ], |
| 172 | + declarations: [FolderPage], |
| 173 | +}) |
| 174 | +export class FolderPageModule {} |
| 175 | +``` |
| 176 | + |
| 177 | +Display the active channel in the `folder.page.html`: |
| 178 | + |
| 179 | +```html |
| 180 | +<ion-content [fullscreen]="true"> |
| 181 | + <stream-channel> |
| 182 | + <stream-channel-header> |
| 183 | + <ion-menu-button></ion-menu-button> |
| 184 | + </stream-channel-header> |
| 185 | + <stream-message-list></stream-message-list> |
| 186 | + <stream-notification-list></stream-notification-list> |
| 187 | + <stream-message-input></stream-message-input> |
| 188 | + <stream-thread name="thread"> |
| 189 | + <stream-message-list mode="thread"></stream-message-list> |
| 190 | + <stream-message-input mode="thread"></stream-message-input> |
| 191 | + </stream-thread> |
| 192 | + </stream-channel> |
| 193 | +</ion-content> |
| 194 | +``` |
| 195 | + |
| 196 | +Our chat UI is now ready. |
| 197 | + |
| 198 | +<img src={Screenshot1} width="500" /> |
| 199 | + |
| 200 | +## Deploying to iOS and Android |
| 201 | + |
| 202 | +Create a clean build |
| 203 | + |
| 204 | +```bash |
| 205 | +ionic build |
| 206 | +``` |
| 207 | + |
| 208 | +Next, create both the iOS and Android projects: |
| 209 | + |
| 210 | +```bash |
| 211 | +ionic cap add ios |
| 212 | +ionic cap add android |
| 213 | +``` |
| 214 | + |
| 215 | +For more information see the [Ionic guide](https://ionicframework.com/docs/angular/your-first-app/deploying-mobile) |
| 216 | + |
| 217 | +Open the iOS app: |
| 218 | + |
| 219 | +```bash |
| 220 | +ionic cap open ios |
| 221 | +``` |
| 222 | + |
| 223 | +This will open XCode on your machine, you can start the app by clicking the play button on the top toolbar. |
| 224 | + |
| 225 | +Open the Android app: |
| 226 | + |
| 227 | +```bash |
| 228 | +ionic cap open android |
| 229 | +``` |
| 230 | + |
| 231 | +This will open Android studio on your machine, you can start the app by clicking the play button on the top toolbar. |
| 232 | + |
| 233 | +We've successfully deployed our Stream chat mobile app to iOS and Android. |
| 234 | + |
| 235 | +## Push notifications |
| 236 | + |
| 237 | +We have a separate [push notification guide](./push-guide.mdx) to show you how to add push notifications to your mobile apps. |
0 commit comments