自然萌 發表於 2022-10-11 16:48:46

iOS推送增加右侧显示图Service Extension

<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>正文</li><li>工程配置(一)</li><li>工程配置(二)集成JPushExtension</li><li>处理推送显示的内容</li><li>注意事项</li></ul></div><p class="maodian"></p><h2>正文</h2>
<p>本Demo推送使用的是极光推送(换成其他推送改动也不大)极光文档 极光Demo</p>
<p>先看下效果图,在系统的推送弹窗右侧增加了一个图片</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202209/20221011085858087.jpg" /></p>
<p class="maodian"></p><h2>工程配置(一)</h2>
<ul><li>首先需要一个已经集成了极光推送,并且可以正常接收推送的工程(参考极光文档again);</li><li>配置主Target,如下截图所示,勾选主Target的Background Modes;</li></ul>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202209/20221011085858088.jpg" /></p>
<ul><li>创建Service Extension,看下面的三图;</li></ul>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202209/20221011085858089.jpg" /></p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202209/20221011085858090.jpg" /></p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202209/20221011085858091.jpg" /></p>
<ul><li>给创建好的PushExtension(子Target)配置Push Notifications,这一步操作就和主Target的配置推送一样;</li></ul>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202209/20221011085858092.jpg" /></p>
<p class="maodian"></p><h2>工程配置(二)集成JPushExtension</h2>
<p>这一步是按照需求可选的,引入JPushExtension的目的是为了极光推送做统计</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202209/20221011085858093.jpg" /></p>
<p class="maodian"></p><h2>处理推送显示的内容</h2>
<p>这是配置好的工程目录,多了一个<code>PushExtention</code>文件夹</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202209/20221011085858094.jpg" /></p>
<p><code>NotificationService.m</code>文件的内容改为如下</p>
<div class="jb51code"><pre class="brush:cpp;">#import "NotificationService.h"
#import "JPushNotificationExtensionService.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 = ;
    // 读取图片地址,并加载
    NSString *imgUrl = ]; // ⚠️图片字段的key值需要跟后台开发统一
    if (imgUrl) {
      NSURL *fileURL = ;
      [self downloadAndSave:fileURL handler:^(NSString *localPath) {
            if (localPath) {
                UNNotificationAttachment * attachment = options:nil error:nil];
                self.bestAttemptContent.attachments = @;
            }
            ;
      }];
    } else {
      ;
    }
}
- (void)serviceExtensionTimeWillExpire {
    self.contentHandler(self.bestAttemptContent);
}
#pragma mark - 私有方法
- (void)downloadAndSave:(NSURL *)fileURL handler:(void (^)(NSString *))handler {
    // 这里需要用系统网络请求来下载图片
    NSURLSession *session = ;
    NSURLSessionDownloadTask *task = [session downloadTaskWithURL:fileURL completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
      NSString *localPath = nil;
      if (!error) {
            // 临时文件夹路径,APP没有运行时会自动清除图片,不会占用内存
            NSString *localURL = ;
            if ([ moveItemAtPath:location.path toPath:localURL error:nil]) {
                localPath = localURL;
            }
      }
      handler(localPath);
    }];
    ;
}
- (void)apnsDeliverWith:(UNNotificationRequest *)request {
    ;
    [JPushNotificationExtensionService jpushReceiveNotificationRequest:request with:^ {
      NSLog(@"apns upload success");
      self.contentHandler(self.bestAttemptContent);
    }];
}
@end
</pre></div>
<p class="maodian"></p><h2>注意事项</h2>
<p>如果传了图片地址却还不显示,不要惊慌,先请确保图片别太大,而且可以使用NSURLSession下载,否则就会出现图片不显示的问题。</p>
<p>以上就是iOS推送增加右侧显示图Service Extension的详细内容,更多关于iOS 推送增加右侧显示图的资料请关注琼殿技术社区其它相关文章!</p>
                           
                            <div class="art_xg">
                              <b>您可能感兴趣的文章:</b><ul><li>iOS13即将到来,iOS推送DeviceToken适配方案详解</li><li>浅谈iOS推送证书生成pem文件(详细生成过程)</li><li>ios 服务器端推送证书生成的方法</li><li>如何用IOS调用WebService(SOAP接口)</li><li>IOS如何在Host App 与 App Extension 之间发送通知</li></ul>
                            </div>

                        </div>
                        <!--endmain-->
頁: [1]
查看完整版本: iOS推送增加右侧显示图Service Extension