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

Posted

技术标签:

【中文标题】如何从 WatchOs 中的推送通知中获取数据并处理推送通知操作按钮单击事件【英文标题】:How to get data from Push Notification in WatchOs and handle push notifications action button click event 【发布时间】:2018-06-18 05:31:27 【问题描述】:

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

(2)如何处理Push Notification动作按钮点击事件

如何从这个方法- (void)didReceiveNotification:(UNNotification *)notification withCompletion:(void(^)(WKUserNotificationInterfaceType interface)) completionHandler获取数据

不推荐使用以下方法请不要将此方法用于解决方案

- (void)didReceiveRemoteNotification:
- (void)didReceiveLocalNotification:
- (void)handleActionWithIdentifier:
- (void)handleActionWithIdentifier:

【问题讨论】:

【参考方案1】:

1.Push Notification被传递到设备时,ios自己处理通知默认界面中数据的显示。

如果您想更改通知界面,请使用通知内容扩展

2.Push Notification被传递到设备时,我们在UIApplicationDelegate’s方法——application(_:didReceiveRemoteNotification:fetchCompletionHandler:)方法中得到回调。

push notificationpayload 在此方法的userInfo 字典中接收。

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)

    print("Push Notification Payload:\n\(userInfo)")
    completionHandler(.newData)

Here你可以找到处理Push Notifications的详细讨论。

【讨论】:

"aps": "alert": "body": "Test message123", "title": "Optional123 title" , "category": "myCategory", "thread-id ":"5280" , "WatchKit Simulator Actions": [ "title": "FB", "identifier": "firstButtonAction" ], "Amount": "USD 20", "At": "Mc Donalds" , "时间": "今天", 我想获取“金额”和“时间”和“时间”值 朋友,我在谈论 Watchkit 推送通知 我不是在谈论通知服务扩展我在谈论 WatchKit 推送通知【参考方案2】:

我认为这个对你有帮助,我正在写本地通知

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()
    


【讨论】:

我不是在谈论通知服务扩展我在谈论 WatchKit 推送通知

以上是关于如何从 WatchOs 中的推送通知中获取数据并处理推送通知操作按钮单击事件的主要内容,如果未能解决你的问题,请参考以下文章

watchOS,不接收远程通知

如何从 WatchOS 3 中的自定义通知操作中打开特定的视图控制器

WatchOS 3 在收到推送通知时崩溃

如何从fireBase推送通知swift 4中获取数据

iWatch开发:WatchOS 消息推送教程

Apple WatchOs 以及用于推送通知的 Firebase 云消息传递