IOS Rich notification didReceiveNotificationRequest is not fired

与世无争的帅哥 提交于 2019-11-28 02:27:02

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);
}
Iain Huxley

The existing answer states that you should set content-available to 1, that NSLog does not work in extensions and the background thread is an issue. These are not right.

  1. content-available:1 has no relevance to a notification service extension, for that to be fired up you need mutable-content:1.

  2. NSLog works fine in extensions, it is handy to use the OS console when troubleshooting.

  3. There is no problem with downloading an image in a background thread.

Try looking in the OS Console to see what might be happening (launch console.app on your Mac, plug in your device and select it on the left, and see what goes by when you send a push. Even without your NSLog you should see the OS trying to find and launch an extension like:

default 10:38:53.925889 -0400   SpringBoard [com.you.whatever] Remote 
notification request 386D-4968 can be modified: 1
default 10:38:53.926227 -0400   SpringBoard [com.you.whatever] Trying to find extension for bundle
info    10:38:53.942366 -0400   pkd Candidate plugin count: 1, info: (
"com.you.whatever.NotificationServiceExtension(1.0.0) 2765CB-8244DD--B83D-20CB148BCEF6")
default 10:38:53.945090 -0400   SpringBoard [com.you.whatever] Extension can modify push notification request 386D-4968? YES

If you don't see success there maybe something is wrong with your project setup, maybe try recreating the target in XCode (do via File -> New -> Target, select Notification Service Extension).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!