如何在iOS的Titanium Hyperloop中访问已交付的UNNotifications?

时间:2018-08-14 14:44:14

标签: appcelerator appcelerator-titanium appcelerator-hyperloop

我正在Titanium中创建一个应用程序,并且正在使用hyperloop来访问本机API的不同部分,这一直很好。但是,现在我试图根据通知ID计算用户收到的通知,但这似乎不起作用(即将开始工作)。

  • Titanium SDK:7.1.1.GA
  • iOS:11.4.1
  • 设备:iPhone 5s

这就是我现在正在尝试的(我想最终获得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)
            >
        >
    >
>

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

1 个答案:

答案 0 :(得分:0)

我找到了答案,显然我必须先将其发送给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"));
    }
  });  
};