IOS Rich уведомления didReceiveNotificationRequest не запускается - PullRequest
0 голосов
/ 20 мая 2018

Я разрабатываю приложение для iPhone с использованием target-c.Базовое push-уведомление работает правильно.Теперь я хочу добавить расширенное уведомление в свое приложение, но не могу запустить didReceiveNotificationRequest в NotificationService.

Вот полезная нагрузка уведомления, которую я получаю на Appdelegate:

Notification Payload(see mutable-content is set to 1)

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

Чего мне не хватает?

Пожалуйста, совет

Semih

Ответы [ 2 ]

0 голосов
/ 29 июня 2018

В существующем ответе говорится, что вы должны установить для content-available значение 1, что NSLog не работает в расширениях, и фоновый поток является проблемой.Они не правы.

  1. доступный контент: 1 не имеет отношения к расширению службы уведомлений, для того, чтобы его запустить, вам нужен изменяемый контент: 1.

  2. NSLog отлично работает в расширениях, удобно использовать консоль ОС при устранении неполадок.

  3. Нет проблем с загрузкой изображения в фоновом потоке.

Попробуйте посмотреть в консоли ОС, чтобы увидеть, что может происходить (запустите console.app на вашем Mac, подключите устройство и выберите его слева, и посмотрите, что произойдет, когда выЕсли вы не используете 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 (выполните через Файл -> Создать -> Цель, выберите Расширение службы уведомлений).

0 голосов
/ 25 мая 2018

Вы загружаете изображение в фоновом потоке, которое вызывает проблему в случае Rich Push Notification.Если вы хотите, вы можете попробовать с этой платформой

Также добавить "content-available": 1 в ваших aps

ИЛИ вы можете попробовать скачать, как это,

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