Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Live activity is not getting create by remote notification in flutter #88

Open
Karishma-au50 opened this issue Sep 11, 2024 · 1 comment

Comments

@Karishma-au50
Copy link

static void startLiveActivity(RemoteMessage message) async {
    try {
      print("initialize liive");
      var liveActivitiesPlugin = LiveActivities();
      await liveActivitiesPlugin.init(
        appGroupId: 'group.coun366.WaterQualityLA',
      );
      print("after iniitiialize live");
      print("inside the liveactivity in try");
      final liveActivityModel = LiveActivityWQModel(
        appLogo: LiveActivityImageFromAsset('assets/icon.jpeg'),
        appNameLogo: LiveActivityImageFromAsset('assets/newLogo.png'),
        title: message.notification?.title ?? "DO LOW",
        pondId: message.data['pondId'] ?? "Unknown Pond ID",
        pondName: message.data['pondName'] ?? "Unknown Pond Name",
      );
      print("before end activiities");

      print("before activityId");

      final activityId =
          await liveActivitiesPlugin.createActivity(liveActivityModel.toMap());
      print('Live Activity started with ID: $activityId');
    } catch (e) {
      print('Failed to start Live Activity: $e');
    }
  }

I am calling this startLiveActivity function in my notification file whose code is given below.

import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:live_activities/live_activities.dart';
import 'package:live_activities/models/live_activity_image.dart';

import '../Models/LiveActivity/live_activity_wq_model.dart';

class LocalNotificationService {
  static final FlutterLocalNotificationsPlugin _notificationsPlugin =
      FlutterLocalNotificationsPlugin();

  static void initialize() {
    FirebaseMessaging.instance.requestPermission(
      alert: true,
      announcement: true,
      badge: true,
      carPlay: false,
      criticalAlert: true,
      provisional: true,
      sound: true,
    );

    const InitializationSettings initializationSettings =
        InitializationSettings(
      android: AndroidInitializationSettings("@mipmap/ic_launcher"),
      iOS: DarwinInitializationSettings(),
    );

    _notificationsPlugin.initialize(initializationSettings);
  }

  static void createAndDisplayNotification(RemoteMessage message) async {
    try {
      final id = DateTime.now().millisecondsSinceEpoch ~/ 1000;
      NotificationDetails notificationDetails;

      message.data['isAlarm']?.toString() == "true"
          ? notificationDetails = NotificationDetails(
              android: AndroidNotificationDetails(
                "AquagenixProPriority",
                "AquagenixProChennalPriority",
                importance: Importance.max,
                priority: Priority.high,
                sound: RawResourceAndroidNotificationSound(
                    message.notification?.android?.sound),
              ),
              iOS: DarwinNotificationDetails(
                presentSound: true,
                sound: message.notification?.apple?.sound?.name,
              ),
            )
          : notificationDetails = const NotificationDetails(
              android: AndroidNotificationDetails(
                "AquagenixPro",
                "AquagenixProChennal",
                importance: Importance.max,
                priority: Priority.high,
              ),
              iOS: DarwinNotificationDetails(),
            );

      await _notificationsPlugin.show(
        id,
        message.notification!.title,
        message.notification!.body,
        notificationDetails,
      );

      startLiveActivity(message);
    } on Exception catch (e) {
      print(e);
    }
  }

  static void startLiveActivity(RemoteMessage message) async {
    try {
      print("initialize liive");
      var liveActivitiesPlugin = LiveActivities();
      await liveActivitiesPlugin.init(
        appGroupId: 'group.coun366.WaterQualityLA',
      );
      print("after iniitiialize live");
      print("inside the liveactivity in try");
      final liveActivityModel = LiveActivityWQModel(
        appLogo: LiveActivityImageFromAsset('assets/icon.jpeg'),
        appNameLogo: LiveActivityImageFromAsset('assets/newLogo.png'),
        title: message.notification?.title ?? "DO LOW",
        pondId: message.data['pondId'] ?? "Unknown Pond ID",
        pondName: message.data['pondName'] ?? "Unknown Pond Name",
      );
      print("before end activiities");

      print("before activityId");

      final activityId =
          await liveActivitiesPlugin.createActivity(liveActivityModel.toMap());
      print('Live Activity started with ID: $activityId');
    } catch (e) {
      print('Failed to start Live Activity: $e');
    }
  }


}

I am getting issue that when my app is open and notification comes from server my live activity gets created and come on lock screen but when I kill the app and notification comes my live activity did not get create and not even come on lock screen.And I am calling notification file in my main.dart file.I am using live activity package

I have tested the app in foreground and send the push notification then activity are created but when i killed the app and send push notification then it's not create even i checked with flutter logs command

@SnapDrive
Copy link
Contributor

Hi,
I think the problem occurs due to the fact that this method is only called if the app is in foreground/background and not fully closed. See example here: https://apnspush.com/how-to-start-and-update-live-activities-with-push-notifications#_2-send-push-notifcation-to-start-a-live-activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants