Search code examples
androidnotificationsandroid-servicealarmmanagerandroid-pendingintent

Notification is triggered again and again


I am creating a notification on a activity called Mainmenu using alarm here is code

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

On notification received , on click of the notification i have to open the same Mainmenu activity.

Problem: again one more notification is generated , again click on notification again Mainmenu activity will open and continues

Here is Notification code

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;

Solution

  • You should check the application has started from push notification or normal.

    How to do this.

    Notification generate code

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

    Hope this would help you.