【问题标题】:Notification within 24 hours24小时内通知
【发布时间】:2017-03-10 17:45:46
【问题描述】:

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

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

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

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

【问题讨论】:

  • 你确定这个问题以前没有回答过吗?
  • @JonZarate 我没有达到这个目的。如果我遇到了,我不太了解。
  • 正如您所指出的,可以使用 AlarmManager。确保警报调用生成通知的服务,并且一切就绪

标签: java android notifications


【解决方案1】:
       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) {

    }


}

【讨论】:

    【解决方案2】:
    0 0 7 1/1 * ? *
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-09
      • 2021-10-06
      • 1970-01-01
      • 2018-12-21
      相关资源
      最近更新 更多