-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add image on notification support #2491
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
176a4af
adding image on notification support
charlotteliang 8a6748c
add new test to target
charlotteliang 080dede
mark API and functions iOS only and iOS 10+
charlotteliang 5e1869e
also move the FIRMessagingExtensionHelper to public folder because it…
charlotteliang e1242b7
also add the new public header to FirebaseMessaging.h
charlotteliang 141077c
provide more information on the error message
charlotteliang 662f8cb
minor touch
charlotteliang 61c83ae
mark the whole extension class iOS 10+ since the notification extenti…
charlotteliang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
Example/Messaging/Tests/FIRMessagingExtensionHelperTest.m
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright 2019 Google | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#import <UIKit/UIKit.h> | ||
#import <XCTest/XCTest.h> | ||
|
||
#import <OCMock/OCMock.h> | ||
|
||
#import "FIRMessaging.h" | ||
#import "FIRMessagingExtensionHelper.h" | ||
|
||
typedef void (^FIRMessagingContentHandler)(UNNotificationContent *content); | ||
|
||
static NSString *const kFCMPayloadOptionsName = @"fcm_options"; | ||
static NSString *const kFCMPayloadOptionsImageURLName = @"image"; | ||
static NSString *const kValidImageURL = | ||
@"https://firebasestorage.googleapis.com/v0/b/fcm-ios-f7f9c.appspot.com/o/" | ||
@"chubbyBunny.jpg?alt=media&token=d6c56a57-c007-4b27-b20f-f267cc83e9e5"; | ||
|
||
@interface FIRMessagingExtensionHelper (ExposedForTest) | ||
#if TARGET_OS_IOS && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 | ||
- (void)loadAttachmentForURL:(NSURL *)attachmentURL | ||
completionHandler:(void (^)(UNNotificationAttachment *))completionHandler; | ||
#endif | ||
@end | ||
|
||
@interface FIRMessagingExtensionHelperTest : XCTestCase { | ||
id _mockExtensionHelper; | ||
} | ||
|
||
@end | ||
|
||
@implementation FIRMessagingExtensionHelperTest | ||
|
||
- (void)setUp { | ||
[super setUp]; | ||
FIRMessagingExtensionHelper *extensionHelper = [FIRMessaging extensionHelper]; | ||
_mockExtensionHelper = OCMPartialMock(extensionHelper); | ||
} | ||
|
||
- (void)tearDown { | ||
[_mockExtensionHelper stopMocking]; | ||
} | ||
|
||
#if TARGET_OS_IOS && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 | ||
- (void)testModifyNotificationWithValidPayloadData { | ||
XCTestExpectation *validPayloadExpectation = | ||
[self expectationWithDescription:@"Test payload is valid."]; | ||
|
||
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init]; | ||
content.userInfo = @{kFCMPayloadOptionsName : @{kFCMPayloadOptionsImageURLName : kValidImageURL}}; | ||
FIRMessagingContentHandler handler = ^(UNNotificationContent *content) { | ||
[validPayloadExpectation fulfill]; | ||
}; | ||
[_mockExtensionHelper populateNotificationContent:content withContentHandler:handler]; | ||
|
||
OCMVerify([_mockExtensionHelper loadAttachmentForURL:[OCMArg any] | ||
completionHandler:[OCMArg any]]); | ||
[self waitForExpectationsWithTimeout:1.0 handler:nil]; | ||
} | ||
|
||
- (void)testModifyNotificationWithInvalidPayloadData { | ||
XCTestExpectation *validPayloadExpectation = | ||
[self expectationWithDescription:@"Test payload is valid."]; | ||
|
||
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init]; | ||
content.userInfo = | ||
@{kFCMPayloadOptionsName : @{kFCMPayloadOptionsImageURLName : @"a invalid URL"}}; | ||
FIRMessagingContentHandler handler = ^(UNNotificationContent *content) { | ||
[validPayloadExpectation fulfill]; | ||
}; | ||
[_mockExtensionHelper populateNotificationContent:content withContentHandler:handler]; | ||
|
||
OCMReject([_mockExtensionHelper loadAttachmentForURL:[OCMArg any] | ||
completionHandler:[OCMArg any]]); | ||
[self waitForExpectationsWithTimeout:1.0 handler:nil]; | ||
} | ||
|
||
- (void)testModifyNotificationWithEmptyPayloadData { | ||
XCTestExpectation *validPayloadExpectation = | ||
[self expectationWithDescription:@"Test payload is valid."]; | ||
|
||
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init]; | ||
content.userInfo = | ||
@{kFCMPayloadOptionsName : @{kFCMPayloadOptionsImageURLName : @"a invalid URL"}}; | ||
FIRMessagingContentHandler handler = ^(UNNotificationContent *content) { | ||
[validPayloadExpectation fulfill]; | ||
}; | ||
[_mockExtensionHelper populateNotificationContent:content withContentHandler:handler]; | ||
|
||
OCMReject([_mockExtensionHelper loadAttachmentForURL:[OCMArg any] | ||
completionHandler:[OCMArg any]]); | ||
[self waitForExpectationsWithTimeout:1.0 handler:nil]; | ||
} | ||
#endif | ||
|
||
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Copyright 2019 Google | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#import "FIRMessagingExtensionHelper.h" | ||
|
||
#import "FIRMMessageCode.h" | ||
#import "FIRMessagingLogger.h" | ||
|
||
static NSString *const kPayloadOptionsName = @"fcm_options"; | ||
static NSString *const kPayloadOptionsImageURLName = @"image"; | ||
|
||
@interface FIRMessagingExtensionHelper () | ||
@property(nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver); | ||
@property(nonatomic, strong) UNMutableNotificationContent *bestAttemptContent; | ||
|
||
@end | ||
|
||
@implementation FIRMessagingExtensionHelper | ||
|
||
- (void)populateNotificationContent:(UNMutableNotificationContent *)content | ||
withContentHandler:(void (^)(UNNotificationContent *_Nonnull))contentHandler { | ||
self.contentHandler = [contentHandler copy]; | ||
self.bestAttemptContent = content; | ||
|
||
NSString *currentImageURL = content.userInfo[kPayloadOptionsName][kPayloadOptionsImageURLName]; | ||
if (!currentImageURL) { | ||
[self deliverNotification]; | ||
return; | ||
} | ||
#if TARGET_OS_IOS | ||
NSURL *attachmentURL = [NSURL URLWithString:currentImageURL]; | ||
if (attachmentURL) { | ||
[self loadAttachmentForURL:attachmentURL | ||
completionHandler:^(UNNotificationAttachment *attachment) { | ||
self.bestAttemptContent.attachments = @[ attachment ]; | ||
[self deliverNotification]; | ||
}]; | ||
} else { | ||
FIRMessagingLoggerError(kFIRMessagingServiceExtensionImageInvalidURL, | ||
@"The Image URL provided is invalid %@.", currentImageURL); | ||
[self deliverNotification]; | ||
} | ||
#else | ||
[self deliverNotification]; | ||
#endif | ||
} | ||
|
||
#if TARGET_OS_IOS | ||
- (void)loadAttachmentForURL:(NSURL *)attachmentURL | ||
completionHandler:(void (^)(UNNotificationAttachment *))completionHandler { | ||
__block UNNotificationAttachment *attachment = nil; | ||
|
||
NSURLSession *session = [NSURLSession | ||
sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; | ||
[[session | ||
downloadTaskWithURL:attachmentURL | ||
completionHandler:^(NSURL *temporaryFileLocation, NSURLResponse *response, NSError *error) { | ||
if (error != nil) { | ||
FIRMessagingLoggerError(kFIRMessagingServiceExtensionImageNotDownloaded, | ||
@"Failed to download image given URL %@, error: %@\n", | ||
attachmentURL, error); | ||
completionHandler(attachment); | ||
return; | ||
} | ||
|
||
NSFileManager *fileManager = [NSFileManager defaultManager]; | ||
NSString *fileExtension = | ||
[NSString stringWithFormat:@".%@", [response.suggestedFilename pathExtension]]; | ||
NSURL *localURL = [NSURL | ||
fileURLWithPath:[temporaryFileLocation.path stringByAppendingString:fileExtension]]; | ||
[fileManager moveItemAtURL:temporaryFileLocation toURL:localURL error:&error]; | ||
if (error) { | ||
FIRMessagingLoggerError( | ||
kFIRMessagingServiceExtensionLocalFileNotCreated, | ||
@"Failed to move the image file to local location: %@, error: %@\n", localURL, | ||
error); | ||
completionHandler(attachment); | ||
return; | ||
} | ||
|
||
attachment = [UNNotificationAttachment attachmentWithIdentifier:@"" | ||
URL:localURL | ||
options:nil | ||
error:&error]; | ||
if (error) { | ||
FIRMessagingLoggerError(kFIRMessagingServiceExtensionImageNotAttached, | ||
@"Failed to create attachment with URL %@, error: %@\n", | ||
localURL, error); | ||
completionHandler(attachment); | ||
return; | ||
} | ||
completionHandler(attachment); | ||
}] resume]; | ||
} | ||
#endif | ||
|
||
- (void)deliverNotification { | ||
if (self.contentHandler) { | ||
self.contentHandler(self.bestAttemptContent); | ||
} | ||
} | ||
|
||
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright 2019 Google | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 | ||
#import <UserNotifications/UserNotifications.h> | ||
#endif | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
__IOS_AVAILABLE(10.0) | ||
@interface FIRMessagingExtensionHelper : NSObject | ||
|
||
/// Call this API to complete your notification content modification. If you like to | ||
/// overwrite some properties of the content instead of using the default payload, | ||
/// make sure to make your customized motification to the content before passing it to | ||
/// this call. | ||
- (void)populateNotificationContent:(UNMutableNotificationContent *)content | ||
withContentHandler:(void (^)(UNNotificationContent *_Nonnull))contentHandler; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ | |
*/ | ||
|
||
#import "FIRMessaging.h" | ||
#import "FIRMessagingExtensionHelper.h" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.