Vibrate and Sound defaults on notification

后端 未结 9 1127
天涯浪人
天涯浪人 2020-12-04 07:37

I\'m trying to get a default vibrate and sound alert when my notification comes in, but so far no luck. I imagine it\'s something to do with the way I set the defaults, but

相关标签:
9条回答
  • 2020-12-04 08:34

    An extension to TeeTracker's answer,

    to get the default notification sound you can do as follows

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

    This will give you the default notification sound.

    0 讨论(0)
  • 2020-12-04 08:34

    For Kotlin you can Try this.

    var builder = NotificationCompat.Builder(this,CHANNEL_ID)
         .setVibrate(longArrayOf(1000, 1000, 1000, 1000, 1000))
         .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
    
    0 讨论(0)
  • 2020-12-04 08:35

    Its work fine to me,You can try it.

     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 讨论(0)
提交回复
热议问题