Search code examples
androidpush-notificationbroadcastreceiveralarmmanager

android- push notification only shows the last id


I have an application for creating routine task with push notification. when I add multiple tasks ( each one has a unique ID), and push notification appears it has the data of the last task. I checked with LogCat and made sure that in TaskActivity each task has a unique ID but in AlarmService only the last task entered gets shown.

I search a lot and also but I couldn't find any clue about what have gone wrong.

I also changed PendingIntent.FLAG_UPDATE_CURRENT to FLAG_CANCEL_CURRENT and FLAG_ONE_SHOT but it did not work.

I thought about adding an intent for each task but I couldn't find a way to do it.

TaskActivity

   .
   .
   .
    public void setNotificationForNewTaskInside() {
    alarmManager = (AlarmManager) getApplicationContext().getSystemService(
            ALARM_SERVICE);
    Intent alarmIntent = new Intent(this,AlarmReceiver.class);

    Random random = new Random();
    randomNum= random.nextInt(9999 - 1000) + 1000;

     pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
             randomNum, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    if (DeviceTimeAndDate.devicetaskTimeDate.equals(newtaskTimeDate)) {

       finalLong=System.currentTimeMillis();

        alarmManager.setExact(AlarmManager.RTC_WAKEUP, finalLong, pendingIntent);

    }
   else   if (DeviceTimeAndDate.devicetaskTimeDate.before(newtaskTimeDate)) {

       finalLong=new_task_timeInMilliseconds;
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, finalLong, pendingIntent);

    } else if (DeviceTimeAndDate.devicetaskTimeDate.after(newtaskTimeDate)) {

        Toast.makeText(context, "زمان یا تاریخ مورد گذشنه است، لطفا زمان دیگری را انتخاب کنید", Toast.LENGTH_SHORT).show();
        startDate.setText(DeviceTimeAndDate.deviceTime);
        taskTime.setText(EditTaskActivity.secondMiConver);
    }

AlarmReciver

 public class AlarmReceiver  extends BroadcastReceiver {
 String TAG = "AlarmReceiver";
  public RemoteViews remoteViews;
public  Uri sound;
public Context context;
public Notification notification;
int finalRandumNum;
 public  static String getTitle,getTime;
public AlarmReceiver (){
}

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

    sound = Uri.parse("android.resource://" + context.getPackageName() + "/raw/plucky");
      remoteViews= new RemoteViews(context.getPackageName(), R.layout.notification);
        remoteViews.setImageViewResource(R.id.notif_image, R.mipmap.ic_launcher);
      remoteViews.setTextViewText(R.id.title,getTitle);
     remoteViews.setTextViewText(R.id.text,getTime);

    NotificationManager notif=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    android.app.Notification notify=new NotificationCompat.Builder(context, "CHANNEL_ID")
            .setContentTitle(TaskActivity.subjEntry)
            .setWhen(finalLong)
            .setSound(sound)
            .setContentText(TaskActivity.timeEntryy)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(TaskActivity.pendingIntent)
            .build();

    notif.notify(TaskActivity.randomNum, notify);

Solution

  • I think you are making a mistake by saving the tasks details in a variable (TaskActivity.subjEntry and TaskActivity.timeEntryy ) which means the data is being replaced every time you try to add a new task.

    So you should save your data in database, array or any other data structure and retrieve the data when creating notification.

    save data to SQlite DB

     public void addData(Data data){
    
        SQLiteDatabase database = getWritableDatabase();
        ContentValues cv_data = new ContentValues();
        cv_data.put("key", data.key());
        cv_data.put("subjEntry", data.subjEntry());
        cv_data.put("subjTime", data.subjTime());
    
       database.insert("tablename",null,cv_data);
       database.close();
    }
    

    Below is a code to retrieve data from SQLite database

       public List<Data> getData(String key){
    
        String columns [] = {
             "key", "subjEntry", "subjTime"
        };
    
        String sortingOrder = " subjTime ASC";
    
        List <Data> dataList = new ArrayList <Data>();
    
        SQLiteDatabase mdDatabase = getWritableDatabase();
    
        Cursor cursor = mdDatabase.query("tablename",
                columns,
                null,
                null,
                null,
                null,
                sortingOrder);
        if (cursor.moveToFirst()){
            do {
            if(cursor.getString(cursor.getColumnIndex("subjEntry")).equals(Key)){
            }
               Data data = new Data();
               data.setsubjEntry(cursor.getString(cursor.getColumnIndex("subjEntry")));
               data.setsubjTime(cursor.getString(cursor.getColumnIndex("subjTime")));
               dataList.add(data);
            }while (cursor.moveToNext());
        }
        mdDatabase.close();
        cursor.close();
    
        return dataList;
    }'
    

    Alternatively try Firebase JobDispatcher