Reputation: 1840
My aplication is making notification in any specific time. I'm using BroadcastReceiver and Service class where the notificationManager is. I need to pass some data(title of item) to notification. What is the best way?
This is in main Activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_day);
Intent myIntent = new Intent(Config.context, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(Config.context, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)Config.context.getSystemService(Config.context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, timeInMillis, 24 * 60 * 60 * 1000, pendingIntent);
}
Here is my BrodacastReceiver
public class MyReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
String title = intent.getStringExtra(DayActivity.EXTRA_TITLE);
Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);
}
}
And here Service class
@SuppressWarnings("static-access")
@Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),DayActivity.class);
Notification notification = new Notification(R.drawable.notification_logo, "Title", System.currentTimeMillis());
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(this.getApplicationContext(), "Title", "Message!", pendingNotificationIntent);
mManager.notify(0, notification);
}
I tried to use intents extra but it doesnt work when the app is down.
Upvotes: 1
Views: 1386
Reputation: 2577
while starting service just put a bundle to that service intent
In Receiver:
String title = intent.getStringExtra(DayActivity.EXTRA_TITLE);
Intent service1 = new Intent(context, MyAlarmService.class);
Bundle dataBundle=new Bundle();
dataBundle.putString("key","value");
context.startService(service1);
In Service:
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
Bundle dataBundle=intent.getExtras();
String value=dataBundle.getString("key");
}
Upvotes: 1