如何在 iOS 上访问 Titanium Hyperloop 中传送的 UNNotifications?

How to access delivered UNNotifications in Titanium Hyperloop on iOS?

我正在 Titanium 中创建一个应用程序,我正在使用超级循环访问本机 API 的不同部分,这一直运行良好。但现在我正在尝试根据通知 ID 计算用户收到的通知,但这似乎不起作用(接近工作)。

这就是我现在正在尝试的(我想最终得到 userInfo.id)

exports.getDelivered = function() {
  var UNUserNotificationCenter = require('UserNotifications/UNUserNotificationCenter');

  var nc = UNUserNotificationCenter.currentNotificationCenter();
  nc.getDeliveredNotificationsWithCompletionHandler(function(notifications) {
    for (var i = 0; i < notifications.count; i++) {
      var notification = notifications.objectAtIndex(i);
      console.log('notification: ' + notification);
      console.log('notification.request.identifier: ' + notification.request.identifier);
    }
  });  
};

这似乎确实得到了正确的通知,因为第一条日志语句给我:

<UNNotification: 0x101075150; 
    date: 2018-08-14 12:58:00 +0000, 
    request: <UNNotificationRequest: 0x10106cbd0; 
        identifier: DB9191AC-B716-4F43-8E03-7613C8D6BFC6, 
        content: <UNNotificationContent: 0x106326930; 
            title: , 
            subtitle: (null), 
            body: Have you completed one crossword, puzzle or brain game today?, 
            categoryIdentifier: , 
            launchImageName: , 
            peopleIdentifiers: (), 
            threadIdentifier: , 
            attachments: (), 
            badge: 1, 
            sound: (null), 
            hasDefaultAction: YES, 
            defaultActionTitle: (null), 
            shouldAddToNotificationsList: YES, 
            shouldAlwaysAlertWhileAppIsForeground: NO, 
            shouldLockDevice: NO, 
            shouldPauseMedia: NO, 
            isSnoozeable: NO, 
            fromSnooze: NO, 
            darwinNotificationName: (null), 
            darwinSnoozedNotificationName: (null), 
            trigger: <UNLegacyNotificationTrigger: 0x100f87010; 
                repeats: YES, 
                date: 2018-08-14 12:58:00 +0000, 
                timeZone: Europe/Amsterdam (CEST) offset 7200 (Daylight), 
                remainingRepeatCount: -2147483648, 
                totalRepeatCount: -2147483648, 
                repeatInterval: 16, 
                repeatCalendar: (null)
            >
        >
    >
>

但是第二个日志语句似乎导致了某种没有消息的异常。所以我的问题是我如何实际访问通知对象,尤其是我为通知提供的 ID (userInfo.id)?

我找到了答案,显然我必须先将通知转换为 UNNotification。

exports.getDelivered = function() {
  var UNUserNotificationCenter = require('UserNotifications/UNUserNotificationCenter');
  var UNNotification = require('UserNotifications/UNNotification');

  var nc = UNUserNotificationCenter.currentNotificationCenter();
  nc.getDeliveredNotificationsWithCompletionHandler(function(notifications) {
    for (var i = 0; i < notifications.count; i++) {
      var notification = UNNotification.cast(notifications.objectAtIndex(i));
      console.log('notification: ' + notification);
      console.log('notification.date: ' + notification.request.content.userInfo.objectForKey("id"));
    }
  });  
};