android - 在android中不显示通知
问题描述
我正在尝试显示通知。但不显示通知。下面是我的代码,
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button createNotificationButton = findViewById(R.id.button_create_notification);
// Waits for you to click the button
createNotificationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Starts the function below
addNotification();
}
});
}
// Creates and displays a notification
private void addNotification() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel("1" , "Notify", NotificationManager.IMPORTANCE_HIGH);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this, "channel_id")
.setContentTitle("Test Title")
.setContentText("Test Message")
.setSmallIcon(R.mipmap.ic_launcher);
notificationManager.notify(1, notification.build());
}
}
}
我想在顶部栏中显示一个通知,并想在我的应用程序中清除或打开它......任何人都可以帮我解决这个问题。提前致谢。
解决方案
您应该只在版本检查中包含通道创建逻辑。否则,如果设备操作系统版本低于 Android O,它将不会显示通知。
并且您在创建频道时使用频道 ID 作为“1”,在创建通知时使用“频道 ID”。
您应该使用相同channelId
的字符串来创建NotificationChannel
和Notification
。
试试这个代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button createNotificationButton = findViewById(R.id.button_create_notification);
// Waits for you to click the button
createNotificationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Starts the function below
addNotification();
}
});
}
// Creates and displays a notification
private void addNotification() {
String channelId = "myNotificationChannel"; // Store channel ID as String or String resource
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(channelId , "Notify", NotificationManager.IMPORTANCE_HIGH);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notification = new NotificationCompat.Builder(this, channelId) // Use the same channelId String while creating notification
.setContentTitle("Test Title")
.setContentText("Test Message")
.setSmallIcon(R.mipmap.ic_launcher);
notificationManager.notify(1, notification.build());
}
}
希望能帮助到你
推荐阅读
- javascript - 具有水平和垂直滚动的 HTML 表格,第一列固定
- android - 如何在 Xamarin 的 Android 绑定项目中绑定通用 Java 类?
- javascript - ePaper 的图像映射
- javascript - 角 | 图像从多个源上传到单个数组
- javascript - Node.js Webrtc 音频问题
- python - 不允许的方法(GET):/在django中
- html - 屏幕中央的 Onclick 按钮
- php - 如果只有一个数组为空,如果两个数组都为空,则如何调用 if 条件然后调用 else 条件?
- arrays - 如何在本机反应中显示带有动态值的 Picker.Item 标签
- java - Jhipster Swagger 文档在 v2/api-docs 中显示 API,但在 swagger-ui/index.html 中未显示任何内容