question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[DOC NEEDED] - How to implement a custom notifications delegate on iOS?

See original GitHub issue

Description of the problem:

First of all, I’m sorry if this shouldn’t be reported as bug report, but I need help with this matter as soon as possible.

The problem that I’m facing is related to having a custom notifications delegate on iOS. I have been having a look at Capacitor core and I have seen that there is a delegate set for UNUserNotificationCenter. However, this delegate implementation doesn’t fit me because my app business requirements need another one. How could I override this implementation? If I try to register another delegate in my application AppDelegate, which is versioned, it doesn’t work because Capacitor core registers its delegate later.

On the other hand, I does be able to do what I want on Android by registering a custom FirebaseMessagingService in AndroidManifest.xml. In addition, I can even call Capacitor Push Notifications plugin to avoid breaking its implementation.

public class MyAppFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onNewToken(String newToken) {
        super.onNewToken(newToken);
        PushNotifications.onNewToken(newToken);
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        PushNotifications.sendRemoteMessage(remoteMessage);
        // I can do whatever I want
    }

}
<service android:name="com.demo.myapp.services.MyAppFirebaseMessagingService" android:stopWithTask="false">
    <intent-filter android:priority="9999">
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

Affected platform

  • Android
  • iOS
  • electron
  • web

OS of the development machine

  • Windows
  • macOS
  • linux

Other information:

Capacitor version: 1.0

node version: 11.4.0

npm version: 6.5.0

CocoaPods version: 1.7.1

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
jcesarmobilecommented, Jul 25, 2019

Just did some testing and by doing this with a custom UNUserNotificationCenterDelegate in AppDelegate the Capacitor one keeps working as expected.

func userNotificationCenter(_ center: UNUserNotificationCenter,
                              didReceive response: UNNotificationResponse,
                              withCompletionHandler completionHandler: @escaping (() -> Void)) {
    // Do your custom handling here
    let capVC = self.window?.rootViewController as? CAPBridgeViewController
    capVC?.bridge?.notificationsDelegate.userNotificationCenter(center, didReceive: response, withCompletionHandler: completionHandler)
  }
    
  func userNotificationCenter(_ center: UNUserNotificationCenter,
                              willPresent notification: UNNotification,
                              withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    // Do your custom handling here
    let capVC = self.window?.rootViewController as? CAPBridgeViewController
    capVC?.bridge?.notificationsDelegate.userNotificationCenter(center, willPresent: notification, withCompletionHandler: completionHandler)
  }

From custom plugins is even easier as they already have the bridge object and can skip the let capVC = self.window?.rootViewController as? CAPBridgeViewController part.

2reactions
rpanaderocommented, Jun 4, 2019

By the way, I have found out another issue related to the iOS notifications. The application is not notified by any Capacitor push notification callback when a notification is received while the application is in background. In addition, if the notification is “content-available” the application is not notified either in background or foreground.

In order to support this iOS feature it should be necessary to implement this method of AppDelegate:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler NS_AVAILABLE_IOS(7_0);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Handling Notifications and Notification-Related Actions
Respond to user interactions with the system's notification interfaces, including handling your app's custom actions.
Read more >
Push setup - iOS / SDK integration - Batch
First of all, open your project in Xcode by clicking on it in the sidebar, then click on the Signing & Capabilities tab....
Read more >
Preparing Your App For iOS 12 Notifications
For Customized Rich Notifications: # · Add the Notification Content extension target to your app to create rich notifications. · Design and ...
Read more >
Notifications in Xamarin.iOS - Microsoft Learn
First, iOS 8 requires applications to ask for the user's permission to display notifications. Add the following code to your app before ...
Read more >
Getting local notifications to show while app is in foreground ...
There is a delegate method to display the notification when the app is open in iOS 10. You have to implement this in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found