[英]iOS not receiving background push notification using apn nodejs
我的設備未收到后台推送通知。 我在智慧結束時,我已經嘗試了我可以通過Stack Overflow找到和搜索的每個教程。
這是回應:
{ “發送”:[{ “設備”: “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:
Node.js的
.p8密鑰來自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);
});
我很感激每個人的意見,我想出了這個問題。
'aps'不屬於有效負載字段,而應該位於通知構造函數中。
Node.js的:
var note = new apn.Notification({
aps: {
"content-available" : 1
}
});
note.payload = {
custom_field_1: {},
custom_field_2: ''
};
我不確定是否是這種情況,因為您的服務器正在使用此APN節點模塊而我從未使用它,但我使用Firebase並且每次嘗試使用content_available : 1
,它都無效。 什么工作是content_available : 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 不是 notificationCenter的委托。
要么改變為
center.delegate = self
或者只是將該函數移除到采用它的類,即 UYLNotificationDelegate
你似乎沒事。 雖然我不知道如何讀取node.js,因此您可能已將錯誤配置為錯誤。 有時候,如果服務器沒有正確管理多個設備令牌,你可能根本沒有得到無聲通知或我們后端的另一個問題是他們在我們測試通知時將通知服務器重定向到他們的本地機器,顯然他們沒有進來!
來自Apple的示例有效負載應如下所示:
{ "aps" : { "content-available" : 1 }, "acme1" : "bar", "acme2" : 42 }
您確定生成的有效負載具有類似的格式嗎?!
我錯誤地建議您應該將center.delegate = notificationDelegate
更改為center.delegate = self
但如果我是絕望的話,我會試一試:D。
確保啟用了后台應用程序刷新 (盡管如果沒有啟用它,您應該在前台接收
如果這不起作用,請嘗試使用警報/徽章發送它,看看是否有效。 可能它不起作用:/
編輯后:
您不應該刪除didReceive 遠程通知< - 這是用於檢索任何具有content-available : 1
推送通知content-available : 1
。
willPresent
將回調該怎么辦。 僅當有效載荷在有效載荷中具有警報或徽章或聲音時才適用 didReceiveNotificationResponse
不是你想象的那樣。 它用於處理管理用戶從通知中選擇的操作,例如,您的通知有兩個操作(暫停,刪除),如下所示: 您使用此回調處理對通知的響應 。
所以從技術上講,如果你只是有一個無聲通知,你只能得到第三個回調,而不是前兩個回調。
如果你在前台有一個應用程序,並且有效負載有: content-available :1
也有動作而用戶點擊了動作,那么你將首先獲得第3個回調,然后是第一個回調,然后是第二個回調。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.