अधिसूचना पर कंपन और ध्वनि चूक


93

जब मेरा नोटिफिकेशन आता है तो मैं डिफॉल्ट वाइब्रेट और साउंड अलर्ट पाने की कोशिश कर रहा हूं, लेकिन अभी तक कोई किस्मत नहीं। मुझे लगता है कि यह कुछ ऐसा है जैसे मैं चूक को सेट करता हूं, लेकिन मैं इसे ठीक करने के तरीके के बारे में अनिश्चित हूं। कोई विचार?

public void connectedNotify() {
    Integer mId = 0;
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_notify)
            .setContentTitle("Device Connected")
            .setContentText("Click to monitor");

    Intent resultIntent = new Intent(this, MainActivity.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =     
          PendingIntent.getActivity(getApplicationContext(), 
          0, 
          resultIntent,  
          PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);
    mBuilder.setOngoing(true);
    Notification note = mBuilder.build();
    note.defaults |= Notification.DEFAULT_VIBRATE;
    note.defaults |= Notification.DEFAULT_SOUND;
    NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(mId, note);

}

3
सभी उत्तर केवल आपके पास पहले से मौजूद कोड की कुछ भिन्नता को दोहराते हैं, और उनमें से कोई भी मेरी राय में प्रश्न का उत्तर नहीं देता है। आपका कोड ठीक है, AFAICT। सबसे अधिक संभावना है, आप सिर्फ android.permission.VIBRATEAndroidManifest.xml में गायब हैं ।
ओलाफ डायटशे

जिनके लिए इस थ्रेड में समाधान लागू हो सकते हैं और अभी भी सूचनाओं पर कोई कंपन नहीं है, आपको पहले अपने सूचना चैनल के कंपन को सक्षम करने की आवश्यकता हो सकती है। इस पर एक नज़र डालें: stackoverflow.com/a/47646166/8551764
मोस्टफा अरियन नजद

जवाबों:


203

कुछ डमी कोड आपकी मदद कर सकते हैं।

   private static NotificationCompat.Builder buildNotificationCommon(Context _context, .....) {
            NotificationCompat.Builder builder = new NotificationCompat.Builder(_context)
            .setWhen(System.currentTimeMillis()).......;
     //Vibration
        builder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });

     //LED
        builder.setLights(Color.RED, 3000, 3000);

     //Ton
        builder.setSound(Uri.parse("uri://sadfasdfasdf.mp3"));

    return builder;
   }

AndroidManifest.xmlफ़ाइल में कंपन के लिए नीचे अनुमति जोड़ें

<uses-permission android:name="android.permission.VIBRATE" />

114
+1 vibrateसुविधा के लिए <uses-permission android:name="android.permission.VIBRATE" />अनुमति की आवश्यकता होती है
आशाकरोव

1
कुछ मामलों में आपको Color.hite के बजाय 0xffffffff की तरह हेक्स कलर के argb प्रारूप का उपयोग करना चाहिए, क्योंकि ARGB परम के लिए उपयोगकर्ता डिवाइस उपयोग Color (RGB) का एक छोटा सा मौका है और आपको गलत रंग मिल जाएगा। यह मेरे साथ हुआ।
बेतो कालदास

55
अब कंपन में 1000 एमएस की देरी है। यदि आप पहले एक को 0 पर सेट करते हैं, तो यह तुरंत बंद हो जाएगा। यह {देरी, कंपन, नींद, कंपन, नींद} पैटर्न है।
टॉम

11
मुझे <uses-permission android:name="android.permission.VIBRATE" />काम करने के लिए जोड़ने की आवश्यकता नहीं है ।
इकबाल

1
किसी को पता है कि एपीआई 26 पर सेटसाउंड का उपयोग कैसे करें?
रॉड्रिगो मंगून्हो सेप

61

TeeTracker के उत्तर के लिए एक एक्सटेंशन,

डिफ़ॉल्ट सूचना ध्वनि प्राप्त करने के लिए आप निम्नानुसार कर सकते हैं

NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_notify)
            .setContentTitle("Device Connected")
            .setContentText("Click to monitor");

Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);

यह आपको डिफॉल्ट नोटिफिकेशन साउंड देगा।


35

अधिसूचना कंपन

mBuilder.setVibrate(new long[] { 1000, 1000});

ध्वनि

mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);

अधिक ध्वनि विकल्प के लिए


2
एक कंपन के लिए, <uses-permission android:name="android.permission.VIBRATE" />अपने AndroidManifest.xml में शामिल होना सुनिश्चित करें
हारून एसाव

यह कंपन कब तक चलता है? या यह सिर्फ एक बार कंपन होगा?
Zapnologica

13

इसका काम मेरे लिए ठीक है, आप इसे आजमा सकते हैं।

 protected void displayNotification() {

        Log.i("Start", "notification");

      // Invoking the default notification service //
        NotificationCompat.Builder  mBuilder =
                new NotificationCompat.Builder(this);
        mBuilder.setAutoCancel(true);

        mBuilder.setContentTitle("New Message");
        mBuilder.setContentText("You have "+unMber_unRead_sms +" new message.");
        mBuilder.setTicker("New message from PayMe..");
        mBuilder.setSmallIcon(R.drawable.icon2);

      // Increase notification number every time a new notification arrives //
        mBuilder.setNumber(unMber_unRead_sms);

      // Creates an explicit intent for an Activity in your app //

        Intent resultIntent = new Intent(this, FreesmsLog.class);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(FreesmsLog.class);

      // Adds the Intent that starts the Activity to the top of the stack //
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);

      //  mBuilder.setOngoing(true);
        Notification note = mBuilder.build();
        note.defaults |= Notification.DEFAULT_VIBRATE;
        note.defaults |= Notification.DEFAULT_SOUND;

        mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      // notificationID allows you to update the notification later on. //
        mNotificationManager.notify(notificationID, mBuilder.build());

    }

10

यह सिस्टम से डिफ़ॉल्ट कंपन और ध्वनि का उपयोग करके अधिसूचना को कॉल करने का एक सरल तरीका है।

private void sendNotification(String message, String tick, String title, boolean sound, boolean vibrate, int iconID) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);
    Notification notification = new Notification();

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);

    if (sound) {
        notification.defaults |= Notification.DEFAULT_SOUND;
    }

    if (vibrate) {
        notification.defaults |= Notification.DEFAULT_VIBRATE;
    }

    notificationBuilder.setDefaults(notification.defaults);
    notificationBuilder.setSmallIcon(iconID)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setTicker(tick)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

यदि आप इसका उपयोग करने जा रहे हैं तो वाइब्रेट अनुमति जोड़ें:

<uses-permission android:name="android.permission.VIBRATE"/>

सौभाग्य,'।


क्या कोई मुझे बता सकता है कि जब फोन कॉल होता है तो कंपन क्यों काम नहीं कर रहा है? या हम फोन में भी कंपन को मजबूर करने के लिए अधिसूचना कर सकते हैं

इसने मुझे नोटिफिकेशन में मदद की।
सेवेन

@ user526206, मुझे नहीं पता। मैंने कभी उसका परीक्षण नहीं किया। आप नया प्रश्न खोल सकते हैं क्योंकि इसमें अधिसूचना व्यवहार के साथ कुछ भी नहीं है।
मैहर अबुथ्रा

7

मैं फॉलोइंग कोड का उपयोग कर रहा हूं और मेरे लिए यह ठीक है।

private void sendNotification(String msg) {
    Log.d(TAG, "Preparing to send notification...: " + msg);
    mNotificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, MainActivity.class), 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("GCM Notification")
            .setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    Log.d(TAG, "Notification sent successfully.");
}

1

कोटलिन के लिए आप यह कोशिश कर सकते हैं।

var builder = NotificationCompat.Builder(this,CHANNEL_ID)<br/>
     .setVibrate(longArrayOf(1000, 1000, 1000, 1000, 1000))<br/>
     .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)


1

SDK संस्करण> = 26 का समर्थन करने के लिए, आपको NotificationChanel का निर्माण करना चाहिए और वहां एक कंपन पैटर्न और ध्वनि सेट करना चाहिए। एक कोटलिन कोड नमूना है:

    val vibrationPattern = longArrayOf(500)
    val soundUri = "<your sound uri>"

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val notificationManager =    
                 getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            val attr = AudioAttributes.Builder()
                        .setUsage(AudioAttributes.USAGE_ALARM)
                        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                        .build()
            val channelName: CharSequence = Constants.NOTIFICATION_CHANNEL_NAME
            val importance = NotificationManager.IMPORTANCE_HIGH
            val notificationChannel =
                NotificationChannel(Constants.NOTIFICATION_CHANNEL_ID, channelName, importance)
                notificationChannel.enableLights(true)
                notificationChannel.lightColor = Color.RED
                notificationChannel.enableVibration(true)
                notificationChannel.setSound(soundUri, attr)
                notificationChannel.vibrationPattern = vibrationPattern
                notificationManager.createNotificationChannel(notificationChannel)
    }

और यह बिल्डर है:

 with(NotificationCompat.Builder(applicationContext, Constants.NOTIFICATION_CHANNEL_ID)) {
        setContentTitle("Some title")
        setContentText("Some content")
        setSmallIcon(R.drawable.ic_logo)
        setAutoCancel(true)    
        setVibrate(vibrationPattern)
        setSound(soundUri)
        setDefaults(Notification.DEFAULT_VIBRATE)
        setContentIntent(
            // this is an extension function of context you should build
            // your own pending intent and place it here
            createNotificationPendingIntent(
                Intent(applicationContext, target).apply {
                    flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
                }
            )
        )

        return build()
    }

सुनिश्चित करें कि आपके AudioAttributesद्वारा यहां अधिक पढ़ने के लिए चुना गया है

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.