Java 在android studio中接收通知
我正在尝试向用户发送通知,提醒他每天上午10点使用我的应用程序做特定的事情 我的问题是,我只有在上午10点打开此应用程序时才会收到通知。从那时起,每次打开应用程序,我都会收到一个通知。因此,如果我在上午10:03打开它,我将收到通知 以下是我的主要活动:Java 在android studio中接收通知,java,android,android-studio,Java,Android,Android Studio,我正在尝试向用户发送通知,提醒他每天上午10点使用我的应用程序做特定的事情 我的问题是,我只有在上午10点打开此应用程序时才会收到通知。从那时起,每次打开应用程序,我都会收到一个通知。因此,如果我在上午10:03打开它,我将收到通知 以下是我的主要活动: public void sendNotification() { Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, 10);// 10 am
public void sendNotification() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 10);// 10 am
cal.set(Calendar.MINUTE, 03);
cal.set(Calendar.SECOND, 00);
cal.set(Calendar.MILLISECOND, 00);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, NotificationReceiver.class);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(this, 0 , intent , 0 ) ;
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY , pendingIntent1);
}
我正在使用onCreate()
方法调用我的方法
AndroidManifest:
<receiver
android:name=".NotificationReceiver"
android:enabled="true"
android:exported="true">
</receiver>
注意:当我第一次安装应用程序时,我也会收到通知 以下是我如何使用Alarm Manager发送每日本地通知。你应该检查一下你是否已经设置了警报。我在这个案例中使用了一个共享的首选项
val pref = PreferenceManager.getDefaultSharedPreferences(context)
pref?.let {
if (!pref.getBoolean(BundleParams.AlarmAlreadyRegistered.name, false)) {
val alarmIntent = Intent(context, TrialNotificationAlarmReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(context, YOUR_CODE, alarmIntent, 0)
val manager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager?
val calendar = Calendar.getInstance().apply {
timeInMillis = System.currentTimeMillis()
set(Calendar.HOUR_OF_DAY, 13)
set(Calendar.MINUTE, 30)
set(Calendar.SECOND, 0)
}
manager?.apply {
setRepeating(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, AlarmManager.INTERVAL_DAY, pendingIntent)
pref.edit().apply {
putBoolean(BundleParams.AlarmAlreadyRegistered.name, true)
}.apply()
}
}
}
此外,我还使用了一个宽频带接收器来监听手机重启时的报警
class DeviceBootReceiver : BroadcastReceiver() {
override fun onReceive(context:Context ?, intent:Intent ?){
intent ?.let {
if (intent.action == "android.intent.action.BOOT_COMPLETED") {
// on device boot complete, reset the alarm
}
}
}
}
希望这能对您有所帮助。以下是我如何使用Alarm Manager发送每日本地通知。你应该检查一下你是否已经设置了警报。我在这个案例中使用了一个共享的首选项
val pref = PreferenceManager.getDefaultSharedPreferences(context)
pref?.let {
if (!pref.getBoolean(BundleParams.AlarmAlreadyRegistered.name, false)) {
val alarmIntent = Intent(context, TrialNotificationAlarmReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(context, YOUR_CODE, alarmIntent, 0)
val manager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager?
val calendar = Calendar.getInstance().apply {
timeInMillis = System.currentTimeMillis()
set(Calendar.HOUR_OF_DAY, 13)
set(Calendar.MINUTE, 30)
set(Calendar.SECOND, 0)
}
manager?.apply {
setRepeating(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, AlarmManager.INTERVAL_DAY, pendingIntent)
pref.edit().apply {
putBoolean(BundleParams.AlarmAlreadyRegistered.name, true)
}.apply()
}
}
}
此外,我还使用了一个宽频带接收器来监听手机重启时的报警
class DeviceBootReceiver : BroadcastReceiver() {
override fun onReceive(context:Context ?, intent:Intent ?){
intent ?.let {
if (intent.action == "android.intent.action.BOOT_COMPLETED") {
// on device boot complete, reset the alarm
}
}
}
}
希望这能对您有所帮助。首先,如果您想在上午10点准时收到应用程序通知,请不要使用
setInexactRepeating
。改用setRepeating
:
public void sendNotification() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 10);// 10 am
cal.set(Calendar.MINUTE, 03);
cal.set(Calendar.SECOND, 00);
cal.set(Calendar.MILLISECOND, 00);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, NotificationReceiver.class);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(this, 0 , intent , 0 );
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY , pendingIntent1);
}
其次,另一个可能的问题是,如果此问题发生在特定的手机上(通常是华为、诺基亚等具有积极电源管理的手机),请禁用该应用程序的电池管理。众所周知,这些手机在杀掉后台应用程序方面臭名昭著,这些应用程序也将使其无法发送通知
看看
这里有针对特定手机的解决方案:
华为:
手机设置>高级设置>电池管理器>受保护的应用程序–检查您的应用程序是否受保护
手机设置>应用程序>你的应用程序>电池>耗电提示[x],并在屏幕关闭[x]后继续运行
手机设置>应用程序>高级(位于底部)>忽略优化>按允许>所有应用程序>在列表中查找你的应用程序并设置为允许
手机设置>电池>应用程序启动,然后将应用程序设置为“手动管理”,并确保所有功能都已打开
手机设置>电池>启动,然后将应用设置为“手动管理”,并确保所有功能都已打开
对于其他品牌的手机,寻找类似的产品
编辑:
我刚刚注意到你的编辑,你得到一个通知,即使是第一次安装。也许可以在您的日历中添加以下内容:
Calendar newDay = Calendar.getInstance();
if (cal.getTimeInMillis() < newDay.getTimeInMillis()) {
cal.add(Calendar.DATE, 1); //add calendar to the next day instead of the time that has passed
}
Calendar newDay=Calendar.getInstance();
if(cal.getTimeInMillis()
首先,如果您想在上午10点准时收到应用程序通知,请不要使用setInexactRepeating
。改用setRepeating
:
public void sendNotification() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 10);// 10 am
cal.set(Calendar.MINUTE, 03);
cal.set(Calendar.SECOND, 00);
cal.set(Calendar.MILLISECOND, 00);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, NotificationReceiver.class);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(this, 0 , intent , 0 );
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY , pendingIntent1);
}
其次,另一个可能的问题是,如果此问题发生在特定的手机上(通常是华为、诺基亚等具有积极电源管理的手机),请禁用该应用程序的电池管理。众所周知,这些手机在杀掉后台应用程序方面臭名昭著,这些应用程序也将使其无法发送通知
看看
这里有针对特定手机的解决方案:
华为:
手机设置>高级设置>电池管理器>受保护的应用程序–检查您的应用程序是否受保护
手机设置>应用程序>你的应用程序>电池>耗电提示[x],并在屏幕关闭[x]后继续运行
手机设置>应用程序>高级(位于底部)>忽略优化>按允许>所有应用程序>在列表中查找你的应用程序并设置为允许
手机设置>电池>应用程序启动,然后将应用程序设置为“手动管理”,并确保所有功能都已打开
手机设置>电池>启动,然后将应用设置为“手动管理”,并确保所有功能都已打开
对于其他品牌的手机,寻找类似的产品
编辑:
我刚刚注意到你的编辑,你得到一个通知,即使是第一次安装。也许可以在您的日历中添加以下内容:
Calendar newDay = Calendar.getInstance();
if (cal.getTimeInMillis() < newDay.getTimeInMillis()) {
cal.add(Calendar.DATE, 1); //add calendar to the next day instead of the time that has passed
}
Calendar newDay=Calendar.getInstance();
if(cal.getTimeInMillis()
是否使用onCreate
方法调用方法?因为我第一次安装应用程序时收到通知!是的,我正在调用此onCreate以确保已设置报警。是否使用onCreate
方法调用您的方法?因为我第一次安装应用程序时收到通知!是的,我正在调用onCreate以确保已设置报警。我没有注意到我正在使用setInexactRepeating:)。谢谢没问题:-)在我看到你的编辑后,我还添加了一个编辑,看看这是否对你有帮助。它有效,谢谢!基本上,这是因为如果您将时间设置为7,而现在是8,则会发送此通知(请查看文档)。为了避免这一点,我们将再增加一天!我只是向那些不知道为什么要加上一天的人解释:)如果我的解决方案对你的其他问题也有帮助,请记下我的答案:-)干杯我没有注意到我在使用setInexactRepeating:)。谢谢没问题:-)在我看到你的编辑后,我还添加了一个编辑,看看这是否对你有帮助。它有效,谢谢!基本上,这是因为如果您将时间设置为7,而现在是8,则会发送此通知(请查看文档)。为了避免这一点,我们将再增加一天!我只是向那些不知道我们为什么要增加一天的人解释:)如果我的解决方案对你的其他问题也有帮助,请记住我的答案:-)干杯