-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add notification service extension to MessagingExampleSwift. #1774
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
base: main
Are you sure you want to change the base?
Conversation
This is to make the app support image and FCM BigQuery export. https://firebase.google.com/docs/cloud-messaging/ios/send-image https://firebase.google.com/docs/cloud-messaging/understand-delivery?platform=ios#bigquery-data-export
Summary of ChangesHello @leojaygoogle, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the MessagingExampleSwift application by incorporating a Notification Service Extension. This addition allows the app to leverage advanced push notification features, specifically enabling the display of rich media like images directly within notifications. Furthermore, it sets up the necessary infrastructure to automatically export FCM delivery data to Google BigQuery, providing comprehensive analytics on message delivery and engagement. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request successfully adds a Notification Service Extension to the MessagingExampleSwift
project, enabling support for rich notifications with images and exporting delivery metrics to BigQuery. The implementation is solid, but I have a couple of suggestions to improve the code's robustness and remove a redundant call. My feedback focuses on making the new NotificationService
more resilient to potential failures and cleaning up the AppDelegate
.
print("Call exportDeliveryMetricsToBigQuery() from AppDelegate") | ||
Messaging.serviceExtension().exportDeliveryMetricsToBigQuery(withMessageInfo: userInfo) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This call to exportDeliveryMetricsToBigQuery(withMessageInfo:)
appears to be redundant. Since you've implemented a NotificationServiceExtension
which also calls this method, this one in the AppDelegate
is not needed and should be removed. According to the Firebase documentation, this method should be called from the service extension if one exists; otherwise, it should be called from the AppDelegate
. Having it in both places could lead to duplicated metrics.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.contentHandler = contentHandler | ||
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) | ||
|
||
if let bestAttemptContent = bestAttemptContent { | ||
// Modify the notification content here... | ||
bestAttemptContent.title = "\(bestAttemptContent.title) [modified]" | ||
|
||
// Log Delivery signals and export to BigQuery. | ||
print("Call exportDeliveryMetricsToBigQuery() from NotificationService") | ||
Messaging.serviceExtension() | ||
.exportDeliveryMetricsToBigQuery(withMessageInfo: request.content.userInfo) | ||
|
||
// Add image, call this last to finish with the content handler. | ||
Messaging.serviceExtension() | ||
.populateNotificationContent(bestAttemptContent, withContentHandler: contentHandler) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementation in didReceive
can be made more robust. If request.content.mutableCopy()
fails and returns nil
, the current code implicitly relies on a timeout to deliver the original notification. It's better practice to handle this case explicitly. Using a guard let
statement to safely unwrap the mutable content and calling the contentHandler
with the original content in the else
block would make the code more robust and easier to read.
self.contentHandler = contentHandler | |
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) | |
if let bestAttemptContent = bestAttemptContent { | |
// Modify the notification content here... | |
bestAttemptContent.title = "\(bestAttemptContent.title) [modified]" | |
// Log Delivery signals and export to BigQuery. | |
print("Call exportDeliveryMetricsToBigQuery() from NotificationService") | |
Messaging.serviceExtension() | |
.exportDeliveryMetricsToBigQuery(withMessageInfo: request.content.userInfo) | |
// Add image, call this last to finish with the content handler. | |
Messaging.serviceExtension() | |
.populateNotificationContent(bestAttemptContent, withContentHandler: contentHandler) | |
} | |
self.contentHandler = contentHandler | |
guard let bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) else { | |
// If we're unable to create a mutable copy, deliver the original content. | |
contentHandler(request.content) | |
return | |
} | |
self.bestAttemptContent = bestAttemptContent | |
// Modify the notification content here... | |
bestAttemptContent.title = "\(bestAttemptContent.title) [modified]" | |
// Log Delivery signals and export to BigQuery. | |
print("Call exportDeliveryMetricsToBigQuery() from NotificationService") | |
Messaging.serviceExtension() | |
.exportDeliveryMetricsToBigQuery(withMessageInfo: request.content.userInfo) | |
// Add image, call this last to finish with the content handler. | |
Messaging.serviceExtension() | |
.populateNotificationContent(bestAttemptContent, withContentHandler: contentHandler) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's very unlikely that request.content.mutableCopy()
returns nil
. I'd like to keep the example app simple.
This is to make the app support images in push notifications and FCM BigQuery export.
https://firebase.google.com/docs/cloud-messaging/ios/send-image
https://firebase.google.com/docs/cloud-messaging/understand-delivery?platform=ios#bigquery-data-export