0

I have an app work on remind the user with timetable

I put a calendar in new class with notification and go to main activity and called it.

but it the first method only showed the notification but the second not showed it why ????????

MainActivity.java

        package com.osman.calendar;

    import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.pushbots.push.Pushbots;

    public class MainActivity extends AppCompatActivity {


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

            new section(this,1, 18, 30,"Title","new message","new message");
            new section(this,2, 18, 37,"Title","get new","get new");

        }
    }

section.java

package com.osman.calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import java.util.Calendar;

public class section {

    public section(Context context,int id,int Hours,int Minute, String Tilte , String Text, String Alert){
        AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, AlarmReceiver.class);
        intent.putExtra("id",id);
        intent.putExtra("Title",Tilte);
        intent.putExtra("Text",Text);
        intent.putExtra("Alert",Alert);
        PendingIntent alarmIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, intent, 0);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, Hours);
        calendar.set(Calendar.MINUTE, Minute);

        alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                1000 * 60 * 60 * 24 * 7, alarmIntent);


    }
}

AlarmReceiver.java

        package com.osman.calendar;

    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Vibrator;
    import android.support.v4.app.NotificationCompat;
    import android.widget.Toast;


    public class AlarmReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "Time is up!!!!.",Toast.LENGTH_LONG).show();
            String Title = intent.getExtras().getString("Title");
            String Text = intent.getExtras().getString("Text");
            String Alert = intent.getExtras().getString("Alert");
            int id = intent.getExtras().getInt("id");
            createNotification(context, Title, Text, Alert,id);
            // Vibrate the mobile phone
            Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
            vibrator.vibrate(2000);
        }

        public void createNotification (Context context, String msg,String msgText, String msgAlert,int id){

            PendingIntent  notificIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(msg)
                    .setContentText(msgText)
                    .setTicker(msgAlert);
            mBuilder.setContentIntent(notificIntent);
            mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
            mBuilder.setAutoCancel(true);
            NotificationManager mNotificationMAnger =(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationMAnger.notify(id, mBuilder.build());
        }

    }

1 Answers1

0

If you schedule a alarm with the same PendingIntent as before, the alarm you set before is updated and not a new one is set. Only one alarm is firing because your PendingIntent didn't change when you use it the second time, only the extras change (according to this question's answer this isn't considered a change).

To not update the old alarm but set a new one, you have to use a different request-code (the second parameter in getBroadcast() - currently you use 0 for both alarms). Just use the id you already have as parameter in your section-constructor as request-code.

Community
  • 1
  • 1
Micha F.
  • 634
  • 4
  • 17