2012-05-04 36 views
7

Tôi đang sử dụng trình tạo NotificationManager để hiển thị cảnh báo trong ứng dụng của mình. Tôi biết rằng tham số đầu tiên cho phương thức notify là id và khung sẽ cập nhật thông báo nếu nó đã hiển thị, nhưng nếu tôi đặt cảnh báo để phát nhạc chuông hoặc rung, nhạc chuông/rung cũng có phát ra nếu cảnh báo không cập nhật?Cập nhật thông báo rung/nhạc chuông

NotificationCompat.Builder nb = new NotificationCompat.Builder(this); 
    nb.setContentTitle("title"); 
    nb.setContentText("message"); 
    nb.setSmallIcon(getResources().getIdentifier("drawable/alert", null, packageName)); 
    nb.setWhen(System.currentTimeMillis()); 
    nb.setAutoCancel(true); 
    nb.setTicker("message"); 

    final Uri ringtone = Uri.parse(PreferenceManager.getDefaultSharedPreferences(this).getString("ringtone", getString(R.string.settings_default_ringtone))); 

    nb.setDefaults(Notification.DEFAULT_VIBRATE); 
    nb.setSound(ringtone);  
    nb.setDefaults(Notification.DEFAULT_LIGHTS); 

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

    final Intent notificationIntent = new Intent(this, Main.class); 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 

    final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
    nb.setContentIntent(contentIntent); 

    Notification notification = nb.getNotification(); 

    nm.notify(0, notification); 
+0

Đi xem

Trả lời

6

Chỉ cần thử nghiệm bản thân mình và rung/nhạc chuông sẽ tắt, ngay cả khi cập nhật.

CẬP NHẬT: Chỉ cần cập nhật, nếu bạn đang sử dụng NotificationCompat.Builder hoặc Notification.Builder bạn có thể đặt setOnlyAlertOnce để chỉ phát nhạc chuông/rung một lần.

0

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

dài [] v = {500,1000}; notificationBuilder.setVibrate (v);

Uri uri = RingtoneManager.getDefaultUri (RingtoneManager.TYPE_NOTIFICATION); notificationBuilder.setSound (uri);

Các vấn đề liên quan