मुझे अपने एंड्रॉइड नोटिफिकेशन (FCM के माध्यम से भेजे गए) में समस्या है। मुझे समझाने दो।
मैं "क्लासिक" सूचनाएं भेज सकता हूं जो अधिसूचना बार में प्रदर्शित होती हैं। मैं व्यक्तिगत डेटा के साथ सूचनाएं भेज सकता हूं लेकिन वे अधिसूचना बार में प्रदर्शित नहीं होते हैं।
हालाँकि, मैं दोनों नहीं कर सकता। वर्तमान में, मैं जो पेलोड भेजता हूं वह इस तरह दिखता है:
{
"to":"token",
"message":{
"notification":{
"body":"Body",
"title":"Title"
}
},
"data":{
"id":2543,
"type":"EVENT"
"title":"Title"
"body":"Body"
}
}
यह डेटा अधिसूचना भेजता है। लेकिन यह फोन पर प्रदर्शित नहीं होता है। मेरी सेवा में मेरा onMessageReceived फ़ंक्शन अन्य बातों के अलावा, यह है:
val id = remoteMessage.data["id"]
val type = remoteMessage.data["type"]
जब तक मेरी सूचना डेटा मोड में जाती है, मुझे ये मान मिल सकते हैं।
इसलिए, मुझे लगता है कि डिस्प्ले नोटिफिकेशन और डेटा नोटिफिकेशन के बीच एक अवधारणा है जिसे मैं समझ नहीं पा रहा हूं।
मैंने इस लिंक का उपयोग किया है: https://firebase.google.com/docs /क्लाउड-मैसेजिंग/अवधारणा-विकल्प#सूचनाएं_और_डेटा_संदेश
और जब मैं अपना data
सरणी message
में डालता हूं तो सूचना नहीं आती है।
यदि आपके पास कोई सुराग है, तो मुझे दिलचस्पी है। मूल विचार यह है कि जब उपयोगकर्ता अधिसूचना (जिसमें किसी घटना का शीर्षक होता है) पर क्लिक करता है, तो एप्लिकेशन उस गतिविधि को खोलता है जो घटना के विवरण के साथ अच्छी तरह से चलती है।
आपकी सहायता के लिए धन्यवाद
[संपादित करें] यह मेरी सेवा में मेरा onMessageReceived(remoteMessage: RemoteMessage)
फ़ंक्शन है जो FirebaseMessagingService
को बढ़ाता है
override fun onMessageReceived(remoteMessage: RemoteMessage) {
super.onMessageReceived(remoteMessage)
val intent:Intent?
val id = remoteMessage.data["id"]
val type = remoteMessage.data["type"]
val title = remoteMessage.data["title"]
val body = remoteMessage.data["body"]
Log.i("YANN", "RemoteMessage Data : " + remoteMessage.data)
Log.i("YANN", "Id : $id")
Log.i("YANN", "Type : $type")
Log.i("YANN", "Title : $title")
Log.i("YANN", "Body : $body")
when (type) {
"NOTIFICATION" -> {
// TODO : Load the NotificationFragment
intent = Intent(this, MainActivity::class.java)
}
"POST" -> {
var post:Post = Post(0, "", "", "", "", "")
// DO SOMETHING TO RETRIVE THE POST
intent = Intent(this, PostActivity::class.java)
intent.putExtra("post", post)
}
"EVENT" -> {
var event = Event(0, "", "", "", "", "", "", "")
// DO SOMETHING TO RETRIEVE THE EVENT
intent = Intent(this, EventActivity::class.java)
intent.putExtra("event", event)
}
else -> {
intent = Intent(this, MainActivity::class.java)
}
}
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)
val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notification = NotificationCompat.Builder(this, resources.getString(R.string.default_notification_channel_id))
.setSmallIcon(R.drawable.ic_dashboard_black_24dp)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent)
NotificationManagerCompat.from(this).notify(1, notification.build())
}
[संपादित करें २] नया नोटिफिकेशनबिल्डर है
val builder: NotificationCompat.Builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val importance = NotificationManager.IMPORTANCE_DEFAULT
val notificationChannel = NotificationChannel(resources.getString(R.string.default_notification_channel_id), resources.getString(R.string.default_notification_channel_id), importance)
val notificationManager:NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(notificationChannel)
NotificationCompat.Builder(this, notificationChannel.id)
} else {
NotificationCompat.Builder(this)
}
builder.setSmallIcon(R.drawable.ic_dashboard_black_24dp)
builder.setContentTitle(title)
builder.setContentText(body)
builder.setAutoCancel(true)
builder.setSound(soundUri)
builder.setContentIntent(pendingIntent)
val notificationID = (0..999999999).random()
NotificationManagerCompat.from(this).notify(notificationID, builder.build())
1 उत्तर
एंड्रॉइड पर पुश नोटिफिकेशन से निपटना वास्तव में थोड़ा दर्द है। अपनी समस्या को ठीक करने का सबसे आसान तरीका है कि आप स्वयं अधिसूचना बनाएं।
आपके कोड में कहीं न कहीं एक सेवा वर्ग होना चाहिए जो FirebaseMessagingService
का विस्तार करता है जिसे आपने अपने AndroidManifest.xml
में घोषित किया है। इसकी एक onMessageReceived(message: RemoteMessage)
विधि है।
आप इस तरह संदेश की सामग्री प्राप्त कर सकते हैं
override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)
val remoteNotification = message.notification ?: return
val title : String = remoteNotification.title
val body : String = remoteNotification.body
फिर आपको अधिसूचना बनाकर इसे स्वयं प्रदर्शित करने की आवश्यकता है, आप यहां अधिसूचना बनाने के बारे में अधिक जानकारी प्राप्त कर सकते हैं:
https://developer.android.com/training/notify-user/build-notification
संपादित करें: एंड्रॉइड के नवीनतम संस्करणों पर, ओरेओ से, आपको चैनल निर्दिष्ट करना होगा, जो आपने किया था। लेकिन आप चैनल बनाना भूल गए। यह भी सुनिश्चित करें कि आप Android के पुराने संस्करणों का समर्थन करते हैं। इस तरह:
var builder: NotificationCompat.Builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val importance = NotificationManager.IMPORTANCE_DEFAULT
val notificationChannel = NotificationChannel("mainChannelId", "Main", importance)
notificationManager.createNotificationChannel(notificationChannel)
NotificationCompat.Builder(applicationContext, notificationChannel.id)
} else {
NotificationCompat.Builder(applicationContext)
}
संबंधित सवाल
जुड़े हुए प्रश्न
नए सवाल
android
एंड्रॉइड Google का मोबाइल ऑपरेटिंग सिस्टम है, जिसका उपयोग प्रोग्रामिंग या डिजिटल डिवाइस (स्मार्टफोन, टैबलेट, ऑटोमोबाइल्स, टीवी, वियर, ग्लास, IoT) को विकसित करने के लिए किया जाता है। एंड्रॉइड से संबंधित विषयों के लिए, एंड्रॉइड-विशिष्ट टैग जैसे कि एंड्रॉइड-इरादे, एंड्रॉइड-गतिविधि, एंड्रॉइड-एडॉप्टर आदि का उपयोग करें। विकास या प्रोग्रामिंग के अलावा अन्य प्रश्नों के लिए, लेकिन एंड्रॉइड फ्रेमवर्क से संबंधित हैं, इस लिंक का उपयोग करें: https: // android.stackexchange.com।
FirebaseMessagingService
तक फैली हुई है। मैं इसे आपको दिखाने के लिए अपनी मूल पोस्ट संपादित करूंगा;)onMessageReceived
फ़ंक्शन कॉल नहीं किया जाता है? इसे केवल तभी कॉल किया जाना चाहिए जब ऐप अग्रभूमि में हो। stackoverflow.com/ प्रश्न/37358462/…notificationManager.createNotification
लाइन को समझना सुनिश्चित नहीं है। आपने इसे घोषित नहीं किया है। मैंने इसे जोड़ा और ऐसा लगता है कि यह काम करता है। मैं अपनी पोस्ट फिर से संपादित करूंगा। धन्यवाद !