Mon terminal ne reçoit pas de notifications push en arrière-plan. Je suis à bout de nerfs, j'ai essayé tous les tutoriels que je peux trouver et chercher dans Stack Overflow.iOS ne recevant pas la notification push en arrière-plan à l'aide d'apn nodejs
- périphériques et APN sont mis à debug/développement
- Je peux enregistrer dispositif de jeton
- J'ai réussi une clé de .p8 active avec les services de notification push
- APN dit le message push a été envoyé avec succès
- didReceiveRemoteNotification ne se déclenche pas
- essayées avec application en arrière-plan et de premier plan
- utilisant AWS: entrant - le port 22, sur liés - tous les ports
C'est la réponse:
{ "envoyé": [{ "device": "cc7ec9a821cf232f9510193ca3ffe7d13dd756351794df3f3e44f9112c037c2a"}], "a échoué": []}
Voici mon code:
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 clé est de Developer Console: Touches> Tous
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);
});
J'ai envoyé des messages push iOS en utilisant '' cert.pem' et key.pem'. Je ne sais pas si cet exemple serait utile. Je peux le poster, mais je veux juste confirmer si c'est possible dans votre cas –
@KukicVladimir quelle bibliothèque node.js utilisez-vous pour envoyer les notifications push? – Andrew
J'utilise 'apn', comme vous le faites –