简体   繁体   English

Swift:不调用didReceiveRemoteNotification()

[英]Swift: doesn't call didReceiveRemoteNotification()

Here's my sample app 这是我的示例应用

在左侧,您可以看到应用程序委托,在右侧,是我的视图控制器

I've had some trouble with this in another app and I'm trying to figure out what I'm doing wrong; 我在另一个应用程序中遇到了一些麻烦,并且试图找出我做错了什么。 It works like this: 它是这样的:

  1. I push a button that schedules the notification 我按了安排通知的按钮
  2. I quit the app and close it 我退出该应用程序并关闭它
  3. I wait for the notification to arrive 我等待通知到达
  4. I restart the app through Xcode 我通过Xcode重新启动应用程序

The thing is: even though the didReceiveRemoteNotification()-function should be called, I still get false value when starting the app which shows me, that for any reason this method does not get called. 事实是:即使应调用didReceiveRemoteNotification()函数,启动显示我的应用程序时,我仍然会得到错误的值,无论出于何种原因都不会调用此方法。 Why? 为什么? Can you help me? 你能帮助我吗?

Method didReceiveRemoteNotification() is not supposed to be called for local notifications... It's only for remote notifications (Push Notifications). 不应为本地通知调用didReceiveRemoteNotification()方法……仅用于远程通知(推送通知)。

Use UNUserNotificationCenterDelegate delegate method 使用UNUserNotificationCenterDelegate委托方法

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)

See details here https://stackoverflow.com/a/44142742/1825618 在此处查看详细信息https://stackoverflow.com/a/44142742/1825618

Get delivered notifications (Only the ones Currently being displayed in notification center) 获取传递的通知 (仅通知中心中当前显示的通知)

UNUserNotificationCenter.current().getDeliveredNotifications {
    (notifications) in
    // your code
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM