Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show android notification after every 48hours?

I tried following code for android notification which is working fine but it is giving me notification popup when I start my android app.

I want to show an notification after every 48hours, how can I do it?

What changes do I need to make in order 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);
like image 677
Neo Avatar asked Mar 06 '14 20:03

Neo


People also ask

How do I show progress notifications on Android?

To display a determinate progress bar, add the bar to your notification by calling setProgress(max, progress, false) and then issue the notification. The third argument is a boolean that indicates whether the progress bar is indeterminate (true) or determinate (false).


2 Answers

Here's something close to what you need in your main activity:

    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 named ShowNotification.java, add the following (assuming your main activity is named 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;
    }
}
like image 87
scottt Avatar answered Oct 09 '22 13:10

scottt


doing at this way: - Your notification will open at the starting of your activity. - If you add a timer in, it only will appears if you have the app opened.

In your case, you should start an service, where you will have a timer that each 48 hours send a notification to Androis O.S.

For to do this loop, you will need a timmer, you can see more about services here: http://www.vogella.com/tutorials/AndroidServices/article.html

like image 42
twcavalli Avatar answered Oct 09 '22 13:10

twcavalli