首页 > 解决方案 > 仅在达到目标后显示一次通知

问题描述

当用户达到目标(步行 84 公里)时,我正在显示通知。我正在显示此通知,但是当我重新打开应用程序时,它再次显示。我怎样才能让它只运行一次(一旦用户到达84公里)?

这是我正在检查 KM 的代码(在asynctask中)

protected String doInBackground(String... aurl) {

        totalKm = (int) Float.parseFloat(TripsInfo.km.get(TripsInfo.userRank - 1));


        if (totalKm == 84 ) {
            milestoneNotification(totalKm);


        } else if (totalKm == 130 ) {
            milestoneNotification(totalKm);

        } else if (totalKm == 200) {
            milestoneNotification(totalKm);

        }

        return null;
    }

和另一个创建通知的功能

public void milestoneNotification(int km_up) {
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(Profile.this, CHANNEL_ID)
            .setSmallIcon(R.mipmap.icon_app)
            .setContentTitle("Gekon")
            .setContentText("Congratulations!You reached " + km_up + "KM!")
            .setVibrate((new long[]{1000, 1000, 1000, 1000, 1000}))
            .setLights(Color.GREEN, 3000, 3000)
            .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0))
            .setAutoCancel(true)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT);

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

    // notificationId is a unique int for each notification that you must define
    notificationManager.notify(1, mBuilder.build());

}

标签: androidnotifications

解决方案


推荐阅读