Giới thiệu
Ví dụ này minh họa cách hiển thị Emoji trong tiêu đề và nội dung thông báo Android. Từ Android 4.4 (API 19), hệ thống đã hỗ trợ Unicode emoji tự nhiên, không cần thư viện thêm.
Các bước thực hiện
Bước 1: Tạo dự án mới
Tạo project Android Studio mới.
Bước 2: Layout (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="16dp"
android:onClick="createNotification"
android:text="Tạo thông báo có Emoji" />
</RelativeLayout>
Bước 3: MainActivity với Emoji (src/MainActivity.java)
package app.tutorialspoint.com.notifyme;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
public static final String NOTIFICATION_CHANNEL_ID = "10001";
private final static String default_notification_channel_id = "default";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void createNotification(View view) {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
MainActivity.this, default_notification_channel_id);
// SỬ DỤNG UNICODE ESCAPE SEQUENCE CHO EMOJI
// 😀 = \uD83D\uDE00
// 🎉 = \uD83C\uDF89
// ❤️ = \u2764\uFE0F
mBuilder.setContentTitle("Thông báo cho tôi 😀 🎉");
mBuilder.setContentText("Có điều gì đó quan trọng! ❤️ 🚀 ⭐");
mBuilder.setSmallIcon(R.drawable.ic_launcher_foreground);
mBuilder.setAutoCancel(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(
NOTIFICATION_CHANNEL_ID, "Kênh có Emoji", importance);
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
assert mNotificationManager != null;
mNotificationManager.createNotificationChannel(notificationChannel);
}
assert mNotificationManager != null;
mNotificationManager.notify((int) System.currentTimeMillis(), mBuilder.build());
}
}
Bước 4: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.tutorialspoint.com.notifyme">
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Các cách nhúng Emoji
1. Unicode Escape (Khuyên dùng cho code Java)
String title = "Th\u00F4ng b\u00E1o \uD83D\uDE00"; // Thông báo 😀 String text = "Ch\u00E0o \uD83D\uDC4B \uD83C\uDF89"; // Chào 👋 🎉
2. Trực tiếp trong code (Java 8+ / Kotlin)
String title = "Thông báo 😀 🎉"; String text = "Chào bạn 👋 ❤️";
3. Từ Resource String (res/values/strings.xml)
<string name="notification_title">Thông báo 😀</string> <string name="notification_text">Nội dung 🎉 ❤️</string>) // Trong code: mBuilder.setContentTitle(getString(R.string.notification_title)); mBuilder.setContentText(getString(R.string.notification_text));
Bảng mã Emoji phổ biến
| Emoji | Tên | Unicode Escape |
|---|---|---|
| 😀 | Grinning Face | \uD83D\uDE00 |
| 😂 | Face with Tears of Joy | \uD83D\uDE02 |
| ❤️ | Red Heart | \u2764\uFE0F |
| 🎉 | Party Popper | \uD83C\uDF89 |
| 👍 | Thumbs Up | \uD83D\uDC4D |
| 🔔 | Bell | \uD83D\uDD14 |
| 🚀 | Rocket | \uD83D\uDE80 |
| ⭐ | Star | \u2B50 |
| ✅ | Check Mark | \u2705 |
Lưu ý quan trọng
- Hiển thị emoji phụ thuộc vào font hệ thống của thiết bị
- Thiết bị cũ (Android < 4.4) có thể hiển thị hộp vuông (tofu) thay vì emoji
- Emoji phức tạp (skin tone, ZWJ sequences) có thể hiển thị khác nhau
- FCM Push Notification: gửi UTF-8 trực tiếp trong JSON payload
Chạy thử nghiệm
Chạy app, nhấn nút tạo thông báo. Kéo thanh thông báo xuống để xem emoji hiển thị trong tiêu đề và nội dung.