簡體   English   中英

如何處理推送通知中的操作按鈕

[英]How to Handle Action Buttons in Push Notifications

處理 Apple Watch 通知:-所以如果我在通知界面中添加按鈕(來自對象 Lib),那么錯誤是:

通知界面不支持按鈕

PushNotificationPayload.apns具有像這樣的WatchKit Simulator Actions

"WatchKit Simulator Actions": 
[
    {
        "title": "View",
        "identifier": "firstButtonAction"
    }
],

模擬器向我展示了這個

在此處輸入圖像描述

現在我的問題是,從服務器發送PushNotification時如何處理此View按鈕,

如果aps文件包含操作按鈕是 Apple Watch 的唯一選項,

如何使用指定的密鑰從通知字典中的服務器發送它?

如何更改動作按鈕的背景顏色?

誰能給我包含ActionButton for Device the Apple Watch not for the Simulator的示例aps文件

我只是通過將WatchKit Simulator Actions Key 更改為WatchKit Actions Actions Key 進行測試,但它沒有顯示 Action Button。

正如@vivekDas 在答案中所建議的那樣,我通過在aps中替換為:

"alert" : {
     "body" : "Acme message received from Johnny Appleseed",
     "action-loc-key" : "VIEW",
     "action" : [
        {
           "id" : "buttonTapped",
           "title" : "View"
        }
     ]
  }

但 Xcode 中的模擬器確實顯示了一個操作按鈕。

我認為這可能會在 Device Apple Watch上運行,這是……?

對此你有什么建議。

我已經為此苦苦掙扎了兩個小時,所以這就是我通過真正的 APNS Serv 為生產中的通知按鈕所做的事情,

1) 在您的 appDelegate 的父應用中注冊該類別:

- (void)registerSettingsAndCategories {
    // Create a mutable set to store the category definitions.
    NSMutableSet* categories = [NSMutableSet set];

    // Define the actions for a meeting invite notification.
    UIMutableUserNotificationAction* acceptAction = [[UIMutableUserNotificationAction alloc] init];
    acceptAction.title = NSLocalizedString(@"Repondre", @"Repondre commentaire");
    acceptAction.identifier = @"respond";
    acceptAction.activationMode = UIUserNotificationActivationModeForeground; //UIUserNotificationActivationModeBackground if no need in foreground.
    acceptAction.authenticationRequired = NO;

    // Create the category object and add it to the set.
    UIMutableUserNotificationCategory* inviteCategory = [[UIMutableUserNotificationCategory alloc] init];
    [inviteCategory setActions:@[acceptAction]
                    forContext:UIUserNotificationActionContextDefault];
    inviteCategory.identifier = @"respond";

    [categories addObject:inviteCategory];

    // Configure other actions and categories and add them to the set...

    UIUserNotificationSettings* settings = [UIUserNotificationSettings settingsForTypes:
                                            (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound)
                                                                             categories:categories];

    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}

2)從您的 Apns 服務器添加類別(對我來說是“響應”)

{"aps":{"alert":"bla","category":"respond","badge":2}}

3) 在您的 WatchKitExtention 中,您有傳入的數據:

- (void)handleActionWithIdentifier:(NSString *)identifier  forRemoteNotification:(NSDictionary *)remoteNotification{

     if ([identifier isEqualToString:@"respond"]) {
//Do stuff Here to handle action... 
     }
}

4) 在您的父應用程序的 appDelegate 中:

- (void) application:(UIApplication *)application
handleActionWithIdentifier:(NSString *)identifier
     forRemoteNotification:(NSDictionary *)userInfo
         completionHandler:(void (^)())completionHandler {
    completionHandler();
}

警告 ! 您還必須在您的父應用程序中處理此操作(因為當您平移通知時,響應按鈕也會在 iphone 上可見。在:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
func userNotificationCenter(_ center: UNUserNotificationCenter,
       didReceive response: UNNotificationResponse,
       withCompletionHandler completionHandler: 
         @escaping () -> Void) {
       
   // Get the meeting ID from the original notification.
   let userInfo = response.notification.request.content.userInfo
   let meetingID = userInfo["MEETING_ID"] as! String
   let userID = userInfo["USER_ID"] as! String
        
   // Perform the task associated with the action.
   switch response.actionIdentifier {
   case "ACCEPT_ACTION":
      sharedMeetingManager.acceptMeeting(user: userID, 
                                    meetingID: meetingID)
      break
        
   case "DECLINE_ACTION":
      sharedMeetingManager.declineMeeting(user: userID, 
                                     meetingID: meetingID)
      break
        
   // Handle other actions…
 
   default:
      break
   }
    
   // Always call the completion handler when done.    
   completionHandler()
}

使用 Json Payload,如下所示

{
    "aps": {
         "badge": 10,
         "alert": "Your message",
         "sound": "cat.caf"
    }
}

檢查這個蘋果文檔鏈接https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html

通知負載部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM