如何在Oreo 8.0中设置通知的声音和振动?

时间:2018-08-18 06:45:38

标签: android android-notifications android-8.0-oreo android-8.1-oreo

我看到了一个YouTube教程,用于在Android 8中创建通知,我唯一不了解的是如何发出通知声音和振动。

这是本教程中的代码,我在频道中添加了该代码。其次是channel.enable振动,.setvibrate和setSound。

频道的帮助程序类:

public class App extends Application {
public static final String CHANNEL_1_ID = "channel1";
public static final String CHANNEL_2_ID = "channel2";

@Override
public void onCreate() {
    super.onCreate();

    createNotificationChannels();
}

private void createNotificationChannels() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel1 = new NotificationChannel(
                CHANNEL_1_ID,
                "Channel 1",
                NotificationManager.IMPORTANCE_HIGH
        );
        channel1.setDescription("This is Channel 1");
        channel1.setSound(null, null);
        channel1.setLockscreenVisibility(NotificationCompat.PRIORITY_HIGH);
        channel1.enableVibration(true);

        NotificationChannel channel2 = new NotificationChannel(
                CHANNEL_2_ID,
                "Channel 2",
                NotificationManager.IMPORTANCE_LOW
        );
        channel2.setDescription("This is Channel 2");
        channel2.setSound(null, null);
        channel2.setLockscreenVisibility(NotificationCompat.PRIORITY_HIGH);
        channel2.enableVibration(true);

        NotificationManager manager = getSystemService(NotificationManager.class);
        manager.createNotificationChannel(channel1);
        manager.createNotificationChannel(channel2);
    }
}

}

主要活动:

public class MainActivity extends AppCompatActivity {
private NotificationManagerCompat notificationManager;
private EditText editTextTitle;
private EditText editTextMessage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    notificationManager = NotificationManagerCompat.from(this);

    editTextTitle = findViewById(R.id.edit_text_title);
    editTextMessage = findViewById(R.id.edit_text_message);
}

public void sendOnChannel1(View v) {
    String title = editTextTitle.getText().toString();
    String message = editTextMessage.getText().toString();

    Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
            .setSmallIcon(R.drawable.ic_one)
            .setContentTitle(title)
            .setContentText(message)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_MESSAGE)
            .setVibrate(new long[] {2000})
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
            .build();

    notificationManager.notify(1, notification);
}

public void sendOnChannel2(View v) {
    String title = editTextTitle.getText().toString();
    String message = editTextMessage.getText().toString();

    Notification notification = new NotificationCompat.Builder(this, CHANNEL_2_ID)
            .setSmallIcon(R.drawable.ic_two)
            .setContentTitle(title)
            .setContentText(message)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setVibrate(new long[] {2000})
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
            .build();

    notificationManager.notify(2, notification);
 }

}

在清单中,我添加了

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

我可以在单击按钮时创建通知,但没有声音和振动。 有人可以向我解释一下这在Android 8中的工作原理吗

3 个答案:

答案 0 :(得分:0)

您需要将VibrationPattern设置为NotificationChannel

尝试一下

private void createNotificationChannels() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel1 = new NotificationChannel(
                CHANNEL_1_ID,
                "Channel 1",
                NotificationManager.IMPORTANCE_HIGH
        );
        channel1.setDescription("This is Channel 1");
        channel1.setSound(null, null);
        channel1.setLockscreenVisibility(NotificationCompat.PRIORITY_HIGH);
        channel1.setVibrationPattern(new long[] {2000});
        channel1.enableVibration(true);

        NotificationChannel channel2 = new NotificationChannel(
                CHANNEL_2_ID,
                "Channel 2",
                NotificationManager.IMPORTANCE_LOW
        );
        channel2.setDescription("This is Channel 2");
        channel2.setSound(null, null);
        channel2.setLockscreenVisibility(NotificationCompat.PRIORITY_HIGH);
        channel2.setVibrationPattern(new long[] {2000});
        channel2.enableVibration(true);

        NotificationManager manager = getSystemService(NotificationManager.class);
        manager.createNotificationChannel(channel1);
        manager.createNotificationChannel(channel2);
    }
}

答案 1 :(得分:0)

关于缺少振动:您的振动模式只是一个整数。

 .setVibrate(new long[] {2000})

振动器的模式规格为{offtime, ontime, offtime, ontime, offtime, ...}

是-出于某种奇怪的原因,offtime首先出现 ,因此在您的模式中,您指定的全部是 2000毫秒的静默。因此,即使模式正确播放,您也不知道!

将模式设置为至少两个整数。

答案 2 :(得分:0)

您可以使用以下代码:

NotificationChannel NCH1 = new NotificationChannel(CHANNEL_1,"channel1", NotificationManager.IMPORTANCE_LOW);