甚至通过 NodeJS 发送推送的通知也看不到操作按钮



我在 nodeJS 下面发送时甚至看不到推送通知警报。从相同的 nodeJS 配置发送简单警报没有问题,如下所示:

notification.alert = 'Hello World u270C';

我看到本地计划通知的警报操作包括:

self.scheduleNotification(event: "test", interval: 3)

很高兴知道我在通知有效负载方面做错了什么。

应用程序委托.swift文件:

import UIKit
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.alert, .sound, .badge]) { (status: Bool, error: Error?) in
if error == nil {
self.registerCategory()
// Now, I can see action buttons for that scheduled notification, but not from payload (nodeJS)
self.scheduleNotification(event: "test", interval: 3)
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
print("Push registration success.")
}
else {
print("Push registration failed:")
print(error!.localizedDescription)
}
}
return true
}
func registerCategory() -> Void {
let acceptAction = UNNotificationAction(identifier: "ACCEPT_ACTION",
title: "Accept",
options: UNNotificationActionOptions(rawValue: 0))
let declineAction = UNNotificationAction(identifier: "DECLINE_ACTION",
title: "Decline",
options: UNNotificationActionOptions(rawValue: 0))
let category : UNNotificationCategory = UNNotificationCategory.init(identifier: "MEETING_INVITATION", actions: [acceptAction, declineAction], intentIdentifiers: [], options: [])
let center = UNUserNotificationCenter.current()
center.setNotificationCategories([category])
}
func scheduleNotification (event : String, interval: TimeInterval) {
let content = UNMutableNotificationContent()
content.title = event
content.body = "body"
content.categoryIdentifier = "MEETING_INVITATION"
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: interval, repeats: false)
let identifier = "id_" + event
let request = UNNotificationRequest.init(identifier: identifier, content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request, withCompletionHandler: { (error) in
})
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
print("Handled deviceToken: ")
print(token)
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
completionHandler()
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(.alert)
}
}

应用程序.js文件:

var apn = require('apn')
var util = require('util')
// Set up apn with the APNs Auth Key:
var apnProvider = new apn.Provider({  
token: {
key: '____', // Path to the key p8 file
keyId: '____', // The Key ID of the p8 file (available at https://developer.apple.com/account/ios/certificate/key)
teamId: '____', // The Team ID of your Apple Developer Account (available at https://developer.apple.com/account/#/membership/)
},
production: false // Set to true if sending a notification to a production iOS app
});
// Enter the device token from the Xcode console:
var deviceToken = '____';
// Prepare a new notification:
var notification = new apn.Notification();
// Bundle ID:
notification.topic = '____'; 
// It works:
notification.category = "MEETING_INVITATION";
notification.alert = {"title":"React with push","body":"Tap to see actions about that push."};
// It doesn't work:
// notification.payload = {"aps":{"category":"MEETING_INVITATION","alert":{"title":"React with push","body":"Tap to see actions about that push."}}};
apnProvider.send(notification, deviceToken).then(function(result) {  
// Check the result for any failed devices
console.log(util.inspect(result, true, 7, true));
});

我认为您需要实现此方法:

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
//do some data processing if needed
completion(.alert)
}

因为联合国信息管理通知中心应处理此类通知,而不是应用程序委派。请注意它是联合国大学通知中心代表协议的一部分。希望这会有所帮助

我已经解决了使用 nodeJS 模块的另一个属性的问题,如下所示:

notification.category = "MEETING_INVITATION";
notification.alert = {"title":"React with push","body":"Tap to see actions about that push."};

但是 aps 类型有效负载(仅限内容(仍然存在问题:

notification.payload = {"aps":{"category":"MEETING_INVITATION","alert":{"title":"React with push","body":"Tap to see actions about that push."}}};

这就是为什么,我离开了那个节点js模块。我发现了一些不错的Mac AppStore APNS应用程序。其中一个做得很好,超出了我的预期。也许我们也可以用nodeJS解决它,但我找不到它。

最新更新