【问题标题】:Show push notification after 10 seconds [duplicate]10秒后显示推送通知[重复]
【发布时间】:2021-11-07 02:00:39
【问题描述】:

我创建了一个推送通知的按钮,但我希​​望此通知在 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);
    }
}

【问题讨论】:

标签: java android push-notification


【解决方案1】:

您可以使用所有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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多