Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 为什么';当我收到远程通知时,是否会发送本地通知?_Swift_Push Notification_Swift3_Firebase Cloud Messaging_Localnotification - Fatal编程技术网

Swift 为什么';当我收到远程通知时,是否会发送本地通知?

Swift 为什么';当我收到远程通知时,是否会发送本地通知?,swift,push-notification,swift3,firebase-cloud-messaging,localnotification,Swift,Push Notification,Swift3,Firebase Cloud Messaging,Localnotification,我试图在收到远程FCM通知时发送位置通知,因为据我所知FCM通知不支持操作按钮。但是本地通知只是偶尔发送,在我看来,它是随机的。我总是收到远程通知。我想在所有三种状态下获得本地通知,前台、后台或应用程序被终止时 下面是我在didFinishLoadingWithOptions中添加按钮的地方: func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIAppli

我试图在收到远程FCM通知时发送位置通知,因为据我所知FCM通知不支持操作按钮。但是本地通知只是偶尔发送,在我看来,它是随机的。我总是收到远程通知。我想在所有三种状态下获得本地通知,前台、后台或应用程序被终止时

下面是我在didFinishLoadingWithOptions中添加按钮的地方:

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


  let incrementAction = UIMutableUserNotificationAction()
    incrementAction.identifier = "First"
    incrementAction.title = "First"
    incrementAction.activationMode = UIUserNotificationActivationMode.foreground
    incrementAction.isAuthenticationRequired = true
    incrementAction.isDestructive = true


    let decrementAction = UIMutableUserNotificationAction()
    decrementAction.identifier = "second"
    decrementAction.title = "second"
    decrementAction.activationMode = UIUserNotificationActivationMode.foreground
    decrementAction.isAuthenticationRequired = true
    decrementAction.isDestructive = false

    // Category
    let counterCategory = UIMutableUserNotificationCategory()
    counterCategory.identifier = "BOOKING_CATEGORY"

    // A. Set actions for the default context
    counterCategory.setActions([incrementAction, decrementAction],
                               for: UIUserNotificationActionContext.default)

    // B. Set actions for the minimal context //No more than 2
    counterCategory.setActions([incrementAction, decrementAction],
                               for: UIUserNotificationActionContext.minimal)

    // iOS 9 support
else if #available(iOS 9, *) {

    UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: NSSet(object: counterCategory) as? Set<UIUserNotificationCategory>))
    UIApplication.shared.registerForRemoteNotifications()
}

}

任何帮助都将不胜感激

为了确保您的应用程序始终处理通知的显示方式(即使通知未运行或在后台),您需要仅设置
数据
有效负载,例如

{
  "to":"push_token",
    "content_available": true,
    "priority": "high",
    "data": {
      "title": "New Message",
      "message": "Bob sent you a message",
    }
}
设置
通知
有效负载将触发FCM以代表客户端应用程序自动向最终用户设备显示消息。如果使用数据有效负载,则可以完全控制如何显示通知


有关更多信息,请查看

为了确保您的应用程序始终能够处理通知的显示方式(即使通知未运行或在后台),您需要仅设置
数据
有效负载,例如

{
  "to":"push_token",
    "content_available": true,
    "priority": "high",
    "data": {
      "title": "New Message",
      "message": "Bob sent you a message",
    }
}
设置
通知
有效负载将触发FCM以代表客户端应用程序自动向最终用户设备显示消息。如果使用数据有效负载,则可以完全控制如何显示通知


有关更多信息,请查看通知的有效负载是什么样子的?您是否有通知有效负载、数据有效负载或两者都有?感谢您的回复,我都有,我实现了在app delegate类中接收本地通知的函数,它似乎每次都会被调用。因此,我的问题是,当应用程序被终止或在后台时,如何安排本地通知,并且我收到远程通知,因为当应用程序被终止时,将不会调用应用程序委托函数!通知的有效负载是什么样子的?您是否有通知有效负载、数据有效负载或两者都有?感谢您的回复,我都有,我实现了在app delegate类中接收本地通知的函数,它似乎每次都会被调用。因此,我的问题是,当应用程序被终止或在后台时,如何安排本地通知,并且我收到远程通知,因为当应用程序被终止时,将不会调用应用程序委托函数!我想使用本地通知的原因是FCM还不支持通知上的操作按钮,他们建议使用本地通知。我也会在每次启动
DidReceiveMemotentification
时创建自己的本地通知,但只有这样,当应用程序未运行时,我才能让它每次启动,在后台/前台仅传递数据有效载荷。添加
通知
告知FCM处理通知。你试过数据负载吗?嗯。。。好的一点,我会尝试,虽然,我现在放弃了这个,因为它不是一个真正重要的功能,但我会给这另一个镜头,感谢评论它花了我一段时间让它正常工作。他们的文档中没有100%清楚,但可以为3个主要应用程序状态中的每一个发出您自己的本地通知。好的,我会在找到时间后再试一次,稍后再讨论。。。干杯我想使用本地通知的原因是FCM还不支持通知上的操作按钮,他们建议使用本地通知。我也会在每次启动
DidReceiveMemotentification
时创建自己的本地通知,但只有这样,当应用程序未运行时,我才能让它每次启动,在后台/前台仅传递数据有效载荷。添加
通知
告知FCM处理通知。你试过数据负载吗?嗯。。。好的一点,我会尝试,虽然,我现在放弃了这个,因为它不是一个真正重要的功能,但我会给这另一个镜头,感谢评论它花了我一段时间让它正常工作。他们的文档中没有100%清楚,但可以为3个主要应用程序状态中的每一个发出您自己的本地通知。好的,我会在找到时间后再试一次,稍后再讨论。。。干杯