I saw a youtube tutorial for creating notifications in Android 8, the only thing I don't understand is how to make notification sounds and vibrate.
Here is the code from the tutorial, which I added with the channel. second, channel.enable vibration, .setvibrate and setSound.
Helperclass for Channels:
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);
}
}
}
Mainactivity:
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);
}
}
In the manifest, I added
<uses-permission android:name="android.permission.VIBRATE"/>
I can create Notifications on button click but without sound and vibration. Can someone explain to me how this works in Android 8