使 UILocalNotifications 仅显示徽章



我一直在研究UILocalNotification的API。我只想在达到触发日期时设置徽章编号。没有弹出窗口。我尝试将通知设置为只有徽章编号 1 且没有警报正文。但这没有用。

//Add new local push notification
locationNotification.fireDate = date;
locationNotification.timeZone = [NSTimeZone defaultTimeZone];
//locationNotification.alertBody = [NSString stringWithFormat:body];
//locationNotification.alertAction = @"Ok";
//locationNotification.soundName = UILocalNotificationDefaultSoundName;
locationNotification.applicationIconBadgeNumber = 1;

这可能吗?我只想在他们打开应用程序后显示消息。

我为我从事的一个项目完成了这样的事情。我只需要显示徽章编号,没有警报。您可能需要在通知中添加"声音",我所做的是添加一个0.5秒的静音声音文件。我的代码如下所示。

        UILocalNotification *notif = [[UILocalNotification alloc] init];
        notif.fireDate = [NSDate  date];
        notif.timeZone = [NSTimeZone defaultTimeZone];
        notif.soundName = @"silence.caf";
        notif.applicationIconBadgeNumber = 2;
        [[UIApplication sharedApplication] scheduleLocalNotification:notif];
        [notif release];

希望这有帮助!

这是一个老问题,但希望这会对某人有所帮助。我认为根本不可能安排无声的本地通知,但似乎确实如此。

这有效:

    UILocalNotification* notification = [[UILocalNotification alloc] init];
    notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
    notification.timeZone = [NSTimeZone systemTimeZone];
    notification.alertBody = nil;        
    notification.applicationIconBadgeNumber = 99;
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];

我还尝试向 userInfo 属性添加一些信息,以便我可以在计划的通知中识别此通知,但这似乎阻止了通知被触发。

取而代之的是,我现在使用无声音频文件作为 soundName 属性,以便我可以对其进行测试。但是,静默音频文件似乎不是实际触发通知所必需的。

如果要执行比将锁屏提醒更新为计划时知道的值更有用的操作,则需要使用静默推送通知并设置"内容可用"标志。

最新更新