java - 在Android Studio中指定发送通知的时间延迟?

标签 java android android-notifications

我在 Android Studio 中设置了通知 channel 来发送通知。

到目前为止,我可以在单击按钮时发送通知。

但是,我想在发送通知时添加延迟。例如,在 20 秒后发送通知。

我知道 AlarmManager 中有一个用于 System.getTimeInMillis 的函数,该函数与此相关,但不确定从这里开始。

这是我的代码:

public class MyNotificationPublisher extends Application {

    public static final String CHANNEL_1_ID = "channel1";
    public static final String CHANNEL_2_ID = "channel2";

    @Override
    public void onCreate() {
        super.onCreate();

        createNotificationChannels();
    }

    private void createNotificationChannels() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel1 = new NotificationChannel(
                    CHANNEL_1_ID,
                    "Channel 1",
                    NotificationManager.IMPORTANCE_HIGH
            );
            channel1.setDescription("This is Channel 1");

            NotificationChannel channel2 = new NotificationChannel(
                    CHANNEL_2_ID,
                    "Channel 2",
                    NotificationManager.IMPORTANCE_LOW
            );
            channel2.setDescription("This is Channel 2");

            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(channel1);
            manager.createNotificationChannel(channel2);
        }
    }
}
public class EmailActivity extends AppCompatActivity {
    private Button btnSend;
    private NotificationManagerCompat notificationManager;
    private long tenSeconds = 10000L;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_email);

        notificationManager = NotificationManagerCompat.from(this);

        btnSend = findViewById(R.id.button_send);
    }

    public void sendOnChannel1(View v) {


        Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentTitle("Hi")
                .setContentText("Test")
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                .build();

        notificationManager.notify(1, notification);
    }

}

最佳答案

您可以只安排要发送的通知:-

使用以下方法:


public void scheduleNotification(Context context, long delay, int notificationId)
 {
//delay is after how much time(in millis) from current time you want to schedule the notification
 NotificationCompat.Builder builder = new NotificationCompat.Builder(context) .setContentTitle(context.getString(R.string.title)) .setContentText(context.getString(R.string.content)) .setAutoCancel(true) .setSmallIcon(R.drawable.app_icon) .setLargeIcon(((BitmapDrawable) context.getResources().getDrawable(R.drawable.app_icon)).getBitmap()) .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); 

Intent intent = new Intent(context, YourActivity.class);

 PendingIntent activity = PendingIntent.getActivity(context, notificationId, intent, PendingIntent.FLAG_CANCEL_CURRENT);

 builder.setContentIntent(activity); Notification notification = builder.build();

 Intent notificationIntent = new Intent(context, MyNotificationPublisher.class);
 notificationIntent.putExtra(MyNotificationPublisher.NOTIFICATION_ID, notificationId);
 notificationIntent.putExtra(MyNotificationPublisher.NOTIFICATION, notification); 

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notificationId, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

 long futureInMillis = SystemClock.elapsedRealtime() + delay; 

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
 alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent); 

} 

然后,接收者类:


public class MyNotificationPublisher extends BroadcastReceiver {

 public static String NOTIFICATION_ID = "notification_id"; 

public static String NOTIFICATION = "notification";

 @Override 
public void onReceive(final Context context, Intent intent)
 {
 NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

 Notification notification = intent.getParcelableExtra(NOTIFICATION);

 int notificationId = intent.getIntExtra(NOTIFICATION_ID, 0);
 notificationManager.notify(notificationId, notification); 
}
 } 

然后,使用适当的参数调用 ScheduleNotification。

关于java - 在Android Studio中指定发送通知的时间延迟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60930925/

相关文章:

java - java for-each循环的类型转换问题

android - 应用程序对内部存储文件的访问权限

android - 为什么在 Android 9 上不显示通知

android - 通过单击按钮创建 android 通知

java - 通过 FCM onMessageReceived 方法从 RemoteMessage 获取值

java - 所有分区的 seekToEnd 并在 Kafka 消费者的自动重新平衡中幸存下来

java - 使用 Java 合并两个链接列表

java - 使用 Jackson 将 JSON 字符串与 Map 合并,无需反序列化字符串

android - 唯一联系 ID

android - 未注释类中的房间异常