코코아:사용자 알림이 전달되지 않음
-
26-12-2019 - |
문제
지역 알림을 전달하려고 하는데 다음과 같습니다.
NSUserNotification *notification = [[NSUserNotification alloc] init];
//set title, subtitle, and sound
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
하지만 알림이 나타나지 않습니다.앱이 맨 앞에 있으면 알림이 표시되지 않는 경우가 있지만 제공 시에는 표시되지 않는 경우가 있습니다.시스템 기본 설정에서 내 앱의 알림이 허용되는지 확인하고 userNotificationCenter:shouldPresentNotification도 재정의했습니다.항상 YES를 반환하는 메서드이지만 여전히 알림을 표시하지 않습니다.
가장 혼란스러운 점은 Mavericks로 업데이트하기 전까지 모든 것이 잘 작동했다는 것입니다.업데이트에서 뭔가 변경된 것 같은데 무엇인지 알 수 없습니다.
도와 주셔서 감사합니다.
해결책
내 생각엔 뭔가가 그런 것 같아 nil
.(유효하고 0이 아닌)을 할당하고 있는지 확인하십시오. title
, 또는 informativeText
.
다음과 같은 다른 속성에 유효하지 않은 값이 있다고 생각합니다. otherButtonTitle
알림이 표시되지 않을 수도 있습니다.
콘솔에 오류 메시지가 있습니까?
디버거를 사용하거나 NSLog()
알림에 할당된 값을 평가하는 문입니다.다음과 같은 경우에도 이 문제를 볼 수 있습니다. NSUserNotification
포인터가 없습니다(게시한 코드의 경우는 아니지만 언급할 가치가 있음).
다음은 Mac OS X 10.9(Mavericks)에서 작동하는 앱 대리자에 대한 최소 테스트입니다.
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSUserNotificationCenter* unc = [NSUserNotificationCenter defaultUserNotificationCenter];
unc.delegate = self;
NSUserNotification* notice = [[NSUserNotification alloc] init];
notice.title = @"title"; // notifications need a title, or informativeText at minimum to appear on screen
//notice.informativeText = @"informativeText";
NSLog(@"notification: %@, notification center:%@", notice, unc);
[unc deliverNotification:notice];
}
// The notifications will always dispaly even if we are in the foreground
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification
{
return YES;
}
제휴하지 않습니다 StackOverflow