android 中不显示通知
Not displaying Notification in 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());
}
}
}
我想在顶部栏中显示通知,并想在我的应用程序中清除或打开它...任何人都可以帮助我。提前致谢。
您应该只将频道创建逻辑包装在版本检查中。否则,如果设备 OS 版本低于 Android O.
,它不会显示通知
并且您在创建频道时将频道 ID 用作“1”,并在创建通知时使用 "channel_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());
}
}
希望对您有所帮助
我正在尝试显示通知。但是不显示通知。下面是我的代码,
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());
}
}
}
我想在顶部栏中显示通知,并想在我的应用程序中清除或打开它...任何人都可以帮助我。提前致谢。
您应该只将频道创建逻辑包装在版本检查中。否则,如果设备 OS 版本低于 Android O.
,它不会显示通知并且您在创建频道时将频道 ID 用作“1”,并在创建通知时使用 "channel_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());
}
}
希望对您有所帮助