AlarmManager通知不起作用,无错误的代码错误

发布于 2025-02-10 13:02:13 字数 3311 浏览 1 评论 0原文

这里有类似的问题,但似乎没有一个解决我的问题。该应用程序运行正常,但没有通知。我尝试关闭然后再次打开该应用程序,但仍然不起作用。我希望它在给定的设定时间通知应用程序后,

这是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文