मैंने इस विषय से संबंधित कई अन्य प्रश्नों की खोज की लेकिन संतोषजनक उत्तर नहीं मिला, उनमें से कोई भी मेरे लिए काम नहीं कर रहा है। मैं एक सतत अधिसूचना दिखाना चाहता हूं जिसे केवल ऐप द्वारा समाप्त किया जाना चाहिए। लेकिन मैंने जो कोड लिखा था वह कुछ दिन पहले काम कर रहा था लेकिन अब नहीं।
private void GenNotification(String title, String body)
{
try
{
Log.i(Config.TAGWorker, "Generating Notification . . .");
Intent myIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
this,
0,
myIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setContentText(body)
.setChannelId("myID")
.setTicker("Notification!")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(false)
.setSmallIcon(R.drawable.floppy)
.setOngoing(true)
.build();
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, notification);
}
catch (Exception e)
{
Log.e(Config.TAGWorker, e.getMessage());
}
}
लॉगकैट में ths के संबंध में कोई अपवाद दर्ज नहीं है। कोड को सेवा के क्रिएट में कहा जाता है। सेवा सही ढंग से शुरू हो रही है मैं लॉग कैट में भी देख सकता हूं, कोई अपवाद नहीं है लेकिन अधिसूचना नहीं दिखाई गई है। नोकिया (पीआई) के लिए मेरा ओएस एंड्रॉइड वन है
2 जवाब
आप एक पदावनत NotificationCompat.Builder
कंस्ट्रक्टर का उपयोग कर रहे हैं जो एक ही तर्क (संदर्भ) लेता है; और यह एंड्रॉइड 8.0 (एपीआई स्तर 26) से शुरू होने पर काम नहीं करेगा।
तो, इसे हल करने के लिए:
चरण 1: इसके साथ एक सूचना चैनल बनाएं NotificationManager
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Notification channels are only available in OREO and higher.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel
("PRIMARY_CHANNEL_ID",
"Service",
NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setDescription("Description");
mNotificationManager.createNotificationChannel(notificationChannel);
}
नोट: अपनी इच्छानुसार तर्क मान बदलें
चरण 2:: गैर-बहिष्कृत Notification.Builder
अपने दो-तर्क निर्माता के साथ वर्ग जो चैनल आईडी के रूप में दूसरा तर्क लेता है जिसे आपने पहले चरण में असाइन किया था, जहां मैंने इसे "PRIMARY_CHANNEL_ID" पर सेट किया था
Notification notification = new NotificationCompat.Builder
(this, "PRIMARY_CHANNEL_ID")
.setContentTitle("title")
.setContentText("body")
.setTicker("Notification!")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher_background)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setOngoing(true)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setAutoCancel(true)
.build();
mNotificationManager.notify(0, notification);
क्या आपने अपने स्ट्रिंग्स (शीर्षक और बॉडी) की जांच की है, यदि यह शून्य अधिसूचना नहीं है तो यह शून्य नहीं है यह भी जांचें कि जब आप अपनी सेवा शुरू करते हैं तो आप अपने अधिसूचना चैनल को कॉल करते हैं, यदि आपका एंड्रॉइड 7.0 से ऊपर है तो अधिसूचना साफ़ करें जब आप इसे अपने मामले में एक ही आईडी याद करते हैं 1 है।
संबंधित सवाल
नए सवाल
java
जावा एक उच्च स्तरीय प्रोग्रामिंग भाषा है। इस टैग का उपयोग तब करें जब आपको भाषा का उपयोग करने या समझने में समस्या हो। इस टैग का उपयोग शायद ही कभी किया जाता है और इसका उपयोग अक्सर [वसंत], [वसंत-बूट], [जकार्ता-ई], [Android], [javafx], [हडूप], [श्रेणी] और [मावेन] के साथ किया जाता है।