在我的前台服务中,我收到通知,这工作得很好。唯一的问题是我的手表在收到通知时不会振动或发出任何声音。NotificationChannel
我尝试打开或打开振动,Notification
但都不起作用。设备是华为手表 2。
通知渠道:
private void createNotificationChannels() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
CHANNEL_ID,
"Service Channel",
NotificationManager.IMPORTANCE_LOW
);
serviceChannel.setDescription("Foreground Service");
NotificationChannel messageChannel= new NotificationChannel(
message_CHANNEL_ID,
"messages Channel",
NotificationManager.IMPORTANCE_HIGH
);
messageChannel.setDescription("message Channel");
messageChannel.enableVibration(true);
messageChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
messageChannel.setVibrationPattern(new long[] {2000});
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
manager.createNotificationChannel(messageChannel);
}
服务通知:
private void runNotification(Datamodel data)
{
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
Notification notification = new NotificationCompat.Builder(this, messageChannel)
.setSmallIcon(R.drawable.alarm)
.setContentTitle(data.getline1())
.setContentText(data.getline2())
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(Html.fromHtml(data.toString(), FROM_HTML_MODE_COMPACT)))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setColor(Color.parseColor("#009999"))
//.setSound(defaultSoundUri)
//.setCategory(NotificationCompat.CATEGORY_ALARM)
//.setVibrate(new long[]{2000})
.build();
notificationManager.notify(NOTIFICATION_ID, notification);
NOTIFICATION_ID++;
}
显现:
<uses-feature android:name="android.hardware.type.watch" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.VIBRATE"/>