2017-09-03 15 views
16

पर अधिसूचना ध्वनि मेरे पास एक कस्टम एमपी 3 ध्वनि है जिसे मैं अपनी सूचनाओं पर उपयोग करता हूं। यह एपीआई 26 के नीचे सभी उपकरणों पर ठीक काम करता है। मैंने अधिसूचना चैनल पर भी ध्वनि सेट करने की कोशिश की, लेकिन अभी भी कोई काम नहीं है। यह डिफ़ॉल्ट ध्वनि बजाता है।एपीआई 26

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId) 
      .setAutoCancel(true) 
      .setSmallIcon(R.drawable.icon_push) 
      .setColor(ContextCompat.getColor(this, R.color.green)) 
      .setContentTitle(title) 
      .setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification)) 
      .setDefaults(Notification.DEFAULT_VIBRATE) 
      .setStyle(new NotificationCompat.BigTextStyle().bigText(message)) 
      .setContentText(message); 
     Notification notification = builder.build(); 
     NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
     if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { 
      NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT); 
      AudioAttributes audioAttributes = new AudioAttributes.Builder() 
        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) 
        .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE) 
        .build(); 
      channel.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification), audioAttributes); 
      notificationManager.createNotificationChannel(channel); 
     } 
     notificationManager.notify(1, notification); 
+0

क्या आपने डिफ़ॉल्ट अधिसूचना ध्वनि म्यूट किया था? मुझे भी एक ही समस्या का सामना करना पड़ रहा है। मैं NotificationManager.IMPORTANCE_MAX के साथ अधिसूचना पर कोई भी स्वर नहीं खेलना चाहता हूं। क्या आप कृपया इसमें मेरी मदद कर सकते हैं। –

उत्तर

7

मैंने रिंगटोन मैनेजर का उपयोग किया, और यह मेरे लिए काम है। thius कोड का प्रयास करें:

NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this); 
    builder.setSmallIcon(android.R.drawable.ic_dialog_alert); 
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com/")); 
    PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0); 
    builder.setContentIntent(pendingIntent); 
    builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)); 
    builder.setContentTitle("Notification title"); 
    builder.setContentText("Notification message."); 
    builder.setSubText("Url link."); 

    try { 
     Uri notification = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.custom_ringtone); 
     Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification); 
     r.play(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    NotificationManager notificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE); 
    notificationManager.notify(1, builder.build()); 
+0

धन्यवाद। यह काम कर रहा है। –

+0

मेरे अनुभव में जो ध्वनि को बार-बार चला रहा है। – HoseinIT

+0

इस समाधान के साथ समस्या यह है कि उपयोगकर्ता अधिसूचना ध्वनि को म्यूट नहीं कर सकते हैं। यह हमेशा ध्वनि बजाता है। –

12

आपने मूल रूप से डिफ़ॉल्ट ध्वनि के साथ चैनल बनाया होगा। एक बार चैनल बनने के बाद इसे बदला नहीं जा सकता है। आपको या तो ऐप को पुनर्स्थापित करना होगा या नए चैनल आईडी के साथ चैनल बनाना होगा।

+0

चैनल का नाम बदलने का प्रयास किया लेकिन अभी भी काम नहीं कर रहा है। –

+0

छोटी दुनिया :-) धन्यवाद Paweł, तुमने मुझे बहुत समय बचाया! –

+0

महत्व, ध्वनि, रोशनी, कंपन, लॉक स्क्रीन, या डीएनडी सेटिंग को बदलने के लिए ऐप को अनइंस्टॉल करें और चैनल को साफ़ करने के लिए फिर से इंस्टॉल करें। Https://developer.android.com/guide/topics/ui/notifiers/notifications.html#ManageChannels, "अधिसूचना चैनल को हटाने" शीर्षक वाले एएसपी अनुभाग देखें – BitByteDog

1

डिफ़ॉल्ट ध्वनि किसी भी ध्वनि ओवरराइड करता है। आप उसके लिए एक चैनल बनाने की आवश्यकता

Android notifications

+0

अभी भी काम नहीं करते हैं। –

+4

आपको ऐप को पुनर्स्थापित करके या उस कोड को जोड़ने से पहले अपने ऐप से जुड़े डेटा को साफ़ करके परीक्षण चैनलों को साफ़ करना होगा !!!! –

+0

ठीक है। मैं आज रात फिर कोशिश करूंगा। धन्यवाद। –

संबंधित मुद्दे