07-05-2019 08:14
07-05-2019 08:14
I am trying to hook into the event listener code to be alerted when I hit a goal. This is the code I have to register for the event:
goals.addEventListener("reachgoal", (goal, evt) => {
console.log(`event fired: ${JSON.stringify(goal)} ${JSON.stringify(evt)}`);
testGoals(goal);
});
But I never see the note in the log, and my code to update the watch face doesn't ever run to show me I hit a goal. Am I doing something wrong here? Do I just have to check for completing a goal every minute when I update the time? Thanks
07-05-2019 09:36
07-05-2019 09:36
Are you trying this on a real device, and you requested the `access_activity` permission?
I previously used this one:
import { goals, today } from "user-activity"; goals.onreachgoal = function(evt) { // check each metric, see which has been met or exceeded if (today.adjusted.steps >= goals.steps { // yay } }
07-05-2019 13:03
07-05-2019 13:03
Yes, I'm running the app on my Fitbit Ionic and it is set up with activity permissions. I can display the stats for the day, just don't seem to be getting the event when a goal is completed. I was following the docs in using addEventListener(), if the proper way is to hook into goals.onreachgoal instead, the docs should be updated to make that clear.
I'll try your way of hooking in a callback function, thanks for the idea.
08-14-2019 18:22
08-14-2019 18:22
I tried your code and it still doesn't fire the events. I hooked in a message to send to the companion app and spit out a line to the logs so I can see if it fires, but it never shows up. Here is the code I have, is there something I'm doing wrong? Thanks
// imports
import { today, goals } from "user-activity";
// I do this chunk in my init function
goals.onreachgoal = function(evt) {
console.log(`onreachgoal event fired: ${JSON.stringify(evt)} ${JSON.stringify(goals)}`);
if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) {
messaging.peerSocket.send({command: "activity", evt: JSON.stringify(evt), goals: JSON.stringify(goals)});
}
testGoals();
};
// and this is the function that tests the goals
// the 'weeklyGoals' variable is an object where the key are the goal names and
// they hold an array tracking if the goal was met for each day of the week
function testGoals() {
let goalCompleted = false;
const curDay = getCurDay();
Object.keys(weeklyGoals).forEach((goalName) => {
if (today.adjusted[goalName] >= goals[goalName] && !weeklyGoals[goalName][curDay].completed) {
console.log(`Reached ${goalName} for the day`);
weeklyGoals[goalName][curDay].completed = true;
goalCompleted = true;
}
});
if (goalCompleted) {
updateWeeklyGoals(true);
}
}
08-14-2019 20:16
08-14-2019 20:16
Is your init() definitely being executed? You could do something like console.log(`goals.onreachgoal`) afterwards to verify that your event handler has been assigned.
08-14-2019 21:09
08-14-2019 21:09
Yes, it is definitely getting called. I have another call to testGoals() in there to catch completing a goal while the watch face isn't active, and that works. I tried adding the console log and it spit out "on reach goal: function(){/* ecmascript */}"
03-31-2022 06:53 - edited 04-03-2022 05:47
03-31-2022 06:53 - edited 04-03-2022 05:47
Does the goal detection work in a clock face?
It doesn't seem to trigger with either, when the goal is reached on the watch.
goals.addEventListener("reachgoal", (goal, evt) => {
goals.onreachgoal = function(evt) {
Is there something special that needs defining?
defined:
import { today, goals } from "user-activity";
and Activity permission requested in the package.json
Update:
Problem solved - user profile permission was not requested in the test version
Author | ch, passion for improvement.