Reputation: 3219
I'm trying to develop birthday reminder application. I'm storing some data as name, last name, date of birthday.. I want to pass name variable to BroadcastReciever
when user set the date of birthday, so when the alarm notify, I could display name of particular birthday. How could I do that? Here is small part of my code from activity where i'm sending data to BroadcastReciever
:
if (dateSelected != null) {
Intent intent = new Intent(AddBirthday.this, AlarmReceiver.class);
intent.putExtra("extra", getFirstName);
sendBroadcast(intent);
PendingIntent pendingIntent = PendingIntent.getBroadcast(AddBirthday.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Set the alarm for a particular time.
//alarmManager.set(AlarmManager.RTC_WAKEUP, test, PendingIntent.getBroadcast(AddBirthday.this, 1 , intent, PendingIntent.FLAG_UPDATE_CURRENT));
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, test, AlarmManager.INTERVAL_DAY, pendingIntent);
}
And here is BroadcastReciever
:
public class AlarmReceiver extends BroadcastReceiver{
private int MID;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotifyBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(
context).setSmallIcon(R.drawable.ic_launcher_icon)
.setContentTitle("Birthday Upcoming")
.setContentText("Its " + "birthday" + "Dont Forget To Buy Some Present!").setSound(alarmSound)
.setAutoCancel(true).setWhen(when)
.setContentIntent(pendingIntent)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
notificationManager.notify(MID, mNotifyBuilder.build());
MID++;
}
}
I saw few questions about this, but I'm not sure how could I do that.
Upvotes: 0
Views: 78
Reputation: 1368
In your AndroidManifest.xml
:
<receiver android:name="com.yourapplication.AlarmReceiver">
<intent-filter>
<action android:name="com.yourapplication.CUSTOM_INTENT" />
</intent-filter>
</receiver>
When preparing your Intent to be fired off when the Alarm hits:
Intent intent = new Intent("com.yourapplication.CUSTOM_INTENT");
intent.putExtra("extra", getFirstName);
PendingIntent pendingIntent = PendingIntent.getBroadcast(AddBirthday.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, test, AlarmManager.INTERVAL_DAY, pendingIntent);
The name is stored in the Intent sent to AlarmReceiver
:
@Override
public void onReceive(Context context, Intent intent) {
String name = intent.getStringExtra("extra");
...
NotificationCompat.Builder mNotifyBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_launcher_icon)
.setContentTitle("Birthday Upcoming for " + name)
...
Upvotes: 1