首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UNUserNotificationCenter didReceive / willPresent未触发ios 11

UNUserNotificationCenter didReceive / willPresent未触发ios 11
EN

Stack Overflow用户
提问于 2019-07-09 13:26:01
回答 1查看 702关注 0票数 1

我有一个正常工作的应用程序,它的通知显示得很好。现在,我想在通知时处理一些事件,比如当用户点击横幅时。

我的iOS部署目标是11.0,对于我的部署目标也是如此。

我实现了AppDelegate.swift文件中的所有内容:

代码语言:javascript
复制
    class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, RCTBridgeDelegate {
var window: UIWindow?
  var didFinishLaunching: Bool = false

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {

    let bridge = RCTBridge(delegate: self, launchOptions: launchOptions)
    let rootView = RCTRootView(bridge: bridge, moduleName: "root", initialProperties: nil)
    rootView?.backgroundColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1)

    self.window = UIWindow(frame: UIScreen.main.bounds)

    let rootViewController = UIViewController()
    rootViewController.view = rootView
    self.window?.rootViewController = rootViewController
    self.window?.makeKeyAndVisible()

    didFinishLaunching = true

    Fabric.with([Crashlytics.self])

    FirebaseApp.configure()


    // This block is necessary to ask user authorization to receive notification
    if #available(iOS 10.0, *) {
      let center = UNUserNotificationCenter.current()
      center.delegate = self
      center.requestAuthorization(options: [.badge, .sound, .alert], completionHandler: {(grant, error)  in
        if error == nil {
          if grant {
            print("### permission granted")
            application.registerForRemoteNotifications()
          } else {
            //User didn't grant permission
          }
        } else {
          print("error: ",error)
        }
      })
    } else {
      // Fallback on earlier versions
      let notificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
      application.registerUserNotificationSettings(notificationSettings)
    }

    self.firstLaunchAction()
    self.initTracker()

    RNSplashScreen.showSplash("LaunchScreen", inRootView: rootViewController.view)

    return true
  }



  func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let tokenParts = deviceToken.map { data -> String in
      return String(format: "%02.2hhx", data)
    }
    let token = tokenParts.joined()
    print("### Device Token: \(token)")
  }

  func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    print("### Failed to register for remote notifications with error: \(error)")
  }

  func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
            if response.actionIdentifier == "like" {
                  print("### Handle like action identifier")
              } else if response.actionIdentifier == "save" {
                  print("### Handle save action identifier")
              } else {
                  print("### No custom action identifiers chosen")
              }
            // Make sure completionHandler method is at the bottom of this func
            completionHandler()
        }

如您所见,我使用Firebase发送远程通知,因此在json中有如下所示的有效负载:

代码语言:javascript
复制
return {
        notification: {
            title,
            body
        },
        data: {
            title,
            body,
            ...data
        },
        android: {
            ttl: 3600 * 1000,
            notification: {
                icon: 'stock_ticker_update',
                color: '#002559'
            }
        },
        apns: {
            payload: {
                aps: {
                    alert: {
                        title: 'NITL ACUMEN BI RFS CODA Dev Offshore',
                        body: 'Send a Message - PN testing group'
                    },
                    sound: 'default'
                }
            }
        },
        condition
    };

我的大部分代码是从网站复制的。但我的实际行为是,当我点击通知横幅时,什么都不会发生.

我看到了噪音横幅,所以我认为我的APN注册没有问题。我不知道为什么didReceive不触发单击事件。

在我的实现过程中,我会错过什么吗?我的通知负载有问题吗?

有人能帮我找出我的错误在哪里吗?我读过无数没有结果的教程。我们会非常感谢你的帮助。)

EN

回答 1

Stack Overflow用户

发布于 2019-07-09 13:35:09

若要支持后台更新通知,请确保有效负载的aps字典包含值为1的content-available键。

代码语言:javascript
复制
{
   "aps" : {
       "content-available" : 1,
       "sound" : “default"
       ....
   },
   ....
}

当后台更新通知传递到用户的设备时,iOS会在后台唤醒您的应用程序,并给它30秒的时间运行。在iOS中,系统通过调用应用程序委托的application: didReceiveRemoteNotification: fetchCompletionHandler:方法来传递后台更新通知。

参考资料:创建远程通知负载

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56953675

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档