[英]android studio notification arent working
im new to android and i want the user to get a notification when he reached the maximum value.我是 android 新手,我希望用户在达到最大值时收到通知。 this is how i wrote the code
这就是我写代码的方式
countUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(highestText == 0 && minimumText == 0){
Toast.makeText(getApplicationContext(),"Please enter the target numbers!",Toast.LENGTH_SHORT).show();
}else {
if (highestText == minimumText) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this, "number");
builder.setContentTitle("Maximum number ");
builder.setContentText("please be aware that you reached the maximum number");
builder.setSmallIcon(R.drawable.ic_launcher_background);
builder.setAutoCancel(true);
NotificationManagerCompat managerCompat = NotificationManagerCompat.from(MainActivity.this);
managerCompat.notify(1, builder.build());
}
}
if(!(highestText == 0 && minimumText == 0)){
count++;
counter.setText("" + count);
}
}
});
You are not using the notification channel id I'm not sure but it could be the cause of the problem.我不确定您没有使用通知渠道 ID,但这可能是问题的原因。 I am pasting the snippet of working code from my project, it should work properly but let me know if it will not work
我正在粘贴项目中的工作代码片段,它应该可以正常工作,但如果它不起作用,请告诉我
public void showNotification(String Title, String Body){
// PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, splash_screen.class), 0);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "channel_id_01";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.notification_icon)
//setticker is a text which is pronounce by accessibility service for differently abled people
.setTicker("NEW NOTIFICATION")
// .setAutoCancel(false)
// .setContentIntent(pi)
.setPriority(Notification.PRIORITY_MAX)
.setContentTitle(Title)
.setContentText(Body)
.setContentInfo("Info");
notificationManager.notify(/*notification id*/1, notificationBuilder.build());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.