为什么我的推送通知不断重复?

Why does my push notification keep repeating irregularly?

使用警报管理器和未决意图设置的推送通知显示异常行为。出于某种原因,通知会在我打开应用程序后一分钟或更短时间内出现。有时一分钟不止一次,然后一段时间不出现。我的想法是让通知每天发出一次,假设用户在超过 24 小时内没有访问我的应用程序。我打算取消任何以前的计时器,并在用户下次访问我的应用程序时开始一个为期 1 天的新计时器。下面是我的代码。

public class Main_Menu extends AppCompatActivity {


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

    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);


    Button goToGame1;

    goToGame1 = (Button) findViewById(R.id.Game1);
    goToGame1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent beAtGame1 = new Intent(Main_Menu.this, memory_modes.class);
            startActivity(beAtGame1);
        }
    });


Button goToGame2;

    goToGame2 = (Button) findViewById(R.id.Game2);
    goToGame2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent beAtGame2 = new Intent(Main_Menu.this, computation_modes.class);
            startActivity(beAtGame2);
        }
    });


Button goToGame3;

    goToGame3 = (Button) findViewById(R.id.Game3);
    goToGame3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent beAtGame3 = new Intent(Main_Menu.this, evaluation_modes.class);
            startActivity(beAtGame3);
        }
    });


    Button goToGame4;

    goToGame4 = (Button) findViewById(R.id.Game4);
    goToGame4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent beAtGame4 = new Intent(Main_Menu.this, attention_modes.class);
            startActivity(beAtGame4);
        }
    });


    Button goToTrackers;

    goToTrackers = (Button) findViewById(R.id.Trackers);
    goToTrackers.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent beAtTrackers = new Intent(Main_Menu.this,trackerMenu.class);
            startActivity(beAtTrackers);
        }
    });



    Intent intent = new Intent(Main_Menu.this, Receiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(Main_Menu.this, 1, intent, 0);
    AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
    alarm.cancel(pendingIntent);
    alarm.setRepeating(alarm.RTC_WAKEUP, System.currentTimeMillis(), alarm.INTERVAL_DAY, pendingIntent);


    }

public void onBackPressed() {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

}

public class Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    showNotification(context);
}

public void showNotification(Context context) {
    Intent intent = new Intent(context, Main_Menu.class);
    PendingIntent pi = PendingIntent.getActivity(context, 1, intent, 0);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.smalliconfinal)
            .setColor(Color.argb(000,255,177,17))
            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.icon4))
            .setContentTitle("Title")
            .setContentText("Some text.");
    mBuilder.setLights(0xffffb111, 750, 750);
    mBuilder.setContentIntent(pi);
    mBuilder.setDefaults(Notification.DEFAULT_SOUND);
    mBuilder.setAutoCancel(true);
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, mBuilder.build());
}

}

来自我的清单:

<receiver android:name=".Receiver"></receiver>

有人知道为什么会出现这个问题吗?

非常感谢, 谢谢。

AlarmManager.setRepeating 方法的第二个参数是闹钟首次响起的时间。

您正在使用这些参数调用此方法:

alarm.setRepeating(alarm.RTC_WAKEUP, System.currentTimeMillis(), alarm.INTERVAL_DAY, pendingIntent);

这意味着您正在将重复闹钟设置为在您调用该方法后立即响起(当 Main_Menu activity 创建时)。

根据您的描述,您似乎正在寻找这样的东西:

alarm.setRepeating(AlarmManager.RTC_WAKEUP, (System.currentTimeMillis() + AlarmManager.INTERVAL_DAY), AlarmManager.INTERVAL_DAY, pendingIntent);