如何为 API 16 级以上的所有设备发送本地通知?
How to send a local notification for all devices above API Level 16?
我可以从 firebase 向设备发送通知。但我需要向同一设备发送通知 locally.I 我正在使用 oreo 移动版。
我尝试使用此代码:
NotificationCompat.Builder b = new NotificationCompat.Builder(this,"150");
b.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("Hearty365")
.setContentTitle("Default notification")
.setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND)
.setContentInfo("Info");
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, b.build());
Android-O Includes Notification Channels
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int notifyId = 1;
String channelId = "some_channel_id";
Notification notification = new Notification.Builder(MainActivity.this)
.setContentTitle("Some Message")
.setContentText("You've received new messages!")
.setSmallIcon(R.drawable.ic_notification)
.setChannel(channelId)
.build();
notificationManager.notify(id, notification);
Notification Channels
通知渠道使我们能够将应用程序发送的通知分组到可管理的组中。一旦我们的通知进入这些渠道,我们就不再对其功能进行输入 —— 所以由用户来管理这些渠道。当涉及到更改我们的应用程序通知的设置时,用户将看到这些选项:
从左边开始,您可以在第一个屏幕中看到我们应用程序的通知设置显示了我们应用程序的通知设置。用户可以从这里:
阻止我们应用程序的所有通知渠道。这意味着来自我们应用程序的通知永远不会显示在用户设备上
如果支持,用户可以声明我们的应用程序通知是否应在 Home 应用程序上显示为徽章
我们的应用程序存在的通知类别。从这里用户可以切换这些以启用或禁用
一旦用户从第一个屏幕选择通知类别,就可以访问下一个屏幕(中间)。用户可以从这里:
阻止来自我们应用程序的来自该频道的所有通知
如果支持,在 Home 应用程序中显示来自此频道的通知
并且如最终截图所示,用户还可以设置来自该频道的通知的重要性。此处选择的选项将说明他们希望在收到通知时如何得到提示。
我们还可以将通知渠道分组到单独的组中。这样我们就可以在多种应用程序模式下拥有相同的通知渠道。
例如,我的应用程序可能支持个人和企业模式,或 Child 和 Parent 模式 — 这允许我们提供跨多个组管理通知设置的选项。
这些显示在与我们的通知渠道相同的位置,除了只是分成相应的组。
在 Android (pre-O) 的旧版本中,这些新功能将被完全忽略,因此我们不必担心当前的实现会中断。
现在我们对什么是通知渠道有了更多的了解,我想是时候看看如何将它们实现到我们的应用程序中了!
创建通知渠道的方法如下:
CharSequence name = getString(R.string.notification_channel_name);
String description = getString(R.string.notification_channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance);
channel.setDescription(description);
NotificationManager notificationManager1 = getSystemService(NotificationManager.class);
notificationManager1.createNotificationChannel(channel);
然后使用通知渠道发送通知(记得保存你的NOTIFICATION_CHANNEL_ID):
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_canceledrantevu)
.setContentIntent(pendingIntent)
.setContentTitle(context.getString(R.string.notification_add_busy_title))
.setContentText(context.getString(R.string.notification_add_busy_msga))
.setVibrate(new long[] {1000, 1000})
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true);
notificationManager.notify(100, builder.build());
请记住,如果您想在 API 级别 >= Version.Oreo
的设备上显示通知,则必须创建通知渠道
我可以从 firebase 向设备发送通知。但我需要向同一设备发送通知 locally.I 我正在使用 oreo 移动版。 我尝试使用此代码:
NotificationCompat.Builder b = new NotificationCompat.Builder(this,"150");
b.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("Hearty365")
.setContentTitle("Default notification")
.setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND)
.setContentInfo("Info");
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, b.build());
Android-O Includes Notification Channels
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int notifyId = 1;
String channelId = "some_channel_id";
Notification notification = new Notification.Builder(MainActivity.this)
.setContentTitle("Some Message")
.setContentText("You've received new messages!")
.setSmallIcon(R.drawable.ic_notification)
.setChannel(channelId)
.build();
notificationManager.notify(id, notification);
Notification Channels
通知渠道使我们能够将应用程序发送的通知分组到可管理的组中。一旦我们的通知进入这些渠道,我们就不再对其功能进行输入 —— 所以由用户来管理这些渠道。当涉及到更改我们的应用程序通知的设置时,用户将看到这些选项:
从左边开始,您可以在第一个屏幕中看到我们应用程序的通知设置显示了我们应用程序的通知设置。用户可以从这里:
阻止我们应用程序的所有通知渠道。这意味着来自我们应用程序的通知永远不会显示在用户设备上 如果支持,用户可以声明我们的应用程序通知是否应在 Home 应用程序上显示为徽章 我们的应用程序存在的通知类别。从这里用户可以切换这些以启用或禁用 一旦用户从第一个屏幕选择通知类别,就可以访问下一个屏幕(中间)。用户可以从这里:
阻止来自我们应用程序的来自该频道的所有通知 如果支持,在 Home 应用程序中显示来自此频道的通知 并且如最终截图所示,用户还可以设置来自该频道的通知的重要性。此处选择的选项将说明他们希望在收到通知时如何得到提示。
我们还可以将通知渠道分组到单独的组中。这样我们就可以在多种应用程序模式下拥有相同的通知渠道。
例如,我的应用程序可能支持个人和企业模式,或 Child 和 Parent 模式 — 这允许我们提供跨多个组管理通知设置的选项。
这些显示在与我们的通知渠道相同的位置,除了只是分成相应的组。
在 Android (pre-O) 的旧版本中,这些新功能将被完全忽略,因此我们不必担心当前的实现会中断。
现在我们对什么是通知渠道有了更多的了解,我想是时候看看如何将它们实现到我们的应用程序中了!
创建通知渠道的方法如下:
CharSequence name = getString(R.string.notification_channel_name);
String description = getString(R.string.notification_channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance);
channel.setDescription(description);
NotificationManager notificationManager1 = getSystemService(NotificationManager.class);
notificationManager1.createNotificationChannel(channel);
然后使用通知渠道发送通知(记得保存你的NOTIFICATION_CHANNEL_ID):
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_canceledrantevu)
.setContentIntent(pendingIntent)
.setContentTitle(context.getString(R.string.notification_add_busy_title))
.setContentText(context.getString(R.string.notification_add_busy_msga))
.setVibrate(new long[] {1000, 1000})
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true);
notificationManager.notify(100, builder.build());
请记住,如果您想在 API 级别 >= Version.Oreo
的设备上显示通知,则必须创建通知渠道