该通知在虚拟设备上效果很好,但在真实电话上效果不佳。我是否需要任何用户许可?我不知道要解决这个问题:D
这是我的代码
NotificationManager notificationM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(whitealarm)
.setContentTitle(acti.name)
.setContentText(interval.timeString())
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentIntent(pendingIntent)
.setOnlyAlertOnce(true)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setOnlyAlertOnce(true)
.setAutoCancel(true);
if(!(audioNotiName.equals("none") ||audioNotiName.equals(""))){
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/raw/"+audioNotiName);
builder.setSound(uri);
}
int notificationid=113;
if(notiHaveVibration){
final Vibrator v = (Vibrator) getSystemService(this.VIBRATOR_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
v.vibrate(VibrationEffect.createOneShot(1000, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
v.vibrate(1000);
}
}
notificationM.notify(notificationid,builder.build());
答案 0 :(得分:1)
在真实电话中,您必须检查android版本。 从奥利奥(Oreo)改变了。因此,您必须添加这样的逻辑。
// create Channel for over oreo
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel("channel", "channel_nm", NotificationManager.IMPORTANCE_HIGH);
mChannel.setVibrationPattern(new long[]{500, 500, 500, 500});
mChannel.setLightColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
AudioAttributes att = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
mChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
mChannel.enableVibration(true);
mChannel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), att);
notificationManager.createNotificationChannel(mChannel);
notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), mChannel.getId());
} else {
notificationBuilder = new NotificationCompat.Builder(getApplicationContext());
}
notificationBuilder
.setLargeIcon(icon2)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(pushTitle)
.setContentText(contentText)
.setStyle(bigText)
.setLights(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary), 3000, 3000)
.setVibrate(new long[]{500, 500, 500, 500})
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentIntent(pendingIntent);
int random_num = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
notificationManager.notify(random_num, notificationBuilder.build());