-1

I am new to android development , i want to show push notification every day when application is closed in android. I know that i need to create service and broadcast receiver for it but i don't to do it practically. Here is my code for simple push notification please help me to make the push notification in every day.

public class MainActivity extends Activity {
EditText ed1,ed2,ed3;
Notification notification;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button b1=(Button)findViewById(R.id.button);
    final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    b1.setOnClickListener(new View.OnClickListener() {
        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public void onClick(View v) {
  Notification.Builder builder = new Notification.Builder(MainActivity.this);
            Intent resultIntent = new Intent(MainActivity.this,Splash.class);
            resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
                    resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            builder.setSmallIcon(R.drawable.icon)
                    .setContentIntent(pendingIntent)
                    .setContentTitle("title");

            notification = builder.build();
            manager.notify(0, notification);
 }
    });
}}

Please help me to achieve the functionality i need with alarm manager or services.

Siva Kumar
  • 57
  • 1
  • 6
  • 2
    you can use alaram maneger Calendar time = Calendar.getInstance(); time.set(Calendar.HOUR_OF_DAY, 5); alarmMgr.setRepeating(AlarmManager.RTC, time.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingI); – saeed Apr 18 '16 at 09:23
  • Check this answer http://stackoverflow.com/a/32305374/403255 for alarm manager. Check http://developer.android.com/reference/android/app/NotificationManager.html for creating notifications – Vinothkumar Arputharaj Apr 18 '16 at 09:38

1 Answers1

2

You can use this code for set an alarm at 7:00 AM morning

Calendar calendar = Calendar.getInstance();
// 7.00 (7 AM) 
calendar.set(Calendar.HOUR_OF_DAY, 7);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);

PendingIntent pi = PendingIntent.getService(context, 0 , new Intent(context, Your_Class.class),PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                        AlarmManager.INTERVAL_DAY, pi);

Use this tutorial for solution notification for a chosen time

Take a look at this also Fire notification at every 24 hours and at exact time of 8 AM

Community
  • 1
  • 1
Praval Sharma
  • 1,921
  • 2
  • 14
  • 20