Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีเพิ่มรองรับ Emoji ใน Android Push Notification

ภาพรวม

ตัวอย่างนี้สาธิตการแสดง Emoji ในการแจ้งเตือน Android ซึ่งรองรับได้โดยอัตโนมัติใน Android 5.0+ (API 21+) ผ่าน Unicode Standard

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

Android Studio → File → New Project

ขั้นตอนที่ 2: 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="create notification" />
</RelativeLayout>

ขั้นตอนที่ 3: MainActivity - ใช้ Emoji ใน Title/Text

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);

        // ใช้ Unicode Emoji โดยตรง
        mBuilder.setContentTitle("Notify Me 😀🎉📱");
        mBuilder.setContentText("Something important! 🔔✨🚀");
        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, "NOTIFICATION_CHANNEL_NAME", importance);
            mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
            assert mNotificationManager != null;
            mNotificationManager.createNotificationChannel(notificationChannel);
        }

        assert mNotificationManager != null;
        mNotificationManager.notify((int) System.currentTimeMillis(), mBuilder.build());
    }
}

ขั้นตอนที่ 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>

การรองรับ Emoji ใน Android

Android Version และ Emoji Support

Android VersionAPIEmoji Support
Android 13+33+Emoji 15.0 (สีเต็มรูปแบบ)
Android 1231-32Emoji 14.0
Android 1130Emoji 13.0
Android 1029Emoji 12.1
Android 928Emoji 11.0
Android 8.0-8.126-27Emoji 5.0
Android 7.0-7.124-25Emoji 4.0 (ขาว-ดำ)
Android 5.0-6.021-23Emoji พื้นฐาน (ขาว-ดำ)
Android 4.4 และก่อน19 และก่อนไม่รองรับ (แสดง □ หรือ ?)

แนวทางแก้ไขสำหรับ Android เก่า

1. ใช้ EmojiCompat Library (Android 4.4+)

// build.gradle
dependencies {
    implementation "androidx.emoji2:emoji2:1.2.0"
    implementation "androidx.emoji2:emoji2-bundled:1.2.0" // รวมฟอนต์
}

// ใน Application class
public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        EmojiCompat.init(new BundledEmojiCompatConfig(this));
    }
}

2. ใช้ AppCompatTextView (รองรับอัตโนมัติ)

TextView ใน Layout ที่ใช้ AppCompat จะรองรับ EmojiCompat อัตโนมัติ

สำหรับ Push Notification (FCM)

ส่ง Emoji ใน JSON payload โดยตรง:

{
  "to": "/topics/all",
  "notification": {
    "title": "🎉 ข่าวสารใหม่!",
    "body": "มีอัปเดตใหม่ให้คุณ 📱✨"
  },
  "data": {
    "custom_key": "custom_value 😀"
  }
}

ข้อควรระวัง

  • Emoji บางตัวอาจแสดงแตกต่างกันตาม廠商 (Samsung, Google, Xiaomi ใช้ชุด Emoji ต่างกัน)
  • Emoji ที่ซับซ้อน (ZWJ sequences เช่น 👨‍👩‍👧‍👦) อาจไม่แสดงบน Android เก่า
  • ขนาดข้อความเพิ่มขึ้นเล็กน้อยจาก Unicode characters
  • ทดสอบบนอุปกรณ์จริงหลายรุ่น