为什么我的推送通知不断重复不规则?
问题描述:
使用警报管理器和待处理意图设置的推送通知显示异常行为。出于某种原因,在我打开应用程序后,通知出现一分钟或更少。有时一分钟不止一次,那么它暂时不会出现。我的想法是每天发布一次通知,假设用户在24小时内没有访问我的应用程序。我打算取消任何以前的计时器,并在用户下一次访问我的应用程序时启动一个新的计时器。以下是我的代码。为什么我的推送通知不断重复不规则?
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
活动)熄灭。
从你的描述好像你正在寻找的东西是这样的:
alarm.setRepeating(AlarmManager.RTC_WAKEUP, (System.currentTimeMillis() + AlarmManager.INTERVAL_DAY), AlarmManager.INTERVAL_DAY, pendingIntent);
大加赞赏。 – 4u53r