使用 PubSub 获取 Gmail 未读邮件计数

发布于 2024-12-15 17:55:06 字数 458 浏览 3 评论 0原文

我正在尝试使用 Cocoa (Mac) 和 PubSub 框架获取 Gmail 未读电子邮件计数。我已经看到一两个链接显示使用 PubSub 和 Gmail,这是到目前为止我的代码。

PSClient *client = [PSClient applicationClient];
NSURL    *url    = [NSURL URLWithString:@"https://mail.google.com/mail/feed/atom/inbox"];
PSFeed   *feed   = [client addFeedWithURL:url];

[feed setLogin: @"myemailhere"];
[feed setPassword: @"mypasswordhere"];

NSLog(@"Error: %@", feed.lastError);

有人知道如何获取未读数吗?

谢谢 :)

I'm trying to get my Gmail unread email count using Cocoa (Mac) and the PubSub framework. I've seen one or two links showing using PubSub and Gmail, here's my code so far.

PSClient *client = [PSClient applicationClient];
NSURL    *url    = [NSURL URLWithString:@"https://mail.google.com/mail/feed/atom/inbox"];
PSFeed   *feed   = [client addFeedWithURL:url];

[feed setLogin: @"myemailhere"];
[feed setPassword: @"mypasswordhere"];

NSLog(@"Error: %@", feed.lastError);

Anyone know how I can get the unread count?

Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转角预定愛 2024-12-22 17:55:06

你有两个问题:一个有解决方案,另一个似乎是一个永恒的问题。

第一个:提要刷新是异步发生的。因此,您需要侦听 PSFeedRefreshingNotification 和 PSFeedEntriesChangedNotification 通知,以查看 feed 何时刷新和更新。通知的对象将是相关的 PSFeed。

举个例子:

-(void)feedRefreshing:(NSNotification*)n
{
    PSFeed *f = [n object];
    NSLog(@"Is Refreshing: %@", [f isRefreshing] ? @"Yes" : @"No");
    NSLog(@"Feed: %@", f);
    NSLog(@"XML: %@", [f XMLRepresentation]);
    NSLog(@"Last Error: %@", [f lastError]);


    if(![f isRefreshing])
    {
        NSInteger emailCount = 0;
        NSEnumerator *e = [f entryEnumeratorSortedBy:nil];
        id entry = nil;

        while(entry = [e nextObject])
        {
            emailCount++;
            NSLog(@"Entry: %@", entry);
        }
        NSLog(@"Email Count: %ld", emailCount);
    }
}

-(void)feedUpdated:(NSNotification*)n
{
    NSLog(@"Updated");
}

-(void)pubSubTest
{
    PSClient *client = [PSClient applicationClient];
    NSURL    *url    = [NSURL URLWithString:@"https://mail.google.com/mail/feed/atom/inbox"];
    PSFeed   *feed   = [client addFeedWithURL:url];

    [feed setLogin: @"[email protected]"];
    [feed setPassword: @"correctPassword"];
    NSError *error = nil;

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(feedUpdated:) name:PSFeedEntriesChangedNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(feedRefreshing:) name:PSFeedRefreshingNotification object:nil];

    [feed refresh:&error];
    if(error)
        NSLog(@"Error: %@", error);
}

第二个(也是更糟糕的)问题是 PubSub 无法正确处理经过身份验证的提要。我在 http://www.dizzey.com/ 看到了这个development/fetching-emails-from-gmail-using-cocoa/ 并且我在自己的系统上重现了相同的行为。我不知道这个 bug 是否是 10.7 特有的,或者它是否影响以前版本的 OS X。

“解决方法”是使用 NSURLConnection 对原始提要 XML 执行经过身份验证的检索。然后,您可以使用其 initWithData:URL: 方法将其推入 PSFeed 中。这样做的一个非常严重的缺点是你实际上不再是 PubSubing 了。您必须运行计时器并在适当的时候手动刷新提要。

我能做的最好的事情就是提交一个错误:rdar://problem/10475065(OpenRadar:1430409)。

您可能应该提交重复的错误,以尝试增加 Apple 修复它的机会。

祝你好运。

You have two problems: one which there's a solution for and one which seems to be a perpetual problem.

The first: Feed refreshes happen asynchronously. So you need to listen to the PSFeedRefreshingNotification and PSFeedEntriesChangedNotification notifications to see when the feed gets refreshed and updated. The notification's object will be the PSFeed in question.

As an example:

-(void)feedRefreshing:(NSNotification*)n
{
    PSFeed *f = [n object];
    NSLog(@"Is Refreshing: %@", [f isRefreshing] ? @"Yes" : @"No");
    NSLog(@"Feed: %@", f);
    NSLog(@"XML: %@", [f XMLRepresentation]);
    NSLog(@"Last Error: %@", [f lastError]);


    if(![f isRefreshing])
    {
        NSInteger emailCount = 0;
        NSEnumerator *e = [f entryEnumeratorSortedBy:nil];
        id entry = nil;

        while(entry = [e nextObject])
        {
            emailCount++;
            NSLog(@"Entry: %@", entry);
        }
        NSLog(@"Email Count: %ld", emailCount);
    }
}

-(void)feedUpdated:(NSNotification*)n
{
    NSLog(@"Updated");
}

-(void)pubSubTest
{
    PSClient *client = [PSClient applicationClient];
    NSURL    *url    = [NSURL URLWithString:@"https://mail.google.com/mail/feed/atom/inbox"];
    PSFeed   *feed   = [client addFeedWithURL:url];

    [feed setLogin: @"[email protected]"];
    [feed setPassword: @"correctPassword"];
    NSError *error = nil;

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(feedUpdated:) name:PSFeedEntriesChangedNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(feedRefreshing:) name:PSFeedRefreshingNotification object:nil];

    [feed refresh:&error];
    if(error)
        NSLog(@"Error: %@", error);
}

The second (and far worse) issue is that PubSub doesn't handle authenticated feeds correctly. I saw this at http://www.dizzey.com/development/fetching-emails-from-gmail-using-cocoa/ and I've reproduced the same behavior on my own system. I don't know if this bug is 10.7 specific or if it affects previous versions of OS X.

The "workaround" is to use NSURLConnection to perform an authenticated retrieval of the raw feed XML. You can then shove that into a PSFeed using its initWithData:URL: method. The very serious drawbacks with this is that you aren't actually PubSubing anymore. You'll have to run a timer and manually refresh the feed whenever appropriate.

The best I was able to do to help you out was to file a bug: rdar://problem/10475065 (OpenRadar: 1430409 ).

You should probably file a duplicate bug to try to increase the chances that Apple will fix it.

Good luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文