【问题标题】:How to get data from Push Notification in WatchOs and handle push notifications action button click event如何从 WatchOs 中的推送通知中获取数据并处理推送通知操作按钮单击事件
【发布时间】: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:

【问题讨论】:

    标签: objective-c apple-push-notifications watchkit apple-watch


    【解决方案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 推送通知
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-16
    • 1970-01-01
    • 2012-06-16
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多