Question

I am trying to send push notification through the following code. Problem: I want to send push notification to users after two days.

In MainClass

Intent intent = new Intent(MainActivity.this, NotificationReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), AlarmManager.INTERVAL_DAY*2, pendingIntent);

The Reciever Class is

public class NotificationReciever extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent arg1) {
    showNotification(context);

}


private void showNotification(Context context) {
    // TODO Auto-generated method stubMain

    PackageManager pm = context.getApplicationContext().getPackageManager();
    ApplicationInfo ai;
    try {
        ai = pm.getApplicationInfo( context.getPackageName(), 0);
    } catch (final NameNotFoundException e) {
        ai = null;
    }
    final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");

    Intent intent = new Intent(context, MainActivity.class);
    PendingIntent pi = PendingIntent.getActivity(context, 0, intent, 0);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
    .setSmallIcon(R.drawable.ic_launcher)
    .setContentTitle(applicationName)
    .setContentText("We haven't seen you in a while. Play now");
    mBuilder.setContentIntent(pi);
    mBuilder.setDefaults(Notification.DEFAULT_SOUND);
    mBuilder.setAutoCancel(true);
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(0, mBuilder.build());
}

}

Confusions: When I run this code, the notification is sent immediately and after two days too. I don't to send the notification immediately I only want to send it after two days again and again. Secondly do I have to call the above code every time the user starts the app or I have to call it once only? Or I have to call it after two days? I want to send the notification every two days as long as the app is installed.

Please guide me. Thanks

Was it helpful?

Solution

The setRepeating method 2nd parameter suggests when the first alarm should go off. You have specified it as System.currentTimeMillis(), hence it goes off immediately.

Change:

am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), AlarmManager.INTERVAL_DAY*2, pendingIntent);

to

long updateInterval = AlarmManager.INTERVAL_DAY*2;
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + updateInterval, updateInterval, pendingIntent);

Regarding your 2nd question

Secondly do I have to call the above code every time the user starts the app or I have to call it once only? Or I have to call it after two days? I want to send the notification every two days as long as the app is installed.

You will have to call it once. The notification will shown every two days until the app is uninstalled.

You can register a BroadcastReceiver for intent android.intent.action.PACKAGE_FIRST_LAUNCH, which will be sent out the first time your application is launched. Here you can set the repeating alarm. May be you need to handle android.intent.action.PACKAGE_REPLACED, in case a newer version of your app is installed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top