gdelataillade / alarm

A Flutter plugin to easily manage alarms on iOS and Android
https://pub.dev/packages/alarm
MIT License
132 stars 86 forks source link

iOS crash on v4.0.1 #247

Closed aykutuludag closed 1 month ago

aykutuludag commented 1 month ago

Alarm plugin version 4.0.1

Describe the bug App crashing when Alarm.init line executed.

Device info All iOS versions

Additional context Firebase Crashlytics report

Crashed: com.gdelataillade.alarm.resourceAccessQueue
0  alarm                          0xb838 $s5alarm16SwiftAlarmPluginC03setC033_6E2EED5D4137BAF8EC3EC9399D6BB069LL4call6resultySo17FlutterMethodCallC_yypSgXEtFyycfU0_ + 620
1  alarm                          0x11380 $s5alarm16SwiftAlarmPluginC19safeModifyResourcesyyyycFyyYbcfU_TA + 20
2  alarm                          0xbc54 $sIegh_IeyBh_TRTm + 28
3  libdispatch.dylib              0x213c _dispatch_call_block_and_release + 32
4  libdispatch.dylib              0x3dd4 _dispatch_client_callout + 20
5  libdispatch.dylib              0xb400 _dispatch_lane_serial_drain + 748
6  libdispatch.dylib              0xbf30 _dispatch_lane_invoke + 380
7  libdispatch.dylib              0x16cb4 _dispatch_root_queue_drain_deferred_wlh + 288
8  libdispatch.dylib              0x16528 _dispatch_workloop_worker_thread + 404
9  libsystem_pthread.dylib        0x4934 _pthread_wqthread + 288
10 libsystem_pthread.dylib        0x10cc start_wqthread + 8
aykutuludag commented 1 month ago

For Android I only detected following issue, happening very rare:

Fatal Exception: java.lang.RuntimeException: Unable to start service com.gdelataillade.alarm.alarm.AlarmService@2c1a71e with null: java.lang.NullPointerException
       at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4712)
       at android.app.ActivityThread.access$2000(ActivityThread.java:309)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2236)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loop(Looper.java:264)
       at android.app.ActivityThread.main(ActivityThread.java:8315)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:632)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1049)

image

aykutuludag commented 1 month ago

Following functions called from Home.dart. I make sure the functions only called once. It is just called as setUpAlarms(context) inside initState.

But alarm library logs is kinda weird like library executing alarm function twice. Here is the code (I removed unnecessary parts):

Future<void> setUpAlarms(BuildContext context) async {
  await Alarm.init();

  // TEMPORARY
  if (Platform.isAndroid &&
      await Permission.scheduleExactAlarm.status.isGranted == false) {
    await Permission.scheduleExactAlarm.request();
  }
  // TEMPORARY

  await Alarm.stop(123);
  await Alarm.stop(456);

  createAlarm(
      123, dateTime, AppLocalizations.of(context).appName, formattedDate);
  createAlarm(
      456, dateTime2, AppLocalizations.of(context).appName, formattedDate2);
}

Future<void> createAlarm(
    int alarmId, DateTime dateTime, String title, String body) async {
  final alarmSettings = AlarmSettings(
      id: alarmId,
      dateTime: dateTime,
      assetAudioPath: 'assets/notification.mp3',
      loopAudio: false,
      vibrate: true,
      volume: 1,
      androidFullScreenIntent: false,
      warningNotificationOnKill: false,
      notificationSettings: NotificationSettings(
        title: title,
        body: body,
      ));
  await Alarm.set(alarmSettings: alarmSettings);
}

Here is the logs:

I/flutter (24813): [Alarm] Alarm with id 63 stopped
I/flutter (24813): [Alarm] Alarm with id 132 stopped
I/flutter (24813): [Alarm] Alarm with id 132 scheduled at 2024-11-06 21:00:00.000
I/flutter (24813): [Alarm] Alarm with id 63 stopped
I/flutter (24813): [Alarm] Alarm with id 132 stopped
I/flutter (24813): [Alarm] Alarm with id 63 scheduled at 2024-10-04 10:00:00.000
I/flutter (24813): [Alarm] Alarm with id 132 scheduled at 2024-11-06 21:00:00.000

Normally according to code, I'm expecting like this:

I/flutter (24813): [Alarm] Alarm with id 63 stopped
I/flutter (24813): [Alarm] Alarm with id 132 stopped
I/flutter (24813): [Alarm] Alarm with id 63 scheduled at 2024-10-04 10:00:00.000
I/flutter (24813): [Alarm] Alarm with id 132 scheduled at 2024-11-06 21:00:00.000

I'm getting the crashes on iOS and Android with this code. Have a nice day. Kind regards.

gdelataillade commented 1 month ago

Hi @aykutuludag !

Thanks for your feedback. I have a few questions:

1) How often the iOS crash occurs ? It crashes when app opens and alarms were previously scheduled right ? 2) About the Android crash, I will take a look later if it's not the priority. 3) About your setUpAlarms method, have you tried to add await before each call of createAlarm ?

aykutuludag commented 1 month ago

1) Hard to tell. Sometimes it is crashing three times in a row, sometimes it doesnt crash a long period. Yes, it is crashing on Alarm.init line. It doesn't matter alarms scheduled or not. I saw on Xcode, I guess it was crashing on the following line in your native ios code.

image

2) It is happening rarely. I will notify you if I saw again on new version. Not encountered for now.

3) I can but isn't await Alarm.set(alarmSettings: alarmSettings); is working for that purpose?

Note: I surroundered with try/catch, doesn't matter still crashing because crash not happened on dart side, it is in swift code.

aykutuludag commented 1 month ago

I was lucky to catch the error when I try. Here is the error my friend.

WhatsApp Image 2024-10-05 at 03 50 28_8054d5b5