0

I want to send notification by BroadcastReceiver. Notification sent by AlarmReceiver but again sent notification when i clicked the notification. The same thing happens again, again and again like endless loop

Here is my AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    android.support.v7.app.NotificationCompat.Builder builder = new android.support.v7.app.NotificationCompat.Builder(context);

    builder.setSmallIcon(R.mipmap.ic_launcher);

    // This intent is fired when notification is clicked
    Intent i = new Intent(context, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, i, 0);

    // Notifcation notify sound
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    // Set the intent that will fire when the user taps the notification.
    builder.setContentIntent(pendingIntent);

    // Large icon appears on the left of the notification
    builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher));

    // Content title, which appears in large type at the top of the notification
    builder.setContentTitle("Have a good weekend");

    //Notification click after clear notification
    builder.setAutoCancel(true);

    //Set notification sound
    builder.setSound(alarmSound);

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    // Will display the notification in the notification bar
    notificationManager.notify(1, builder.build());

    }
}

and MainActivity.java

public class MainActivity extends AppCompatActivity {

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

    setAlarm();
}

private void setAlarm(){
    AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    Intent alarmIntent = new Intent(MainActivity.this, MyAlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, alarmIntent, 0);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());

    calendar.set(Calendar.DAY_OF_WEEK,6);
    calendar.set(Calendar.HOUR_OF_DAY,16);
    calendar.set(Calendar.MINUTE,53);
    calendar.set(Calendar.SECOND,0);

    manager.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);

    }
}
emrekose26
  • 683
  • 1
  • 8
  • 21
  • 1
    hi @emrekose26, you have face this problem because you set set Alarm() in On create activity and when you received notification and click on notification then MainActivity is open. so, again call setAlarm() give notification again. – Krunal Patel Jul 29 '16 at 14:51

1 Answers1

0

As on click on notification it open MainActivity and in onCreate method of this activity you are calling setAlarm() . So on click of notification onCreate method is invoked then setAlarm() is invoked , this again set Alarm and build notification.

Do following changes

call setAlarm() onClick of a button, so that it is not invoked automatically onCreate of activity.

If you want to send notification automatically

change the intent of notification

 Intent i = new Intent(context, MainActivity.class);

As of now you are using intent to open ManinActivity on click of notification.

Change Intent to

 Intent i = new Intent(context, OtherActivity.class);

OtherActivity is the activity which does not setAlarm() or build notification in onCreate method.

Alternative Method

Use sharedPreferences to check whether the notification is build once or not. if build once then don't call setAlarm()

Vishwesh Jainkuniya
  • 2,821
  • 3
  • 18
  • 35