24小时内通知

时间:2017-03-10 17:45:46

标签: java android notifications

我创建了一种方法,使用NotificationCompat.Builder and NotificationManager向名为sendNotification ()的用户发送通知。

我需要以24小时的固定时间间隔或甚至在特定时间发布此通知,例如每天 07:00 ,在这种情况下会导致相同的24小时,将来可由用户调整。

在我看来,使用公共类AlarmManager可以执行此过程,但我不确定是否必须创建服务或者它是否是服务本身。

如何在24小时内完成此通知?

2 个答案:

答案 0 :(得分:0)

       public void setLocalNotification(){
                alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
                alarmIntent = new Intent(this, LocalNotificationReceiver.class);
                pendingIntent = PendingIntent.getBroadcast(this, 99, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
                Log.d("TAG ","LocalNotification Start");
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis() + AlarmManager.INTERVAL_DAY, AlarmManager.INTERVAL_DAY, pendingIntent);
            }


public class LocalNotificationReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("TUS-NOTAS ", "LocalNotification Receiver");
        Intent service1 = new Intent(context, ShowNotificationService.class);
        context.startService(service1);
    }
}


public class ShowNotificationService extends IntentService {

    private static final int NOTIFICATION_ID = 1;
    private PendingIntent pendingIntent;
    private NotificationManager notificationManager;
    private final static String TAG = "ShowNotification";

    public ShowNotificationService()
    {
        super("ServiceNotification");
    }


    public ShowNotificationService(String name) {
        super(name);
    }


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

        Context context = this.getApplicationContext();
        Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

         notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
        Intent mIntent = new Intent(this, SplashActivity.class);
        pendingIntent = PendingIntent.getActivity(context,99, mIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setContentTitle(getResources().getString(R.string.app_name));

        builder.setVibrate(new long[] { 200, 200});
        builder.setSound(uri);

        if (Global.getStringKey(getApplicationContext(), Definitions.LANGUAGE_VALUE).equals("en"))
        {
            builder.setStyle(new NotificationCompat.BigTextStyle().bigText("We miss you!"));
            builder.setContentText("You have not added any notes recently.");
        }else if (Global.getStringKey(getApplicationContext(),Definitions.LANGUAGE_VALUE).equals("es"))
        {
            builder.setStyle(new NotificationCompat.BigTextStyle().bigText("Te extrañamos!"));
            builder.setContentText("No has agregado notas recientemente.");
        }


        builder.setAutoCancel(true);
        builder.setSmallIcon(R.drawable.ic_logo_app);

        builder.setContentIntent(pendingIntent);

        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(NOTIFICATION_ID, builder.build());

        Log.d("TUS-NOTAS"," LocalNotification Service");

    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    protected void onHandleIntent(Intent intent) {

    }


}

答案 1 :(得分:-1)

0 0 7 1/1 * ? *

这是由http://www.cronmaker.com/制作的cron语句。您可以在每天早上7点执行的cron作业中使用它。