【问题标题】:daily notification showing at app startup应用启动时显示的每日通知
【发布时间】:2017-10-19 13:29:10
【问题描述】:

我希望我的应用每天在特定时间显示通知。为此,我将以下代码放入我的 MainActivity 类的 onCreate 方法中(应用程序启动时默认打开)。

Mainactivity.class

public class MainActivity extends AppCompatActivity {

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

        setNotification(Mainactivity.this);
    }

    public void setNotification(Context context) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY,12);
        calendar.set(Calendar.MINUTE,1);
        calendar.set(Calendar.SECOND,0);
        long c = System.currentTimeMillis();
        long t = calendar.getTimeInMillis();
        if (t <= c) {
            calendar.add(Calendar.DATE, 1);
        }
        Intent intent = new Intent(this, NotificationReciever.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 100, intent, 
        PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);    alarmManager.set(AlarmManager,RTC_WAKEUP,calendar.getTimeInMillis(),pendingIntent);

        boolean alarmUp = (PendingIntent.getBroadcast(MainActivity.this, 100,
        new Intent(this, NotificationReciever.class),
        PendingIntent.FLAG_NO_CREATE) != null);
        if (alarmUp) {
          Toast.makeText(MainActivity.this,"Aan",Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(MainActivity.this,"Niet aan",Toast.LENGTH_SHORT).show();
        }
    }

然后在 NotificationReceiver 类中,我简单地构造并启动通知。它在正确的时间执行此操作,但也始终在应用程序启动时执行此操作。我不知道如何在应用程序启动时停止通知停止启动,有人可以帮我解决这个问题吗?

我尝试过的一件事是检查当前时间是否大于或等于应该启动通知的时间。我通过在 NotificationReceiver 类中实现以下代码来做到这一点:

NotificationReceiver.class

public class NotificationReciever extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);

        Intent new_intent = new Intent(context, MainActivity.class);
        new_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, new_intent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder notification = new NotificationCompat.Builder(context)
            .setContentIntent(pendingIntent)
            .setSmallIcon(R.drawable.ic_stat_name)
            .setTicker("ticker_title")
            .setContentTitle("content_title")
            .setContentText("description")
            .setAutoCancel(true);

        notificationManager.notify(0, notification.build());

        MainActivity mainActivity = new MainActivity();
        mainActivity.setNotification(context);
    }
}

但是,这种方式在应用程序启动之前不会启动通知,在本例中是 12:01,但在此时间之后它仍然会启动..(顺便说一下,以下代码已添加到我的清单中)

<receiver android:name=".NotificationReciever"/>

【问题讨论】:

  • 你已经接近了。根据当前时间检查闹钟时间正是您想要做的,但如果当前时间更大,您不想立即显示Notification,您只想在闹钟Calendar 中添加一天 - 例如@987654326 @ - 像往常一样设置闹钟。
  • 我会尽快尝试,但事先有一个关于它的问题。如果“calendar.add(Calendar.DATE, 1);”中的 1 是否正确会被 2 代替,警报会每隔一天触发一次吗?
  • 我编辑了这个问题,插入“calendar.add(Calendar.DATE, 1);”但是每次启动应用程序时仍然会触发通知..
  • 您插入了calendar.set(Calendar.DATE, 1);,而不是add(),它放错了位置。您想在 if 块中添加一天。
  • 我删除了之前的评论,实际上还是不行。每次启动应用都会在设置的时间后显示通知

标签: android notifications alarmmanager alarm repeatingalarm


【解决方案1】:

您应该使用service job 来安排任务

【讨论】:

    【解决方案2】:

    使用 Firebase Job Dispatcher ,您也可以使用 JobScheduler 但 Firebase Job Dispatcher 会很简单。休息你的选择。

    【讨论】:

    • 我是 Android Studio 的新手,我找不到任何有关 Firebase Job Dispatcher 的有用教程。你知道我应该如何实现它吗?
    • Firebase Job Dispatcher 的 README.md 绰绰有余,跟着这个你就明白了:github.com/firebase/firebase-jobdispatcher-android/blob/master/…
    • 谢谢你的回答,我觉得这个方法更通用,也更容易使用。目前我没有时间研究这个,但我会记住它以备后用:)
    猜你喜欢
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多