The notification did well on virtual device but not on real phone. Did I need to have any user permission? I have no idea to solve this problem ๐
here is my code
JavaScript
โx
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());
โ
Advertisement
Answer
In real phone, you have to check android version. From Oreo, it changed. So you have to add logic like this.
JavaScript
// 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());
โ