通知被一次又一次地触发
Notification is triggered again and again
我正在使用警报在名为 Mainmenu 的 activity 上创建一个通知,这里是代码
Intent myIntent = new Intent(MainMenu.this, SyncService.class);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(MainMenu.this, 1, myIntent,0);
Calendar calforAlram = Calendar.getInstance();
calforAlram.set(Calendar.HOUR_OF_DAY, 20);
calforAlram.set(Calendar.MINUTE, 46);
calforAlram.set(Calendar.SECOND, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calforAlram.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent);
alarmManager.cancel(pendingIntent);
收到通知后,单击通知我必须打开相同的 Mainmenu
activity。
问题:再次生成一个通知,再次单击通知主菜单 activity 将打开并继续
这是通知代码
NotificationManager mManager = (NotificationManager) getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),MAFLogonActivity.class);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle("XXXXXXXX")
.setContentText("Please sync data.").setAutoCancel(true);
intent1.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this.getApplicationContext(), 1, intent1, 0);
notification.setContentIntent(pendingNotificationIntent);
Log.d("On service", "Alarms set for everyday 2 pm.");
mManager.notify(0, notification.build());
return Service.START_NOT_STICKY;
您应该检查应用程序是否已从推送通知启动或正常。
怎么做。
通知生成码
Intent intent1 = new Intent(this.getApplicationContext(),MAFLogonActivity.class);
intent1.putExtra("isFromNotification", true);
onCreate() of MAFLogonActivity.java
boolean isFromNotification = getIntent().getBooleanExtra("isFromNotification", false);
if(!isFromNotification){
Intent myIntent = new Intent(MainMenu.this, SyncService.class);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(MainMenu.this, 1, myIntent, 0);
Calendar calforAlram = Calendar.getInstance();
calforAlram.set(Calendar.HOUR_OF_DAY, 20);
calforAlram.set(Calendar.MINUTE, 46);
calforAlram.set(Calendar.SECOND, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calforAlram.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent);
alarmManager.cancel(pendingIntent);
}
希望对您有所帮助。
我正在使用警报在名为 Mainmenu 的 activity 上创建一个通知,这里是代码
Intent myIntent = new Intent(MainMenu.this, SyncService.class);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(MainMenu.this, 1, myIntent,0);
Calendar calforAlram = Calendar.getInstance();
calforAlram.set(Calendar.HOUR_OF_DAY, 20);
calforAlram.set(Calendar.MINUTE, 46);
calforAlram.set(Calendar.SECOND, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calforAlram.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent);
alarmManager.cancel(pendingIntent);
收到通知后,单击通知我必须打开相同的 Mainmenu
activity。
问题:再次生成一个通知,再次单击通知主菜单 activity 将打开并继续
这是通知代码
NotificationManager mManager = (NotificationManager) getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),MAFLogonActivity.class);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle("XXXXXXXX")
.setContentText("Please sync data.").setAutoCancel(true);
intent1.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this.getApplicationContext(), 1, intent1, 0);
notification.setContentIntent(pendingNotificationIntent);
Log.d("On service", "Alarms set for everyday 2 pm.");
mManager.notify(0, notification.build());
return Service.START_NOT_STICKY;
您应该检查应用程序是否已从推送通知启动或正常。
怎么做。
通知生成码
Intent intent1 = new Intent(this.getApplicationContext(),MAFLogonActivity.class);
intent1.putExtra("isFromNotification", true);
onCreate() of MAFLogonActivity.java
boolean isFromNotification = getIntent().getBooleanExtra("isFromNotification", false);
if(!isFromNotification){
Intent myIntent = new Intent(MainMenu.this, SyncService.class);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(MainMenu.this, 1, myIntent, 0);
Calendar calforAlram = Calendar.getInstance();
calforAlram.set(Calendar.HOUR_OF_DAY, 20);
calforAlram.set(Calendar.MINUTE, 46);
calforAlram.set(Calendar.SECOND, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calforAlram.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent);
alarmManager.cancel(pendingIntent);
}
希望对您有所帮助。