1

Getting value in putExtra But cant retrieve in getStringExtra.

public class StartNotificationReceiver extends BroadcastReceiver{




private void sendTodayNotification() {
    DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
    Date todateDate = new Date();
    String TodayDate_String = dateFormat.format(todateDate);
    String Val=AppSettingsPref.getStringValue(context, AppSettingsPref.KEY_TODAY_DATE_AND_TIME, "");
    if(!Val.equals(TodayDate_String))
    {
        AppSettingsPref.saveString(context, AppSettingsPref.KEY_TODAY_DATE_AND_TIME, TodayDate_String);

        final HGDate englishDate = new HGDate(context);
        englishDate.setGregorian(Calendar.YEAR, Calendar.MONTH + 1, 1);
        final HGDate islamicDateToday = new HGDate(englishDate);
        islamicDateToday.toHigri();
        int islamicDayToday = islamicDateToday.getDay();
        int islamicMonthToday = islamicDateToday.getMonth();

        String otherMonth = Dates.islamicMonthName(context, islamicDateToday.getMonth() - 1);
        int adjustedDate = AppSettingsPref.getIntValue(context, AppSettingsPref.CURRENT_ADJUSTMENT_KEY, 0);
        String todayDateTimeinhijri = islamicDateToday.getDay() + (adjustedDate) + " "
                + otherMonth + " " + islamicDateToday.getYear();




        Intent intent = new Intent(context,NotificationReceiver.class);
    -------->intent.putExtra("todayDateTimeinhijri", todayDateTimeinhijri.toString());<-----------




        databaseAccess = DatabaseAccess.getInstance(context);
        databaseAccess.open();
        ArrayList<Event> events = databaseAccess.showEventsByMuslimType(muslimType);
        for (int i = 0; i < events.size(); i++) {
            Event event = events.get(i);
            String[] date = event.getHejriDate().split("-");
            int islamicDayOfEvent = Integer.parseInt(date[0].trim());
            int islamicMonthOfEvent = Integer.parseInt(date[1].trim());
            boolean isViladat = event.isVilaadat;
            if (islamicDayToday == islamicDayOfEvent && islamicMonthToday == islamicMonthOfEvent) {
                makeNotification(event);
            }
        }
    }

intent.putExtra("todayDateTimeinhijri", todayDateTimeinhijri.toString());

This is where .getStringExtra is getting null value

public class NotificationReceiver extends BroadcastReceiver{

 public void onReceive(Context context, Intent intent) {
        this.context = context;
}
        todayDateTime2 = intent.getStringExtra("todayDateTimeinhijri");

}

The extra code is removed for better understanding Thanks in advance

Help will be appreciated. cant find solution.

2 Answers2

0

Where do you send (or start) the intent with the parameter "todayDateTimeinhijri"? If you don't send the intent the NotificationReceiver isn't able to receive anything.

//example activity using receiver and send intent via broadcast ( sendBroadcast(intent) )

public class Example extends AppCompatActivity {
    
    private NotificationReceiver mNotificationReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mNotificationReceiver = new NotificationReceiver();
        IntentFilter intentFilter = new IntentFilter("your_action");
        registerReceiver(mNotificationReceiver, intentFilter);

        StartNotificationReceiver mStartNotificationReceiver = new StartNotificationReceiver(getApplicationContext());
        mStartNotificationReceiver.sendTodayNotification();
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mNotificationReceiver != null) {
            unregisterReceiver(mNotificationReceiver);
        }
    }
    
    private class NotificationReceiver extends BroadcastReceiver {
        Context context;
        @Override
        public void onReceive(Context context, Intent intent) {
            this.context = context;
            String action = intent.getAction();// should be "your_action"
            String todayDateTime2 = intent.getStringExtra("todayDateTimeinhijri");
            Log.i("NotificationReceiver","action: " + action );
            Log.i("NotificationReceiver","dateTime: " + todayDateTime2 );
        }
    }

    public class StartNotificationReceiver {

        private Context context;

        public StartNotificationReceiver(Context ctx){
            this.context = ctx;
        }

        public void sendTodayNotification() {
            Intent intent = new Intent("your_action");
            intent.putExtra("todayDateTimeinhijri", "your String value");
            context.sendBroadcast(intent);
        }
    }
}
TomInCode
  • 506
  • 5
  • 11
  • i have send the intent . i have edited the question. – Mukhtar Ali Dec 02 '20 at 04:52
  • as Ridcully wrote, you need to send the intent like sendBroadcast(intent); or context.sendBroadcast(intent); don't forget to register the NotificationReceiver in the appropriate context. Just take look: https://stackoverflow.com/a/28861229/8391649 – TomInCode Dec 02 '20 at 05:12
  • public class NotificationReceiver extends BroadcastReceiver{ Context context; public void onReceive(Context context, Intent intent) { this.context = context; String todayDateTime2 = intent.getStringExtra("todayDateTimeinhijri"); } } how do i register here ? – Mukhtar Ali Dec 02 '20 at 05:32
  • i have send the intent like context.sendBroadcast(intent); will it work ? or i need to do changes in notificationreciver? – Mukhtar Ali Dec 02 '20 at 05:33
  • yes it will work. I added an example class in my answer above. Just try it. – TomInCode Dec 02 '20 at 06:48
  • really appreciate the way you taught me. Thank you :) – Mukhtar Ali Dec 02 '20 at 07:13
0

Putting the extra in the intent does not send it. There sould be a sendBroadcast() call or similar somewhere.

Ridcully
  • 23,362
  • 7
  • 71
  • 86