2013-08-15 9 views
74

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

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। सबसे अधिक संभावना है, आप एंड्रॉइडManifest.xml में 'android.permission.VIBRATE' को याद कर रहे हैं। –

उत्तर

167

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

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; 
    } 
+95

+1 'कंपन' सुविधा के लिए '<उपयोग-अनुमति एंड्रॉइड: नाम = "android.permission.VIBRATE" /> 'अनुमति –

+0

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

+44

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

12

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

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()); 

    } 
+0

यदि काम नहीं करते हैं, तो मुझे सूचित करें: [email protected] आशा है कि यह कंपन और अंगूठी के साथ काम करे। –

57

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); 

यह आप डिफ़ॉल्ट सूचना ध्वनि दे देंगे इस प्रकार कर सकते हैं पाने के लिए।

23

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

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

ध्वनि

mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI); 

for more sound option

+2

कंपन के लिए, अपने AndroidManifest.xml – Arin

5

मैं followung कोड का उपयोग हूँ और उसके मेरे लिए ठीक काम कर रहा।

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."); 
} 
9

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

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"/> 

गुड लक '।

+0

में '<उपयोग-अनुमति एंड्रॉइड: name =" android.permission.VIBRATE "/>' शामिल करना सुनिश्चित करें, कोई मुझे बता सकता है कि फोन कॉल होने पर कंपन क्यों काम नहीं कर रही है ? या क्या हम फोन में भी कंपन को बल देने के लिए अधिसूचना कर सकते हैं कॉल में है –

+0

यह मुझे अधिसूचना Builder.setDefaults (अधिसूचना।चूक); – Saveen

+0

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

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