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

Callback when scheduled notification is shown #21

Closed
radzish opened this issue Apr 21, 2018 · 12 comments
Closed

Callback when scheduled notification is shown #21

radzish opened this issue Apr 21, 2018 · 12 comments
Labels
blocked enhancement New feature or request help wanted Extra attention is needed

Comments

@radzish
Copy link

radzish commented Apr 21, 2018

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

@MaikuB
Copy link
Owner

MaikuB commented Apr 21, 2018

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

@radzish
Copy link
Author

radzish commented Apr 22, 2018

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.

@MaikuB
Copy link
Owner

MaikuB commented Apr 22, 2018

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

@MaikuB
Copy link
Owner

MaikuB commented Apr 28, 2018

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

@MaikuB MaikuB closed this as completed Apr 28, 2018
@MaikuB MaikuB added enhancement New feature or request wontfix This will not be worked on and removed wontfix This will not be worked on labels Apr 28, 2018
@MaikuB
Copy link
Owner

MaikuB commented Apr 29, 2018

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...

@MaikuB
Copy link
Owner

MaikuB commented Sep 2, 2018

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)

@cdharris
Copy link

cdharris commented Mar 5, 2019

Hi! Apologies if this doesn't change much but I noticed in flutter/flutter#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.

@MaikuB
Copy link
Owner

MaikuB commented Mar 5, 2019

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

@real1900
Copy link

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

@MaikuB
Copy link
Owner

MaikuB commented Apr 28, 2020

@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

@yourshinsuke
Copy link

yourshinsuke commented Sep 1, 2020

@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,
    );

@yourshinsuke
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

5 participants