(1)我想从推送通知中获取数据并在静态通知接口中分配给UIButton和UILabel
(2)如何处理推送通知操作按钮单击事件
如何从此方法获取数据- (void)didReceiveNotification:(UNNotification *)notification withCompletion:(void(^)(WKUserNotificationInterfaceType interface)) completionHandler
不推荐使用以下方法请不要将此方法用于解决方案
- (void)didReceiveRemoteNotification:
- (void)didReceiveLocalNotification:
- (void)handleActionWithIdentifier:
- (void)handleActionWithIdentifier:
答案 0 :(得分:0)
1。当IsEnabled
传递到设备时,iOS本身会处理通知默认界面中的数据显示。
如果您想要更改以更改通知的界面,请使用通知内容扩展。
2。当Push Notification
投放到设备后,我们会使用Push Notification
方法 - UIApplicationDelegate’s
方法获得回调。
此方法的application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
字典中收到了payload
push notification
。
userInfo
Here您可以找到有关处理func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
{
print("Push Notification Payload:\n\(userInfo)")
completionHandler(.newData)
}
的详细讨论。
答案 1 :(得分:-1)
我认为这个可以帮到你,我正在撰写本地通知
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()
}
}