2017-07-14 123 views
2

我的设备没有收到后台推送通知。我很有智慧,我尝试了每个可以通过Stack Overflow找到并搜索到的教程。iOS没有收到使用apn nodejs的后台推送通知

  • 设备和APN设置为调试/开发
  • 我可以成功注册设备令牌
  • 我有推送通知服务
  • APN积极.p8键说,推送消息发送成功
  • didReceiveRemoteNotification不火
  • 尝试使用AWS在后台应用和前景
  • :入站 - 端口22,出结合 - 所有端口

这是响应:

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

这里是我的代码:

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

的XCode:

enter image description here enter image description here

Node.js的

.p8关键是从开发者控制台:键>所有

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); 
}); 
+0

我已经发送的iOS使用'cert.pem'和'key.pem'推送消息。我不知道这个例子会有帮助。我可以发布它,但只是想确认这是否可能在你的情况下 –

+0

@KukicVladimir你用什么node.js库发送推送通知? – Andrew

+0

我正在使用'apn',和你一样 –

回答

0

我不知道,如果是这样的话因为您的服务器正在使用此APN节点模块,并且我从未使用它,但是我使用Firebase,并且每次尝试使用content_available : 1时都无法使用。什么工作是content_available : true

希望这对你有用。

0

您写了这对你的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,所以您可能已经配置了不正确的负载。有时候,如果服务器没有正确管理多个设备令牌,您可能不会收到任何安全通知,或者我们与后端发生的另一个问题是,在我们测试通知时他们将通知服务器重定向到本地计算机,显然他们没有进来!

Apple样本有效载荷应该是这样的:

{ 
    "aps" : { 
     "content-available" : 1 
    }, 
    "acme1" : "bar", 
    "acme2" : 42 
} 

。您确定要产生有效载荷具有类似的格式?

  • 我错误地建议,你应该改变center.delegate = notificationDelegatecenter.delegate = self,但如果我是绝望的我给一个尝试:d。

  • 确保background App Refresh启用(虽然仍然没有这使你应该得到的前景

  • 如果这不起作用,然后尝试与警报/徽章发送它,并看到 是否可行。可能的它不会工作:/

  • 删除应用程序,然后安装 再次有时候授权不,如果你 重装刚刚工作正确
  • 干净得出的数据,干净的构建,重新启动的Xcode

在您编辑后:

你不应该删除的didReceive 远程通知< - 这是用于检索具有content-available : 1任何推送通知。

  • willPresent是回调如果应用程序在FOREGROUND中该怎么做。仅当有效载荷在有效载荷中具有警报或徽章或声音时才适用
  • didReceiveNotificationResponse不是你认为的那样。它用于处理管理用户从通知中选择的操作,例如您的通知有两个动作(贪睡,删除),点击如下图所示: enter image description here

您处理响应使用此回调通知。

所以从技术上讲,如果你只是有一个安静的通知,你只会得到第三个回调,而不是前两个。

如果你有一个应用程序在前台,有效载荷有:content-available :1也有动作,用户点击动作,那么你会首先得到第三个回调,然后第一个回调,然后第二个回调。

+0

我真的很感激你的回应,我已经清理了我的AppDelegate,使得更容易看到发生了什么 – Andrew

+0

@Andrew看到我的编辑 – Honey

0

我很感谢大家的意见,我想清楚了这个问题。

'aps'不属于有效负载字段,而应该在Notification构造函数中。

的Node.js:

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

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