IOS Rich notification didReceiveNotificationRequest is not fired
You are downloading the image in background thread which is causing the issue in Rich Push Notification case. If you want you can try with this framework
Also Add "content-available":1 in your aps
OR you can try downloading like this,
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent *))contentHandler {
self.contentHandler = contentHandler;
self.bestAttemptContent = [request.content mutableCopy];
self.bestAttemptContent.title = [NSString stringWithFormat:@"%@",request.content.title];
self.bestAttemptContent.body = [NSString stringWithFormat:@"%@",request.content.body];
NSString *attachmentUrlString = [NSString stringWithFormat:@"%@",[request.content.userInfo valueForKey:@"thumbnail_image"]];
if (![attachmentUrlString isKindOfClass:[NSString class]]) {
[self failEarly];
return;
}
NSURL *url = [NSURL URLWithString:attachmentUrlString];
if (!url) {
[self failEarly];
return;
}
NSData *data = [NSData dataWithContentsOfURL:url];
if (!data) {
[self failEarly];
return;
}
NSString *identifierName = [self getIdentifierName:attachmentUrlString];
NSString *tmpSubFolderName = [[NSProcessInfo processInfo] globallyUniqueString];
self.bestAttemptContent.attachments = [NSArray arrayWithObject:[self create:identifierName andData:data withOptions:nil andTmpFolderName:tmpSubFolderName]] ;
self.contentHandler(self.bestAttemptContent);
}
-(void)failEarly {
self.contentHandler(self.bestAttemptContent);
}
-(NSString *)getIdentifierName:(NSString *)fileURL
{
NSString *identifierName = @"image.jpg";
if (fileURL) {
identifierName = [NSString stringWithFormat:@"file.%@",fileURL.lastPathComponent];
}
return identifierName;
}
-(UNNotificationAttachment *)create:(NSString *)identifier andData:(NSData *)data withOptions:(NSDictionary *)options andTmpFolderName:(NSString *)tmpSubFolderName {
NSString *fileURLPath = NSTemporaryDirectory();
NSString *tmpSubFolderURL = [fileURLPath stringByAppendingPathComponent:tmpSubFolderName];
NSError *error;
[[NSFileManager defaultManager] createDirectoryAtPath:tmpSubFolderURL withIntermediateDirectories:TRUE attributes:nil error:&error];
if(!error) {
NSString *fileURL = [tmpSubFolderURL stringByAppendingPathComponent:identifier];
[data writeToFile:fileURL atomically:YES];
UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:identifier URL:[NSURL fileURLWithPath:fileURL] options:options error:&error];
return attachment;
}
return nil;
}
- (void)serviceExtensionTimeWillExpire {
self.contentHandler(self.bestAttemptContent);
}
UNNotificationServiceExtension's didRecieve not called
Good news! Your service extension is indeed being called - the image on your notification is evidence of that. What is probably happening here is that you are unable to debug the extension using the workflow you are used to with applications.
Debugging notification extensions is not like debugging an app. Extensions are plug-ins to an iOS process outside your application. Just setting a breakpoint is not a reliable way to debug them. Instead:
Debugging A Notification Service Extension
- Launch the app from Xcode or the device
- In Xcode, select Attach To Process or PID By Name... from the Debug menu
- Enter the name of your notification extension
- Trigger a notification (by sending a push, etc.).
When the notification is delivered the service extension should launch in to the debugger. Service extensions are only relevant to remote (push) notifications, so you will need a device to troubleshoot them.
Debugging A Notification Content Extension
There are at least two ways. The steps shown above for a service extension also work for a content extension. The second method is more familiar but less reliable.
- Select the extension scheme in Xcode using the toolbar
- In the Product menu, select Edit Scheme...
- Set the Executable to the parent application.
- Set a breakpoint inside the content extension.
- Now build and run your extension. It will launch the parent application.
- Trigger a notification that will cause the content extension to load.
It's worth noting that adding logging using the logging framework can be very useful for debugging and troubleshooting as well.
Why The Video May Not Be Playing
iOS limits the size of content that can be presented in notifications. This is described in the documentation for UNNotificationAttachment. For video it is generally 50Mb. Make sure your video is as small as you can make it in terms of bytes, and of course provide a video that is sized appropriately for the device it will be played on. Do not try to play a 1080p video in a notification that is 400 points wide!
In practice it is almost always better to use HLS instead of downloading video, and present it in a content extension.
Another thing in your code that may be problematic is the identifiers you are assigning to your attachments. Identifiers should be unique. Typically this would be a reverse-domain notation string like your bundle ID followed by a UUID string. You could also use the original URL of the content followed by a UUID string. If you provide an empty string iOS will create a unique identifier for you.
With the user notifications framework having non-unique identifiers (for notifications, attachments, etc.) tends to cause difficult to track down issues inside the framework. For example, this can cause an attached watchOS device to crash.
If you want to implement "auto play" for your video - it is not clear from your question wether that is what you are describing - you will need to implement your own player functionality in a content extension.
If you are going to do that, again, HLS is the preferred way to display video in a notification. It usually uses less RAM, offers a better user experience and tends to be more stable.
iOS10 UNNotificationServiceExtension not called
Finally I have this working correctly, and this is what I remember from this issue.
1) Do not use devices with iOS10 beta version, because one of the problems I had was because I was using a beta version.
2) only the app requres APNS entitlements, this is not required for the privisoning used for the extension.
3) I was using a provisioning profile matching the id of the extension (not wildcard), anyway I cannot confirm if it works fine or not with wildcard.
4) NSExtensionAttributes are not required, just use NSExtensionPointIdentifier and NSExtensionPrincipalClass for the extension .plist. Unless you are using your own layout
5) This is working even using iOS 9 token registration methods.
6) don't forget mutable-content value in the payload coming in the push notification, this is the only mandatory value you need from the server to go through the extension.
I think this covers all the problems I had
Related Topics
Save an Object in Nsuserdefaults and Realm
Detected a Case Where Constraints Ambiguously Suggest a Height of Zero
How to Change "Initwithnibname" in Storyboard
Refresh Certain Row of Uitableview Based on Int in Swift
Number of Threads Created by Gcd
iOS 9 Searchbar Disappears from Table Header View When Uisearchcontroller Is Active
Native Facebook Login Stopped Working After Sdk Update to 3.14
Capture Metal Mtkview as Movie in Realtime
Tableview Rounded Corners and Shadow
Self Scrollable Text Inside Cascrolllayer
How to Change the Image Displayed in a Uiimageview Programmatically
iOS App Submission and Beta Review Process
Flutter: How to Create a New Project
What's Dead & Exploded in Swift's Exception Stack
Which Tasks Are More Suitable to Nsoperation Than Gcd