Rexios80 / wear_ongoing_activity

Create and maintain ongoing activity notifications on Wear OS
https://pub.dev/packages/wear_ongoing_activity
BSD 3-Clause "New" or "Revised" License
3 stars 2 forks source link

wear_ongoing_activity

Create and maintain ongoing activity notifications on Wear OS

Getting Started

AndroidManifest.xml

<!-- Add permissions for the types specified in `foregroundServiceType` below -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_HEALTH" />
<uses-permission android:name="android.permission.BODY_SENSORS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_REMOTE_MESSAGING" />

<application>
    ...
    <service
        android:name="dev.rexios.wear_ongoing_activity.OngoingActivityService"
        android:exported="false"
        android:foregroundServiceType="health|remoteMessaging|..."
        android:stopWithTask="true" />
    ...
</application>

Make sure to request notification permission before starting an activity. Also request any permissions required for the specified foregroundServiceTypes.

import 'package:permission_handler/permission_handler.dart';

void main() {
  [Permission.notification, Permission.sensors].request();
}

Usage

Start an activity

import 'package:wear_ongoing_activity/wear_ongoing_activity.dart';

void main() {
  WearOngoingActivity.start(
    channelId: 'ongoing_activity',
    channelName: 'Ongoing Activity',
    notificationId: 12345,
    category: NotificationCategory.workout,
    // This must be a file in `android/app/src/main/res/drawable`
    smallIcon: 'ic_launcher',
    // This must be a file in `android/app/src/main/res/drawable`
    staticIcon: 'ic_launcher',
    status: OngoingActivityStatus(
      templates: [
        '#type#: #time#',
      ],
      parts: [
        TextPart(name: 'type', text: 'countup'),
        StopwatchPart(name: 'time', timeZero: DateTime.now()),
      ],
    ),
  );
}

Update an activity

import 'package:wear_ongoing_activity/wear_ongoing_activity.dart';

void main() async {
  if (!await WearOngoingActivity.isOngoing()) {
    // Attempting to update an activity that is not ongoing will throw an error
    return;
  }

  await WearOngoingActivity.update(
    OngoingActivityStatus(
      templates: [
        '#type#: #time#',
      ],
      parts: [
        TextPart(name: 'type', text: 'countdown'),
        TimerPart(
          name: 'time',
          timeZero: DateTime.now().add(const Duration(minutes: 5)),
        ),
      ],
    ),
  );
}

Stop an activity

import 'package:wear_ongoing_activity/wear_ongoing_activity.dart';

void main() {
  WearOngoingActivity.stop();
}