AlarmManager通知不起作用,无错误的代码错误
这里有类似的问题,但似乎没有一个解决我的问题。该应用程序运行正常,但没有通知。我尝试关闭然后再次打开该应用程序,但仍然不起作用。我希望它在给定的设定时间通知应用程序后,
这是 index.java
public class index extends AppCompatActivity {
public final void NotifyUsers(){
Intent intent = new Intent(this, NotifyClassReceiver.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(index.this,
0, intent, PendingIntent.FLAG_IMMUTABLE);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
long currentTime = System.currentTimeMillis(); // variable to get the current time
long tenSeconds = 1000 * 10; // 1 sec = 1000 millis || 10 seconds
alarmManager.set(AlarmManager.RTC_WAKEUP, currentTime + tenSeconds,
pendingIntent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_index);
createNotificationChannel();
// other codes here
NotifyUsers();
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "newClass";
String description = "You have a class 1 hour from now";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("notifyClass",
name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}
这是 notifyclassreciever.java
public class NotifyClassReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "notifyClass")
.setSmallIcon(R.drawable.icon_notif)
.setContentTitle("Synchronous Class")
.setContentText("You have a class 1 hour from now")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
notificationManagerCompat.notify(200, builder.build());
}
}
这是 androidManifest
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TimeManagementApp">
//other codes here
<receiver android:name=".NotifyClassReceiver"/>
</application>
There are similar questions here, but none seems to solve my issue. The app is running properly but it does not notify. I tried closing then opening the app again but it still doesn't work. I want it to notify at the set time given once the application is up
This is the index.java
public class index extends AppCompatActivity {
public final void NotifyUsers(){
Intent intent = new Intent(this, NotifyClassReceiver.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(index.this,
0, intent, PendingIntent.FLAG_IMMUTABLE);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
long currentTime = System.currentTimeMillis(); // variable to get the current time
long tenSeconds = 1000 * 10; // 1 sec = 1000 millis || 10 seconds
alarmManager.set(AlarmManager.RTC_WAKEUP, currentTime + tenSeconds,
pendingIntent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_index);
createNotificationChannel();
// other codes here
NotifyUsers();
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "newClass";
String description = "You have a class 1 hour from now";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("notifyClass",
name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}
This is the NotifyClassReciever.java
public class NotifyClassReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "notifyClass")
.setSmallIcon(R.drawable.icon_notif)
.setContentTitle("Synchronous Class")
.setContentText("You have a class 1 hour from now")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
notificationManagerCompat.notify(200, builder.build());
}
}
This is the AndroidManifest
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TimeManagementApp">
//other codes here
<receiver android:name=".NotifyClassReceiver"/>
</application>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论