नोटिफिकेशन में इमोजी दिखाना
आधुनिक एंड्रॉइड (5.0+) में नोटिफिकेशन टाइटल/टेक्स्ट में यूनिकोड इमोजी स्वाभाविक रूप से सपोर्टेड हैं। बस स्ट्रिंग में इमोजी कैरेक्टर डालें।
चरण 1-4: वही बेसिक सेटअप
पिछले आर्टिकल्स जैसा प्रोजेक्ट, लेआउट, मैनिफेस्ट।
MainActivity — इमोजी के साथ टाइटल
package app.tutorialspoint.com.notifyme;
import android.app.NotificationChannel;
import android.app.NotificationManager;
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);
// यूनिकोड इमोजी सीधे स्ट्रिंग में
mBuilder.setContentTitle("Notify Me 😀");
mBuilder.setContentText("Something important! 🎉✨");
mBuilder.setSmallIcon(R.drawable.ic_launcher_foreground);
mBuilder.setAutoCancel(true);
if (android.os.Build.VERSION.SDK_INT >= android.os.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.4 (KitKat) से बेसिक इमोजी सपोर्ट
- एंड्रॉइड 5.0 (Lollipop) से कलर इमोजी (NotoColorEmoji फॉन्ट)
- एंड्रॉइड 7.0 (Nougat) से इमोजी 4.0+ सपोर्ट
- कोई अतिरिक्त लाइब्रेरी या कोड जरूरी नहीं — बस यूनिकोड स्ट्रिंग उपयोग करें
स्ट्रिंग रिसोर्स में इमोजी
res/values/strings.xml में:
<string name="notify_title">नया संदेश 😊</string> <string name="notify_text">आपके पास नया मैसेज है 📨</string>
फिर कोड में: getString(R.string.notify_title)
पुश नोटिफिकेशन (FCM) में इमोजी
सर्वर से JSON में यूनिकोड इमोजी सीधे भेजें:
{
"notification": {
"title": "नया मैसेज 🎉",
"body": "हेलो! 👋 कैसे हो?"
}
}
या डेटा पेलोड में। क्लाइंट साइड पर वही स्ट्रिंग नोटिफिकेशन बिल्डर को पास करें।
पुराने डिवाइस पर फॉलबैक
बहुत पुराने डिवाइस (API < 19) पर इमोजी □ या ? दिख सकते हैं। इसके लिए EmojiCompat लाइब्रेरी उपयोग करें, लेकिन आजकल इसकी जरूरत नहीं पड़ती।