首页 > 解决方案 > 如何在 iOS 上访问 Titanium Hyperloop 中交付的 UNNotifications?

问题描述

我正在 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)?

标签: appceleratorappcelerator-titaniumappcelerator-hyperloop

解决方案


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

推荐阅读