Flutter_local_notifications: Callback when scheduled notification is shown

Created on 21 Apr 2018  路  12Comments  路  Source: MaikuB/flutter_local_notifications

It would be really nice to have a callback that would be called when notification is shown.

blocked enhancement help wanted

All 12 comments

Can you give me a sample use case for that feature? Also, from my understanding this is blocked on the Flutter engine as it needs to be able to fire callbacks even the app is terminated. It's the same reason why I haven't added the ability to define custom actions #17

Use case: event reminder showing how much time left to event. We need to constantly update such reminder.
Terminated app case is now supported on Android. So to me it would be nice to have such functionality implemented for Android at least. Anyway even for native iOS it is not possible to do such kind of thing.

I can try to take a look but concerned on if the Flutter API will change to not use isolates if they figure out how to get it working for iOS. Don't know what you mean on how it's not possible for native iOS but at the least this is exposed on iOS
https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate/1649518-usernotificationcenter

Had more thoughts about this. Even if Flutter provides headless Dart-support for iOS, providing a callback/event that doesn't work the same way across all platforms doesn't align with my goal of providing cross-platform abstractions. So, unfortunately I won't be looking to add this and recommend you implement the functionality yourself. You could have your application reference a local copy of the plugin's code that also includes your own customisations to handle this scenario

On second thought, I'm going to reopen this as an alternative is to handle the callback as part of the NotificationDetailsAndroid class as opposed to callback that is associated with the FlutterLocalNotificationsPlugin class. That is, once I figured out how to make use of the headless Dart code execution capability...

Tried to take a look at this and had some success but every now and then I'll run into exceptions on the Flutter side. Not too sure if it's a stability issue with Flutter or not at the moment but if someone out there is able to help then it'd be much appreciated. Code is on the onnotification_callback branch. It's something that should work on iOS but have only started looking at it on Android at the moment.

What I have found is that headless execution will only work on callbacks that fire top-level or static functions. Unless I'm missing something, I believe this means separate callbacks are required depending on if they require the app to be running as top-level/static functions won't get access to a BuildContext (e.g. to trigger navigation) and wasn't able to start the app using just code on the Dart side using top-level/static functions (e.g. by call runApp(...) again)

Hi! Apologies if this doesn't change much but I noticed in https://github.com/flutter/flutter/issues/21925 support for doing background work that accesses plugins in iOS is now supported.

Any chance of being able to update the onnotification_callback branch with master so I can try it out with the latest code?

Really eager to have some background execution work in flutter! Thanks.

There's actually another issue I've raised since I had noticed the headless execution could stop working that from what I gather, will require a solution from Flutter on dealing with plugins that use multiple isolates. I believe that is still ongoing

Here is my solution to this:

Add the following method for iOS:
`

  • (void)notifyFlutter:(UNNotification * _Nonnull)notification API_AVAILABLE(ios(10.0)){
    NSMutableDictionary *arguments = [[NSMutableDictionary alloc] init];
    arguments[ID]= notification.request.content.userInfo[NOTIFICATION_ID];
    if (notification.request.content.userInfo[TITLE] != [NSNull null]) {
    arguments[TITLE] =notification.request.content.userInfo[TITLE];
    }
    if (notification.request.content.body != nil) {
    arguments[BODY] = notification.request.content.body;
    }
    if (notification.request.content.userInfo[PAYLOAD] != [NSNull null]) {
    arguments[PAYLOAD] =notification.request.content.userInfo[PAYLOAD];
    }
    [_channel invokeMethod:DID_RECEIVE_LOCAL_NOTIFICATION arguments:arguments];
    }
    `
    Call it inside Will present Notification as follows:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification :(UNNotification *)notification withCompletionHandler :(void (^)(UNNotificationPresentationOptions))completionHandler NS_AVAILABLE_IOS(10.0) {
    UNNotificationPresentationOptions presentationOptions = 0;
    NSNumber *presentAlertValue = (NSNumber*)notification.request.content.userInfo[PRESENT_ALERT];
    NSNumber *presentSoundValue = (NSNumber*)notification.request.content.userInfo[PRESENT_SOUND];
    NSNumber *presentBadgeValue = (NSNumber*)notification.request.content.userInfo[PRESENT_BADGE];
    bool presentAlert = [presentAlertValue boolValue];
    bool presentSound = [presentSoundValue boolValue];
    bool presentBadge = [presentBadgeValue boolValue];
    if(presentAlert) {
        presentationOptions |= UNNotificationPresentationOptionAlert;
    }

    if(presentSound){
        presentationOptions |= UNNotificationPresentationOptionSound;
    }

    if(presentBadge) {
        presentationOptions |= UNNotificationPresentationOptionBadge;
    }
    **[self notifyFlutter:notification];**

    completionHandler(presentationOptions);
}

This should trigger _didReceiveLocalNotificationSubject_. See attached .

FlutterLocalNotificationsPlugin.m.zip

@real1900 note that only works if the app is in the foreground.

To follow up I'm going to close this issue as it's been around as it's been around for two years now and the background execution process for Flutter hasn't had changes and may even no longer be supported. Feel free to fork the repo and modify the code to support the cases you need

@radzish I agree with you!

Finally I did like this 9223372036854775 as 292471208 years later (now one live then, or Android may already dead :>)

    await flutterLocalNotificationsPlugin.schedule(
      0,
      null,
      null,
      DateTime.now().add(Duration(seconds: 9223372036854775)),
      platformChannelSpecifics,
    );

ahhhhh.
No show() calling, no register channel.
WTF

Was this page helpful?
0 / 5 - 0 ratings