Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.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
iOS未使用apn节点接收后台推送通知_Ios_Node.js_Swift_Apple Push Notifications - Fatal编程技术网

iOS未使用apn节点接收后台推送通知

iOS未使用apn节点接收后台推送通知,ios,node.js,swift,apple-push-notifications,Ios,Node.js,Swift,Apple Push Notifications,我的设备未接收后台推送通知。我束手无策,我已经尝试了我能找到的所有教程,并通过堆栈溢出进行了搜索 设备和APN设置为调试/开发 我可以成功注册设备令牌 我有一个带推送通知服务的活动.p8键 APN表示推送消息已成功发送 DidReceiveEmotonification不会启动 在后台和前台试用应用程序 使用AWS:入站-端口22,出站-所有端口 以下是回应: {“已发送”:[{“设备”:“CC7EC9A821CF232F9510193CA3FFE7D13DD756351794DF3E44F

我的设备未接收后台推送通知。我束手无策,我已经尝试了我能找到的所有教程,并通过堆栈溢出进行了搜索

  • 设备和APN设置为调试/开发
  • 我可以成功注册设备令牌
  • 我有一个带推送通知服务的活动.p8键
  • APN表示推送消息已成功发送
  • DidReceiveEmotonification不会启动
  • 在后台和前台试用应用程序
  • 使用AWS:入站-端口22,出站-所有端口
以下是回应:

{“已发送”:[{“设备”:“CC7EC9A821CF232F9510193CA3FFE7D13DD756351794DF3E44F9112C037C2A”}],“失败”:[]}

这是我的密码:

AppDelegate.swift

import UIKit
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        if #available(iOS 10, *) {
            let center  = UNUserNotificationCenter.current()
            center.delegate = self
            center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
                if error == nil {
                  UIApplication.shared.registerForRemoteNotifications()
                }
            }
        }
    }

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {            
        var deviceTokenString: String = ""
        for i in 0..<deviceToken.count {
            deviceTokenString += String(format: "%02.2hhx", deviceToken[i] as CVarArg)
        }
        UserDefaults.standard.set(deviceTokenString, forKey: "push_token")
    }


    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter,  willPresent notification: UNNotification, withCompletionHandler   completionHandler: @escaping (_ options:   UNNotificationPresentationOptions) -> Void) {
        print("Handle push from foreground")
        print("\(notification.request.content.userInfo)")
        completionHandler([.alert, .badge, .sound])
    }

    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        print("Handle push from background or closed")
        // if you set a member variable in didReceiveRemoteNotification, you  will know if this is from closed or background
        print("\(response.notification.request.content.userInfo)")
        completionHandler()
    }
}

我不确定情况是否如此,因为您的服务器正在使用此APN节点模块,而我从未使用过它,但我使用Firebase,每次尝试使用
内容\u available:1
,它都不起作用。所做的工作是
内容\u可用:true

希望这对你有用。

这是你在AppDelegate上写的

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        print("Silent push notification received: \(userInfo)")    
    }
但是你还写了:

center.delegate = notificationDelegate
这意味着您是AppDelegate而不是通知中心的代表

要么换成

center.delegate = self
或者只需将该函数删除到采用它的类,即您的
UYLNotificationDelegate

你看起来很好。虽然我不知道如何读取node.js,所以您可能配置的负载不正确。此外,有时如果服务器没有正确管理多个设备令牌,您可能根本不会收到静默通知,或者我们后端的另一个问题是,在我们测试通知时,他们正在将通知服务器重定向到其本地计算机,显然他们没有进入

来自的示例有效负载应如下所示:

{
    "aps" : {
        "content-available" : 1
    },
    "acme1" : "bar",
    "acme2" : 42
}
您确定生成的有效负载具有类似的格式吗

  • 我错误地建议您将
    center.delegate=notificationDelegate
    更改为
    center.delegate=self
    ,但如果我那么绝望,我会尝试一下:d

  • 确保已启用(尽管仍然没有启用),您应该在前台接收到该选项

  • 如果这不起作用,请尝试将其与警报/徽章一起发送,然后查看 如果有效的话。很可能不起作用:/

  • 删除应用程序,然后重试 请重新安装。有时,如果 你只需重新安装
  • 清理派生数据,清理构建,重新启动Xcode
编辑后:


你不应该删除didReceive远程通知我感谢大家的意见,我解决了这个问题

“aps”不属于有效负载字段,但应位于通知构造函数中

Node.js:

var note = new apn.Notification({
    aps: {
        "content-available" : 1
    }
});

note.payload = {
    custom_field_1: {},
    custom_field_2: ''
};

我一直在使用
cert.pem
key.pem
发送iOS推送消息。我不知道这个示例是否有用。我可以发布它,但只想确认在您的case@KukicVladimir您使用哪个node.js库发送推送通知?我使用的是
apn
,与您使用的node.js库相同n最新?服务器名称最近更改。您能检查通知发送到哪些服务器名称吗?非常感谢您的回复,我已清理了AppDelegate,以便更容易查看发生了什么
var note = new apn.Notification({
    aps: {
        "content-available" : 1
    }
});

note.payload = {
    custom_field_1: {},
    custom_field_2: ''
};