Objective c 如何从WatchOs中的推送通知获取数据并处理推送通知操作按钮单击事件

Objective c 如何从WatchOs中的推送通知获取数据并处理推送通知操作按钮单击事件,objective-c,apple-push-notifications,watchkit,apple-watch,Objective C,Apple Push Notifications,Watchkit,Apple Watch,1我想从推送通知中获取数据,并在静态通知界面中分配给UIButton和UILabel 2如何处理推送通知操作按钮单击事件 如何从此方法获取数据-voiddidReceiveNotification:UNNotification*notification with Completion:void^WKUserNotificationInterfaceType接口completionHandler 以下方法已弃用。请不要将此方法用于解决方案 - (void)didReceiveRemoteNotif

1我想从推送通知中获取数据,并在静态通知界面中分配给UIButton和UILabel

2如何处理推送通知操作按钮单击事件

如何从此方法获取数据-voiddidReceiveNotification:UNNotification*notification with Completion:void^WKUserNotificationInterfaceType接口completionHandler

以下方法已弃用。请不要将此方法用于解决方案

- (void)didReceiveRemoteNotification:
- (void)didReceiveLocalNotification:
- (void)handleActionWithIdentifier:
- (void)handleActionWithIdentifier:
一,。当推送通知发送到设备时,iOS本身处理通知默认界面中的数据显示

如果要更改通知的接口,请使用通知内容扩展

二,。当推送通知传递到设备时,我们将在UIApplicationLegate的方法-application:DidReceiveMemotentification:fetchCompletionHandler:method中获得回调

推送通知的有效负载在该方法的userInfo字典中接收

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
{
    print("Push Notification Payload:\n\(userInfo)")
    completionHandler(.newData)
}

您可以找到有关处理推送通知的详细讨论。

我认为这一部分对您有所帮助,我正在写本地通知

import UIKit
import UserNotifications

class ViewController: UIViewController{

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        // Toget the Permissions
        self.initNotificationSetUpCheck()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBAction func LocalNotificationClick(_ sender: UIButton) {
          UNUserNotificationCenter.current().delegate = self
        //Notifdication Conntent Body
        let localNotification = UNMutableNotificationContent()
        localNotification.title = "Rize"
        localNotification.subtitle = "Ios Developers"
        localNotification.body = "Welcome"

        //Notification Triggring
        let notificationTriger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats:false)
        // Notification Request
        let request = UNNotificationRequest(identifier: "Notification1", content: localNotification, trigger: notificationTriger)
        //Ad Notification Request
        UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

    }

    func initNotificationSetUpCheck(){
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert], completionHandler:{(success,error) in
            if success {
               print("Permission Granted!")
            }else{
              print("There was a problem!")
            }
        })

    }

}

extension ViewController: UNUserNotificationCenterDelegate {

    //for displaying notification when app is in foreground
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

        //If you don't want to show notification when app is open, do something here else and make a return here.
        //Even you you don't implement this delegate method, you will not see the notification on the specified controller. So, you have to implement this delegate and make sure the below line execute. i.e. completionHandler.

        completionHandler([.alert, .badge, .sound])
    }


    // For handling tap and user actions
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

        switch response.actionIdentifier {
        case "Notification1":
            print("Action First Tapped")
        case "action2":
            print("Action Second Tapped")
        default:
            break
        }
        completionHandler()
    }

}

{aps:{alert:{body:Test message123,title:Optional123 title},category:myCategory,thread id:5280},WatchKit模拟器操作:[{title:FB,标识符:firstButtonAction}],金额:20美元,地址:Mc Donalds,时间:今天,}我想获取金额、时间和价值朋友我说的是Watchkit推送通知我不是说通知服务扩展我是说Watchkit推送通知我不是说通知服务扩展我是说Watchkit推送通知