Push Notifications (FCM)
VieLang Mobile sử dụng Firebase Cloud Messaging (FCM) để gửi thông báo đẩy khi có alarm mới, cảnh báo hệ thống hoặc thông báo tùy chỉnh.
Bước 1: Tạo Firebase Project
- Vào Firebase Console
- Click Add project
- Nhập tên project → Tiếp tục
- Enable/disable Google Analytics → Create project
Bước 2: Bật Firebase Cloud Messaging API
- Vào Project Settings (icon bánh răng)
- Tab Cloud Messaging
- Nếu Cloud Messaging API bị disabled → click Manage API in Google Cloud Console
- Bật API
Bước 3: Lấy Service Account Key
- Project Settings → tab Service accounts
- Click Generate new private key
- Chọn Java format
- Download file
.json
Bước 4: Upload Key lên VieLang IoT
- Đăng nhập với System Admin
- Vào Settings → Notifications → Mobile settings
- Upload file JSON vừa tải
- Click Save
Bước 5: Thêm Firebase vào Flutter App
Cài Firebase CLI
npm install -g firebase-tools
firebase loginCài FlutterFire CLI
dart pub global activate flutterfire_cliConfigure Firebase
cd vielang-mobile
flutterfire configureChọn Firebase project → Chọn platforms (Android + iOS) → Confirm.
File lib/firebase_options.dart sẽ được tạo tự động.
Bước 6: Cấu hình iOS (APNs)
iOS cần APNs Authentication Key để gửi notification:
- Vào Apple Developer Center
- Keys → + → Tên key → Check Apple Push Notifications service (APNs)
- Download file
.p8
Upload lên Firebase:
- Firebase Console → Project Settings → Cloud Messaging tab
- Phần Apple app configuration → Upload APNs Auth Key
- Upload file
.p8+ Team ID + Key ID
Bước 7: Chạy và kiểm tra
flutter run --dart-define-from-file configs.jsonTrong log phải thấy:
Firebase initialized
FCM Token: fXBM5...
Test gửi notification từ VieLang IoT
- Vào Notifications → + Add notification rule
- Trigger: Alarm → khi alarm tạo/cleared
- Delivery method: Mobile (Push)
- Target: All users hoặc user cụ thể
- Lưu
Tạo alarm test → kiểm tra notification trên điện thoại.
Cấu trúc Notification
{
"title": "Cảnh báo: Nhiệt độ cao",
"body": "Sensor-01: 38°C vượt ngưỡng 35°C",
"data": {
"entityType": "ALARM",
"entityId": "alarm-uuid",
"severity": "CRITICAL"
}
}App tự động navigate đến màn hình alarm khi người dùng tap notification.
Troubleshooting
| Lỗi | Giải pháp |
|---|---|
firebase_options.dart không tồn tại | Chạy flutterfire configure |
| Không nhận notification trên iOS | Kiểm tra APNs key + Xcode capabilities: Push Notifications |
| Không nhận notification trên Android | Kiểm tra google-services.json |
MissingPluginException | Chạy lại flutter pub get + rebuild |
Notification Channels (Android)
App tự tạo notification channel:
| Channel | Ý nghĩa |
|---|---|
CRITICAL_ALARM | Âm thanh to, hiển thị ngay |
ALARM | Âm thanh thường |
INFO | Không âm thanh |