【问题标题】:IOS Rich notification didReceiveNotificationRequest is not firedIOS Rich 通知 didReceiveNotificationRequest 未触发
【发布时间】:2018-10-30 09:33:11
【问题描述】:

我正在使用objective-c 开发一个iPhone 应用程序。基本推送通知工作正常。现在我想在我的应用中添加丰富的通知,但我无法在 NotificationService 中触发 didReceiveNotificationRequest。

这是我在 Appdelegate 上收到的通知负载:

https://image.ibb.co/ndA2Qo/grab.png

这里是 NotificationService.m 文件;

#import "NotificationService.h"
#import "Common.h"

@interface NotificationService ()

@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;

@end

@implementation NotificationService

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
self.contentHandler = contentHandler;
self.bestAttemptContent = [request.content mutableCopy];
NSLog(@"didReceiveNotificationRequest");
// Modify the notification content here...
// load the attachment
NSDictionary *userInfo = request.content.userInfo;
NSString *imageURL = [userInfo valueForKey:@"thumbnail_image"];
NSArray *parts = [imageURL componentsSeparatedByString:@"."];
NSString *extension = [parts lastObject];
[self loadAttachmentForUrlString:imageURL
                        withExtension:extension
               completionHandler:^(UNNotificationAttachment *attachment) {
                   if (attachment) {
                       self.bestAttemptContent.attachments = [NSArray arrayWithObject:attachment];
                   }
                   //self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title];

                   self.contentHandler(self.bestAttemptContent);
               }];



}

- (void)serviceExtensionTimeWillExpire {
// Called just before the extension will be terminated by the system.
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
self.contentHandler(self.bestAttemptContent);
}

- (void)loadAttachmentForUrlString:(NSString *)urlString withExtension:(NSString *)extension completionHandler:(void(^)(UNNotificationAttachment *))completionHandler  {

__block UNNotificationAttachment *attachment = nil;
NSURL *attachmentURL = [NSURL URLWithString:urlString];

NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session downloadTaskWithURL:attachmentURL
            completionHandler:^(NSURL *temporaryFileLocation, NSURLResponse *response, NSError *error) {
                if (error != nil) {
                    NSLog(@"%@", error.localizedDescription);
                } else {
                    NSFileManager *fileManager = [NSFileManager defaultManager];
                    NSURL *localURL = [NSURL fileURLWithPath:[temporaryFileLocation.path stringByAppendingString:extension]];
                    [fileManager moveItemAtURL:temporaryFileLocation toURL:localURL error:&error];

                    NSError *attachmentError = nil;
                    attachment = [UNNotificationAttachment attachmentWithIdentifier:@"" URL:localURL options:nil error:&attachmentError];
                    if (attachmentError) {
                        NSLog(@"%@", attachmentError.localizedDescription);
                    }
                }
                completionHandler(attachment);
            }] resume];
}

@end

我错过了什么?

请指教,

半成品

【问题讨论】:

    标签: ios objective-c apple-push-notifications rich-notifications


    【解决方案1】:

    现有答案指出您应该将 content-available 设置为 1,NSLog 在扩展中不起作用,并且后台线程是一个问题。这些都不对。

    1. content-available:1 与通知服务扩展无关,要启动它,您需要 mutable-content:1。

    2. NSLog 在扩展中运行良好,故障排除时使用 OS 控制台很方便。

    3. 后台线程下载图片没有问题。

    尝试在 OS 控制台中查看可能发生的情况(在 Mac 上启动 console.app,插入设备并在左侧选择它,然后查看发送推送时发生的情况。即使没有 NSLog您应该会看到操作系统正在尝试查找并启动一个扩展,例如:

    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
    

    如果您没有看到成功,则可能是您的项目设置有问题,可以尝试在 XCode 中重新创建目标(通过文件 -> 新建 -> 目标,选择通知服务扩展)。

    【讨论】:

      【解决方案2】:

      您正在后台线程中下载图像,这导致富推送通知案例中的问题。如果你愿意,你可以试试this framework

      还在您的aps

      中添加"content-available":1

      或者你可以试试这样下载,

      - (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);
      }
      

      【讨论】:

      • 感谢您的回复,我会尝试并回复您。但是,我仍然想知道为什么 NSLog(@"didReceiveNotificationRequest") 没有在日志窗口上记录任何内容。如果 didReceiveNotificationRequest 被击中,至少它应该记录,对吗?
      • NSLog 不适用于扩展。如果您想调试您的扩展代码,请将 pid 附加到您的应用程序中,您也可以借助 Extension 中的断点对其进行测试。
      • 我明白了,不知道谢谢!一旦我测试你的代码,我就会把它作为答案。
      • 如果您遇到任何其他问题,请务必告诉我。
      • 欢迎。乐于助人:)
      猜你喜欢
      • 1970-01-01
      • 2019-03-09
      • 2014-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-04
      • 1970-01-01
      相关资源
      最近更新 更多