我有一个推送通知,其中启用了徽章、声音和警报。 如果程序没有运行 - 一切都很好。但是当程序运行时 - 我需要阻止所有不是由程序生成的声音、警报和徽章,因为我与我的服务器有实时连接,并在 APNS 向我的服务器发送通知之前接收所有事件苹果电脑设备。 我找到了隐藏警报的方法,但找不到任何方法来接管对dockTile图标徽章的控制。 如果我这样做:
-(void)application:(NSApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[[NSApp dockTile] setBadgeLabel:nil];
}
什么也没发生,APNS 设置的徽章仍然存在。我尝试对badgeLabel或dockTileNumber属性进行KVO,如图所示here ,但observeValueForKeyPath:ofObject:change:context: 永远不会被调用。 APNS 如何设置badgeLabel?也许我做错了什么,并且有正确的方法可以在程序运行时禁用警报/声音/徽章?
最佳答案
由于我没有找到任何解决方案来正确执行此操作,因此我将接受我的解决方法:
-(void)application:(NSApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[application dockTile].badgeLabel = @" ";
[application dockTile].badgeLabel = @"";
}
可以通过NSUserNotificationCenterDelegate
委托(delegate)方法userNotificationCenter:shouldPresentNotification:
禁用警报:
-(BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification
{
//apple push notification alert will contain userInfo with aps payload, so disable here
if (notification.userInfo[@"aps"])
return NO;
return YES;
}
我还没有找到任何禁用声音的方法。
关于macos - 如何防止设置不是由我的代码设置的 [NSApp dockTile] badLabel 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23768278/