我正在尝试通过以下代码发送推送通知。 问题:我想在两天后向用户发送推送通知。
在主类中
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);
接收者类是
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());
}
}
困惑:当我运行此代码时,通知会立即发送,两天后也会发送。我不会立即发送通知,我只想在两天后一次又一次地发送。其次,每次用户启动应用程序时我必须调用上述代码还是只调用一次?或者我必须在两天后打电话给它?只要安装了应用程序,我想每两天发送一次通知。
请指导我。谢谢