Search code examples
androidsqlalarmmanagerandroid-notificationslocal

Updating multiple local notifications with SQL


Hello currently I have setup a application which allows the user to set an alarm. When this alarms reaches it designated time it will then send a notification to the user. Now currently each time this notification is activated it sends a static message e.g. "You have a notification". I want to be able to change this notifications title to the alarms name which I have stored inside of SQLOPENHELPER when the user made the alarm.

I have also given each alarm a specific ID which is the current time in milliseconds and then also stored this inside of my SQL. Now I know you require a new ID for each notification but how would I do this and relate it back to my SQL?

I have included a small diagram just to clarify what I'm doing and my current alarm and broadcast receiver...

Diagram................

enter image description here

Set alarm.....

// id - relates to the custom id given to that alarm // receiver - is our intent

 pendingIntent = PendingIntent.getBroadcast(v.getContext(), id,  receiver, PendingIntent.FLAG_UPDATE_CURRENT);

 alarmManager.set(AlarmManager.RTC_WAKEUP, myCalendar.getTimeInMillis(), pendingIntent);

// Intent array list is used to store multiple alarms

 intentArrayList.add(pendingIntent);

Broadcast receiver...

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



    Intent moveActivity = new Intent(context, AlarmActivity.class);


    moveActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    // Works with moveActivity to move the user back to main application.
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, moveActivity, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setContentIntent(pendingIntent)
            .setContentTitle("title")
            .setContentText("text")
            .setSmallIcon(R.mipmap.ic_launcher)

            .setAutoCancel(true);


    notificationManager.notify(0, builder.build());

// Notification ID has been left at 0 because I don't know what to do as this point and after trying to figure it out for 2 days I thought I should ask.

//// UPDATE Main Activity

I have implemented a put extra just under my intent Array List

   receiver.putExtra("title", id);

Broadcast

Bundle bundle = intent.getExtras();

    if (bundle != null) {

        pass = bundle.getString("title");

 //I have put my notification in this section


 }

I tested it by putting "pass" which is the String for retrieving the put extra into the title of my notification and it comes up blank?


Solution

    1. Update your receiver intent as below to pass the ALARM_ID:

      Intent receiver = new Intent(getApplicationContext(), Receiver.class);
      receiver.putExtra("KEY_ALARM_ID", ALARM_ID); // ALARM_ID = id
      
    2. In PendingIntent.getBroadcast(), Use ALARM_ID as requestCode.

      PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, ALARM_ID, receiver, PendingIntent.FLAG_UPDATE_CURRENT );
      

    Always use different requestCode for different notification to get correct result.

    1. In notify() method, use ALARM_ID as id:

      notificationManager.notify(ALARM_ID, builder.build());
      

    Notification id should be unique within your application. If a notification with the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.

    1. In your BroadcastReceiver class onReceive() method get the ALARM_ID:

      @Override
      public void onReceive(Context context, Intent intent) 
      {
      
          int alarmId= intent.getExtras().getInt("KEY_ALARM_ID");
      
          // Do something with alarmId
      }
      

    Hope this will solve your problem.