J. *_*Doe 3 android android-notifications
我正在尝试编写一个简单的通知应用程序。它所做的只是在单击按钮时发出通知。
我根据本教程逐步编写我的应用程序。
我不明白应该将什么通道 ID 传递给构造函数才能使应用程序正常工作。现在应用程序不执行任何操作(它也不会崩溃)。我猜问题出在变量的初始化上NotificationCompat.Builder notifyBuilder。我不知道要传递什么,所以我只是传递了一个任意字符串"0"。
我的问题是我应该将什么传递给构造函数NotificationCompat.Builder,它会让应用程序正常工作吗?
这是主要活动:
public class MainActivity extends AppCompatActivity {
private Button mNotifyButton;
private static final int NOTIFICATION_ID = 0;
private static final String NOTIFICATION_ID_STRING = "0";
private NotificationManager mNotifyManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNotifyButton = (Button) findViewById(R.id.notificationButton);
}
public void sendNotification(View view) {
mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder notifyBuilder
=new NotificationCompat.Builder(this, NOTIFICATION_ID_STRING)
.setContentTitle("You've been notified!")
.setContentText("This is your notification text.")
.setSmallIcon(R.drawable.ic_android);
Notification myNotification = notifyBuilder.build();
mNotifyManager.notify(NOTIFICATION_ID, myNotification);
}
}
Run Code Online (Sandbox Code Playgroud)
private static final int NOTIFICATION_ID = 0;
Run Code Online (Sandbox Code Playgroud)
这会识别您的通知。如果您使用同一 ID 发送多个通知,则旧通知将被新通知替换。因此,如果想同时显示多个通知,则需要多个ID。
private static final String NOTIFICATION_ID_STRING = "0";
Run Code Online (Sandbox Code Playgroud)
这是频道的 ID。它告诉我们应该使用哪个通道来发送该通知。您可以分配任何String. String您只需要确保对特定渠道使用相同的方法即可。如果您想创建更多频道,则需要String为它们使用不同的频道。
关于频道
在最新版本的 Android(Android Oreo 及以上版本)中,您可以使用不同的渠道来发送通知。这样,您就可以让用户自定义他想要接收的通知。
例如,您有一个购物应用程序。然后,您可以拥有一些渠道,例如:
在该频道上发送通知之前,您应该创建它:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Orders Notification";
String description = "Notifications about my order";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(NOTIFICATION_ID_STRING, name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以通过以下方式发送通知:
NotificationCompat.Builder notifyBuilder
= new NotificationCompat.Builder(this, name.toString())
Run Code Online (Sandbox Code Playgroud)
您可以通过以下方式检查应用程序拥有的所有频道:
Settings -> Apps -> App you want to check -> Notifications
Run Code Online (Sandbox Code Playgroud)
有些应用程序只有一个频道。其他应用程序可能有多个渠道。
回到你的问题
所以,回到你的例子,NOTIFICATION_ID_STRING可以是任何字符串。"0"您只需每次想要在该特定频道上发送消息时使用即可。"channel1"并且,例如,如果您创建新通道,请使用新字符串。
但是,您必须在发送通知之前创建一个通道:
// This ID can be the value you want.
private static final int NOTIFICATION_ID = 0;
// This ID can be the value you want.
private static final String NOTIFICATION_ID_STRING = "My Notifications";
public void sendNotification(View view) {
mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//Create the channel. Android will automatically check if the channel already exists
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(NOTIFICATION_ID_STRING, "My Channel Name", NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("My notification channel description");
mNotifyManager.createNotificationChannel(channel);
}
NotificationCompat.Builder notifyBuilder
= new NotificationCompat.Builder(this, NOTIFICATION_ID_STRING)
.setContentTitle("You've been notified!")
.setContentText("This is your notification text.")
.setSmallIcon(R.drawable.ic_android);
Notification myNotification = notifyBuilder.build();
mNotifyManager.notify(NOTIFICATION_ID, myNotification);
}
Run Code Online (Sandbox Code Playgroud)
您可以在此处找到有关它们的更多信息