Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notification on every 5 minutes

I want to give the notification to user on every 5 minutes I am using following code. it shows me notification first time but not give next time.

public void startAlarm() {
    AlarmManager alarmManager = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);
    long whenFirst = System.currentTimeMillis();         // notification time
    Intent intent = new Intent(this, NotifyUser.class);
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
    alarmManager.setRepeating(AlarmManager.RTC, whenFirst, 60*5000, pendingIntent);            
}

public class NotifyUser extends Service {   
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        loadNotification(); 
    }

    private void loadNotification() {
        NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notify = new Notification(R.drawable.ic_launcher/*android.R.drawable.stat_notify_more*/, "Hanumanji waiting for you", System.currentTimeMillis());
        Context context = NotifyUser.this;
        CharSequence title = "Hanumanji is waiting for you";
        CharSequence details = "Do Hanuman Chalisa Parayan with ShlokApp.";
        Intent intent = new Intent(context, NotifyUser.class);
        PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
        notify.setLatestEventInfo(context, title, details, pending);
        notify.sound = Uri.parse("android.resource://pro.shlokapp.hanumanchalisa/"+ R.raw.game_sound_pause);
        nm.notify(0, notify);
    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        return 1;
    }

    public void onStart(Intent intent, int startId) {
        // TO DO
    }

    public IBinder onUnBind(Intent arg0) {
        // TO DO Auto-generated method
        return null;
    }

    public void onStop() {}

    public void onPause() {}

    @Override
    public void onDestroy() {}

    @Override
    public void onLowMemory() {}
}
like image 200
user2546410 Avatar asked Oct 11 '13 12:10

user2546410


2 Answers

it shows me notification first time but not give next time. : The reason is you use nm.notify(0, notify); Do not use 0 as it will show latest notification.

Below code works like a charm :

public class MainActivity extends Activity {

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

    MyTimerTask myTask = new MyTimerTask();
    Timer myTimer = new Timer();

    myTimer.schedule(myTask, 5000, 1500);

}

class MyTimerTask extends TimerTask {
    public void run() {

        generateNotification(getApplicationContext(), "Hello");
    }
}

private void generateNotification(Context context, String message) {

    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    String appname = context.getResources().getString(R.string.app_name);
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    int currentapiVersion = android.os.Build.VERSION.SDK_INT;
    Notification notification;
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            new Intent(context, MainActivity.class), 0);

    // To support 2.3 os, we use "Notification" class and 3.0+ os will use
    // "NotificationCompat.Builder" class.
    if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
        notification = new Notification(icon, message, 0);
        notification.setLatestEventInfo(context, appname, message,
                contentIntent);
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify((int) when, notification);

    } else {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                context);
        notification = builder.setContentIntent(contentIntent)
                .setSmallIcon(icon).setTicker(appname).setWhen(0)
                .setAutoCancel(true).setContentTitle(appname)
                .setContentText(message).build();

        notificationManager.notify((int) when, notification);

    }

}

}

Use Timer class. Change the timer interval as per your needs. Hope this helps.

like image 84
Siddharth_Vyas Avatar answered Nov 03 '22 02:11

Siddharth_Vyas


in this post How exactly to use Notification.Builder there is an example. I used it to make the notification in my app. It also use the NotificationBuilder from the support library.

I think in your code above, you are just updating the notification, that is already there. Try to check it by displaying a number that is increased by one every time you set/update a new notification.

Hope this will help you =).

like image 22
Tim Avatar answered Nov 03 '22 00:11

Tim