1

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.

1

1 Answer 1

1

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"); } 
Sign up to request clarification or add additional context in comments.

1 Comment

I need to pas those data from Activity. I there any difference between Bundle and direct Extra in this case?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.