onMessageReceived (RemoteMessage RemoteMessage) विधि निम्नलिखित मामलों पर आधारित है।
- अधिसूचना और डेटा ब्लॉक के साथ FCM प्रतिक्रिया :
{
"to": "device token list",
"notification": {
"body": "Body of Your Notification",
"title": "Title of Your Notification"
},
"data": {
"body": "Body of Your Notification in Data",
"title": "Title of Your Notification in Title",
"key_1": "Value for key_1",
"image_url": "www.abc.com/xyz.jpeg",
"key_2": "Value for key_2"
}
}
- फोरग्राउंड में ऐप:
onMessageReceived (RemoteMessage RemoteMessage) , जिसे सूचना पट्टी में लार्जआईकॉन और बिगपिक्योर दिखाता है। हम अधिसूचना और डेटा ब्लॉक दोनों से सामग्री पढ़ सकते हैं
- पृष्ठभूमि में ऐप:
onMessageReceived (RemoteMessage RemoteMessage) को नहीं बुलाया जाता है, सिस्टम ट्रे संदेश को प्राप्त करेगी और सूचना ब्लॉक से बॉडी और शीर्षक पढ़ेगी और सूचना पट्टी में डिफ़ॉल्ट संदेश और शीर्षक दिखाएगा ।
- केवल डेटा ब्लॉक के साथ FCM रिस्पांस :
इस मामले में, अधिसूचना ब्लॉक को json से हटा दें
{
"to": "device token list",
"data": {
"body": "Body of Your Notification in Data",
"title": "Title of Your Notification in Title",
"key_1": "Value for key_1",
"image_url": "www.abc.com/xyz.jpeg",
"key_2": "Value for key_2"
}
}
OnMessageReceived () कॉलिंग के लिए समाधान
- फोरग्राउंड में ऐप:
onMessageReceived (RemoteMessage RemoteMessage) , जिसे सूचना पट्टी में लार्जआईकॉन और बिगपिक्योर दिखाता है। हम अधिसूचना और डेटा ब्लॉक दोनों से सामग्री पढ़ सकते हैं
- पृष्ठभूमि में ऐप:
onMessageReceived (RemoteMessage RemoteMessage) कहा जाता है, सिस्टम ट्रे को संदेश प्राप्त नहीं होगा क्योंकि अधिसूचना कुंजी प्रतिक्रिया में नहीं है। नोटिफिकेशन बार में लार्जआईकॉन और बिगपिक्योर दिखाता है
कोड
private void sendNotification(Bitmap bitmap, String title, String
message, PendingIntent resultPendingIntent) {
NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle();
style.bigPicture(bitmap);
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = mContext.getString(R.string.default_notification_channel_id);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "channel_name", NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(notificationChannel);
}
Bitmap iconLarge = BitmapFactory.decodeResource(mContext.getResources(),
R.drawable.mdmlogo);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.mdmlogo)
.setContentTitle(title)
.setAutoCancel(true)
.setSound(defaultSound)
.setContentText(message)
.setContentIntent(resultPendingIntent)
.setStyle(style)
.setLargeIcon(iconLarge)
.setWhen(System.currentTimeMillis())
.setPriority(Notification.PRIORITY_MAX)
.setChannelId(NOTIFICATION_CHANNEL_ID);
notificationManager.notify(1, notificationBuilder.build());
}
संदर्भ लिंक:
https://firebase.google.com/docs/cloud-messaging/android/receive