gpt4 book ai didi

Android:在特定时间触发 Notification.Builder

转载 作者:太空宇宙 更新时间:2023-11-03 12:52:27 25 4
gpt4 key购买 nike

我有一个 Notification.Builder,它会在我单击按钮时立即向通知栏发送一条消息。有没有办法让通知出现在选定的时间?我查看了 Android 文档,但没有看到任何看起来可行的东西。

这是我的代码 fragment :

        Notification n = new Notification.Builder(this)
.setContentTitle("Random title")
.setContentText("Random text")
.setSmallIcon(R.drawable.abc_ic_go_search_api_mtrl_alpha)
.setContentIntent(pIntent).build();

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

notificationManager.notify(0, n);

谢谢!

最佳答案

1) 使用您的通知代码创建一个广播接收器。

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

Notification notification = new NotificationCompat.Builder(context)
.setContentTitle("Random title")
.setContentText("Random text")
.setSmallIcon(R.drawable.abc_ic_go_search_api_mtrl_alpha)
.setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, MyActivity.class), 0))
.build();

notificationManager.notify(0, notification);
}
}

2) 使用 AlarmManager 为您的广播接收器广播 Intent 安排警报。

AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
// set for 30 seconds later
alarmMgr.set(AlarmManager.RTC, Calendar.getInstance().getTimeInMillis() + 30000, alarmIntent);

关于Android:在特定时间触发 Notification.Builder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27992711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com