appceleratorappcelerator-titaniumappcelerator-hyperloop

How to access delivered UNNotifications in Titanium Hyperloop on iOS?


I'm creating an application in Titanium and I'm using hyperloop to access different parts of the native API's and this has been working great. But now I'm trying to count the notifications a user has received based on notification id, but this doesn't seem to work (it's close to working).

This is what I'm trying now (I want to get the userInfo.id eventually)

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);
    }
  });  
};

This does seem to get the right notification, as the first log statement gives me:

<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)
            >
        >
    >
>

But the second log statement seems to cause some kind of exception without a message. So my question is how do I actually access the notification object, especially the id that I've given to the notification (userInfo.id)?


Solution

  • I found the answer, apparently I have to cast to notification to a UNNotification first.

    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"));
        }
      });  
    };