如何在iOS 10中收到的有效负载中设置动作按钮(即UNNotificationAction)标题

问题描述 投票:1回答:2

我正在开发iOS 10 Rich Media Push Notification。我想在通知Payload中发送UNNotificationAction按钮标题的标题,我该如何在Objective C中执行它。假设我发送通知Payload如下:

 "apns": {
    "aps": {
     "alert":{
    "title":"Pusher Native Push Notifications API",
    "subtitle":"Bringing you iOS 10 support!",
    "body":"Now add more content to your Push Notifications!"
    },
 "badge":0,
 "sound":"default",
 "mutable-content":1,
 "category":"pusher"
},
"mediaUrl":"https:\/\/framework.realtime.co\/blog\/img\/ios10-video.mp4",
"mediaType":"mp4",
"action 1":{
"btnTitle":"View",
"Url":"https://www.go_to_this_link_on_Click_This_Link"
},
"action 2":{
"btnTitle":"Send Action",
"Url":"https://www.go_to_this_link_on_Click_This_Link"
}
}

我想在push中设置动作按钮标题,如上所述。

静态UNNotificationAction标题对我有用,但我想从有效负载设置它我的静态按钮代码如下:

    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate = delegate;                    
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
      if( !error ) {
        [[UIApplication sharedApplication] registerForRemoteNotifications];
        // required to get the app to do anything at all about push notifications
        NSLog( @"Push registration success." );
      } else {
        NSLog( @"Push registration FAILED" );
        NSLog( @"ERROR: %@ - %@", error.localizedFailureReason, error.localizedDescription );
        NSLog( @"SUGGESTIONS: %@ - %@", error.localizedRecoveryOptions, error.localizedRecoverySuggestion );
                             }
                         }];



 UNNotificationAction *ActionBtn1 = [UNNotificationAction actionWithIdentifier:@"Btn1" title:@"BUTTON 1" options:UNNotificationActionOptionNone];

UNNotificationAction *ActionBtn2 = [UNNotificationAction actionWithIdentifier:@"Btn2" title:@"BUTTON 2" options:UNNotificationActionOptionDestructive];

UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"pusher" actions:@[ActionBtn1,ActionBtn1] intentIdentifiers:@[] options:UNNotificationCategoryOptionNone];

    NSSet *categories = [NSSet setWithObject:category];
    [center setNotificationCategories:categories];

谁能请帮帮我!

objective-c iphone ios10
2个回答
2
投票

我在NotificationServiceExtention实现文件中创建了UNNotificationAction,如下所示:

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {

  self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];
    NSDictionary *userInfo = request.content.userInfo;
    if (userInfo == nil) {
        [self contentComplete];
        return;
    }


/* Start of User Action Buttons */    

NSString *categoryName = userInfo[@"aps"][@"category"];
 NSString *Button1Title = userInfo[@"action 1"][@"btnTitle"];
 NSString *Button2Title = userInfo[@"action 2"][@"btnTitle"];
 NSString *Button1Id = userInfo[@"action 1"][@"Identifier"];
 NSString *Button2Id = userInfo[@"action 2"][@"Identifier"];
 UNNotificationAction *ActionBtn1 = [UNNotificationAction actionWithIdentifier:Button1Id title:Button1Title options:UNNotificationActionOptionForeground];
 UNNotificationAction *ActionBtn2 = [UNNotificationAction actionWithIdentifier:Button2Id title:Button2Title options:UNNotificationActionOptionForeground];
 UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:categoryName actions:@[ActionBtn1,ActionBtn2] intentIdentifiers:@[] options:UNNotificationCategoryOptionNone];
 NSSet *categories = [NSSet setWithObject:category];
 [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:categories];


/* End of User Action Buttons */



    NSString *mediaUrl = userInfo[@"mediaUrl"];
    NSString *mediaType = userInfo[@"mediaType"];

    if (mediaUrl == nil || mediaType == nil) {
        [self contentComplete];
        return;
    }

    // load the attachment
   [self loadAttachmentForUrlString:mediaUrl
                            withType:mediaType
                   completionHandler:^(UNNotificationAttachment *attachment) {
                       if (attachment) {
                           self.bestAttemptContent.attachments = [NSArray arrayWithObject:attachment];
                       }
                       [self contentComplete];
    }];
}


- (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 contentComplete];
}

- (void)contentComplete {
    self.contentHandler(self.bestAttemptContent);
}

- (NSString *)fileExtensionForMediaType:(NSString *)type {
    NSString *ext = type;

    if ([type isEqualToString:@"image"]) {
        ext = @"jpg";
    }

    if ([type isEqualToString:@"video"]) {
        ext = @"mp4";
    }

    if ([type isEqualToString:@"audio"]) {
        ext = @"mp3";
    }

    return [@"." stringByAppendingString:ext];
}

- (void)loadAttachmentForUrlString:(NSString *)urlString withType:(NSString *)type
                 completionHandler:(void(^)(UNNotificationAttachment *))completionHandler  {

    __block UNNotificationAttachment *attachment = nil;
    NSURL *attachmentURL = [NSURL URLWithString:urlString];
    NSString *fileExt = [self fileExtensionForMediaType:type];

    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:fileExt]];
                        [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];
}

因此,您可以根据正常推送在didReceiveNotificationResponse方法中收到的actionIdentifier处理按钮点击。


0
投票

使用以下方法进行标识符操作

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void (^)())completionHandler {
    NSDictionary *userInfo = response.notification.request.content.userInfo;


    if ([response.notification.request.identifier isEqualToString:@"Btn1"]) {

    }
    else
    {

    }

    completionHandler(UNNotificationPresentationOptionBadge + UNNotificationPresentationOptionSound);
}
© www.soinside.com 2019 - 2024. All rights reserved.