10 秒后显示推送通知
Show push notification after 10 seconds
我创建了一个推送通知的按钮,但我希望此通知在 10 秒后出现。怎么做?
这是我的 MainActivity
import android.app.Notification;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private NotificationManagerCompat notificationManager;
private EditText editTextTitle;
private EditText editTextMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notificationManager= NotificationManagerCompat.from(this);
editTextTitle = findViewById(R.id.editTxtTitle);
editTextMessage = findViewById(R.id.editTxtMessage);
}
public void sendOnChannel(View view){
String title = editTextTitle.getText().toString();
String message = editTextMessage.getText().toString();
Notification notification = new NotificationCompat.Builder(this,CHANNEL_1_ID)
.setSmallIcon(R.drawable.ic_one).setContentTitle(title).setContentText(message)
.setCategory(NotificationCompat.CATEGORY_MESSAGE).build();
notificationManager.notify(1, notification);
}
}
您可以使用所有 View
项的 postDelayed
方法实现此目的:
public void sendOnChannel(View view){
String title = editTextTitle.getText().toString();
String message = editTextMessage.getText().toString();
Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
.setSmallIcon(R.drawable.ic_one).setContentTitle(title).setContentText(message)
.setCategory(NotificationCompat.CATEGORY_MESSAGE).build();
view.postDelayed(new Runnable() {
@Override
public void run() {
notificationManager.notify(1, notification);
}
}, 10 * 1000);
}
我创建了一个推送通知的按钮,但我希望此通知在 10 秒后出现。怎么做? 这是我的 MainActivity
import android.app.Notification;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private NotificationManagerCompat notificationManager;
private EditText editTextTitle;
private EditText editTextMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notificationManager= NotificationManagerCompat.from(this);
editTextTitle = findViewById(R.id.editTxtTitle);
editTextMessage = findViewById(R.id.editTxtMessage);
}
public void sendOnChannel(View view){
String title = editTextTitle.getText().toString();
String message = editTextMessage.getText().toString();
Notification notification = new NotificationCompat.Builder(this,CHANNEL_1_ID)
.setSmallIcon(R.drawable.ic_one).setContentTitle(title).setContentText(message)
.setCategory(NotificationCompat.CATEGORY_MESSAGE).build();
notificationManager.notify(1, notification);
}
}
您可以使用所有 View
项的 postDelayed
方法实现此目的:
public void sendOnChannel(View view){
String title = editTextTitle.getText().toString();
String message = editTextMessage.getText().toString();
Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
.setSmallIcon(R.drawable.ic_one).setContentTitle(title).setContentText(message)
.setCategory(NotificationCompat.CATEGORY_MESSAGE).build();
view.postDelayed(new Runnable() {
@Override
public void run() {
notificationManager.notify(1, notification);
}
}, 10 * 1000);
}