iOS 未使用 apn nodejs 接收后台推送通知 [英] iOS not receiving background push notification using apn nodejs

查看:24
本文介绍了iOS 未使用 apn nodejs 接收后台推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的设备没有收到后台推送通知.我不知所措,我已经尝试了所有可以通过 Stack Overflow 找到和搜索的教程.

My device is not receiving background push notifications. I'm at wits end, I've tried every tutorial I can find and searched through Stack Overflow.

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

这是回复:

{"sent":[{"device":"cc7ec9a821cf232f9510193ca3ffe7d13dd756351794df3f3e44f9112c037c2a"}],"失败":[]}

{"sent":[{"device":"cc7ec9a821cf232f9510193ca3ffe7d13dd756351794df3f3e44f9112c037c2a"}],"failed":[]}

这是我的代码:

AppDelegate.swift

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()
    }
}

代码:

Node.js

.p8 密钥来自开发者控制台:Keys > All

.p8 key is from Developer Console: Keys > All

var apn = require('apn');

var apnProvider = new apn.Provider({
    token: {
        key: "/path/to/AuthKey_1234ABCD.p8",
        keyId: "1234ABCD",
        teamId: "1234ABCD"
    },
    production: false
});

var note = new apn.Notification();
note.topic = "com.company.app";
note.payload = {
    aps: {
        "content-available" : 1
    },
    custom_field: {}
};

apnProvider.send(note, push_token).then(result => {
            console.log('result: ', result);
            console.log("sent:", result.sent.length);
            console.log("failed:", result.failed.length);
});

推荐答案

感谢大家的意见,我找到了问题所在.

I appreciate everyone's input, I figured out the issue.

'aps' 不属于有效载荷字段,而应在通知构造函数中.

'aps' does not belong in the payload field, but instead should be in the Notification constructor.

Node.js:

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

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

这篇关于iOS 未使用 apn nodejs 接收后台推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆