How to show Android notification after every 48 hours?

I tried the following code for Android notification, which works fine, but it gives me a pop-up notification when launching the Android app.

I want to show a notification every 48 hours, how can I do this?

What changes need to be made for this to work?

Notification code

Intent notificationIntent = new Intent(MainActivity.this, Activity.class); PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent, 0); NotificationManager notificationManager = (NotificationManager) MainActivity.this .getSystemService(Context.NOTIFICATION_SERVICE); Notification noti = new NotificationCompat.Builder(MainActivity.this) .setSmallIcon(R.drawable.ic_launcher) .setTicker("ticker message") .setWhen(System.currentTimeMillis()) .setContentTitle("HELLO") .setContentText("PLEASE CHECK WE HAVE UPDATED NEWS") .setContentIntent(contentIntent) //At most three action buttons can be added .setAutoCancel(true).build(); int notifyID =0; notificationManager.notify(notifyID, noti); 
+7
android android-layout push-notification notifications
source share
3 answers

Here is something close to what you need in your main business:

  Intent notificationIntent = new Intent(context, ShowNotification.class); PendingIntent contentIntent = PendingIntent.getService(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); am.cancel(contentIntent); am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + AlarmManager.INTERVAL_DAY * 2, AlarmManager.INTERVAL_DAY * 2, contentIntent); 

Then in another file called ShowNotification.java add the following (if your main activity is called MainActivity):

 import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Context; import android.content.Intent; import android.os.IBinder; import android.support.v4.app.NotificationCompat; import android.util.Log; public class ShowNotification extends Service { private final static String TAG = "ShowNotification"; @Override public void onCreate() { super.onCreate(); Intent mainIntent = new Intent(this, MainActivity.class); NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); Notification noti = new NotificationCompat.Builder(this) .setAutoCancel(true) .setContentIntent(PendingIntent.getActivity(this, 0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT)) .setContentTitle("HELLO " + System.currentTimeMillis()) .setContentText("PLEASE CHECK WE HAVE UPDATED NEWS") .setDefaults(Notification.DEFAULT_ALL) .setSmallIcon(R.drawable.ic_launcher) .setTicker("ticker message") .setWhen(System.currentTimeMillis()) .build(); notificationManager.notify(0, noti); Log.i(TAG, "Notification created"); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } } 
+9
source share

doing so: - Your notice will be open at the beginning of your activity. - If you add a timer, it will appear only if the application is open.

In your case, you should start a service where you will have a timer that will send an Androis OS notification every 48 hours

For this cycle you will need timmer, you can learn more about the services here: http://www.vogella.com/tutorials/AndroidServices/article.html

+1
source share

You probably want to use the AlarmManager class to schedule your notifications. Here is a snippet that should do this:

 AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); alarmManager.cancel(contentIntent); alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + AlarmManager.INTERVAL_DAY * 2, AlarmManager.INTERVAL_DAY * 2, contentIntent); 
+1
source share

All Articles