2012-08-05 8 views

उत्तर

148

माउंटेन शेर में अधिसूचनाओं को दो वर्गों द्वारा नियंत्रित किया जाता है। NSUserNotification और NSUserNotificationCenterNSUserNotification आपकी वास्तविक अधिसूचना है, इसमें एक शीर्षक, एक संदेश इत्यादि है जिसे गुणों के माध्यम से सेट किया जा सकता है। आपके द्वारा बनाई गई अधिसूचना देने के लिए, आप NSUserNotificationCenter में उपलब्ध deliverNotification: विधि का उपयोग कर सकते हैं। एप्पल डॉक्स NSUserNotification & NSUserNotificationCenter पर विस्तृत जानकारी उपलब्ध है, लेकिन बुनियादी कोड एक अधिसूचना पोस्ट करने के लिए इस तरह दिखता है:

- (IBAction)showNotification:(id)sender{ 
    NSUserNotification *notification = [[NSUserNotification alloc] init]; 
    notification.title = @"Hello, World!"; 
    notification.informativeText = @"A notification"; 
    notification.soundName = NSUserNotificationDefaultSoundName; 

    [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification]; 
} 

कि एक शीर्षक के साथ एक अधिसूचना का उत्पादन होगा, संदेश और उस डिफ़ॉल्ट ध्वनि जब चलने लगेगा यह प्रदर्शित होता है। इस बात की तुलना में अधिसूचनाओं के साथ आप बहुत कुछ कर सकते हैं (जैसे शेड्यूलिंग अधिसूचनाएं) और यह मेरे द्वारा लिंक किए गए दस्तावेज में विस्तृत है।

एक छोटा सा बिंदु, अधिसूचना केवल तब प्रदर्शित की जाएगी जब आपका एप्लिकेशन मुख्य एप्लिकेशन होगा। अगर आप अपनी सूचनाओं को प्रदर्शित करना चाहते हैं, भले ही आपका एप्लिकेशन कुंजी है या नहीं, तो आपको NSUserNotificationCenter के लिए एक प्रतिनिधि निर्दिष्ट करना होगा और प्रतिनिधि विधि userNotificationCenter:shouldPresentNotification: को ओवरराइड करना होगा ताकि वह हाँ लौटा सके। NSUserNotificationCenterDelegate के लिए प्रलेखन here

यहां NSUserNotificationCenter को एक प्रतिनिधि प्रदान करने का एक उदाहरण दिया गया है और उसके बाद सूचनाओं को प्रदर्शित करने के लिए सूचनाओं को मजबूर करने के लिए मजबूर किया जा रहा है। अपने आवेदन की AppDelegate.m फ़ाइल में, यह इस तरह से संपादित:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{ 
    [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self]; 
} 

- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification{ 
    return YES; 
} 

और AppDelegate.h में, घोषणा करते हैं कि वर्ग NSUserNotificationCenterDelegate प्रोटोकॉल के अनुरूप है:

@interface AppDelegate : NSObject <NSApplicationDelegate, NSUserNotificationCenterDelegate> 
+0

आप कैसे userNotificationCenter ओवरराइड करने के लिए पर विस्तृत कर सकते हैं ? (माफ करना मैं वास्तव में इस के लिए नया :) :) – haseo98

+3

@ haseo98 Yup, मैंने अभी अपने जवाब में एक उदाहरण जोड़ा है। – alexjohnj

+0

मुझे विधि के applicationdidfinishlaunching सेक्शन में एक त्रुटि मिल रही है, असंगत प्रकार 'id ' के पैरामीटर के लिए 'AppDelegate * const __strong' भेजना। कोई विचार? – haseo98