通知一次又一次地被触发

时间:2016-07-21 07:05:11

标签: android notifications android-service alarmmanager android-pendingintent

我正在使用闹钟创建一个名为Mainmenu的活动的通知,这里是代码

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活动。

问题:再次生成一个通知,再次再次点击通知Mainmenu活动将打开并继续

这是通知代码

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;

1 个答案:

答案 0 :(得分:1)

您应该检查应用程序是否已从推送通知开始或正常。

如何做到这一点。

通知生成代码

Intent intent1 = new Intent(this.getApplicationContext(),MAFLogonActivity.class);
intent1.putExtra("isFromNotification", true);
MAFLogonActivity.java

onCreate()

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);
 }

希望这会对你有所帮助。