5

में स्थानीय अधिसूचनाएं फायरिंग नहीं कर रही हैं IOS 10 के लिए UNUserNotificationCenter का उपयोग कर रहा हूं। परीक्षण के लिए, मैं वर्तमान समय से 10 सेकंड के लिए स्थानीय अधिसूचना सेट कर रहा हूं।आईओएस 10

यह है कि मैं क्या करने की कोशिश की है,

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; 
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) 
          completionHandler:^(BOOL granted, NSError * _Nullable error) { 
           if (!error) { 
            NSLog(@"request succeeded!"); 
            [self set10Notifs]; 
           } 
          }];  
} 

-(void) set10Notifs 
{ 
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) { 
    #if XCODE_VERSION_GREATER_THAN_OR_EQUAL_TO_8 
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 

    [calendar setTimeZone:[NSTimeZone localTimeZone]];   

    NSDateComponents *components = [calendar components:NSCalendarUnitTimeZone fromDate:[[NSDate date] dateByAddingTimeInterval:10]]; 

    UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init]; 
    objNotificationContent.title = [NSString localizedUserNotificationStringForKey:@"Prayer!" arguments:nil]; 
    objNotificationContent.body = [NSString localizedUserNotificationStringForKey:@"Time now" 
                     arguments:nil]; 
    objNotificationContent.sound = [UNNotificationSound defaultSound]; 

    UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:NO]; 


    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"Prayer" 
                      content:objNotificationContent trigger:trigger]; 
    UNUserNotificationCenter *userCenter = [UNUserNotificationCenter currentNotificationCenter]; 
    [userCenter addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { 
     if (!error) { 
      NSLog(@"Local Notification succeeded"); 
     } 
     else { 
      NSLog(@"Local Notification failed"); 
     } 
    }]; 
#endif 
    } 
} 

मैं लॉग देख सकते हैं "स्थानीय अधिसूचना सफल रहा"। लेकिन स्थानीय अधिसूचना डिवाइस में फायरिंग नहीं कर रही है।

AppDelegate में, मैं

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    // Override point for customization after application launch. 
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; 
    center.delegate = self; 
    return YES; 
} 
- (void)userNotificationCenter:(UNUserNotificationCenter *)center 
    willPresentNotification:(UNNotification *)notification 
    withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler { 
    NSLog(@"Notification is triggered"); 
    completionHandler(UNNotificationPresentationOptionBadge); 
} 

-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{ 
    NSLog(@"User Info : %@",response.notification.request.content.userInfo); 
    completionHandler(); 
} 

क्या मैं गलत क्या किया जोड़ा? ऐप अधिसूचनाओं को क्यों नहीं निकाल दिया गया?

+0

http://stackoverflow.com/questions/37807302/add-local-notification-in -ios10-swift-3? rq = 1 – Sanju

+0

अपने डिवाइस का समय और ट्रिगरटाइम जांचें - http://stackoverflow.com/questions/39941778/how-to-schedule-a-local-notification-in-ios-10- विशेषण -c? rq = 1 – Sanju

+0

क्या आपने एक्सकोड सेटिंग्स से पुश सूचनाएं सक्षम की हैं? – Stefan

उत्तर

6

सेट NSDateComponents की तरह:

NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitTimeZone fromDate:[[NSDate date] dateByAddingTimeInterval:10]]; 
+0

[एकाधिक अधिसूचनाएं] सेट नहीं कर सकता है (http://stackoverflow.com/questions/40216915/multiple-unusernotifications-not-firing) .... – NAZIK

+0

ऐसा प्रतीत होता है कि आपको कैलेंडर के ' तिथि कॉम्पोनेंट्स (_ घटक: सेट करें <कैलेंडर.कंपोनेंट>, तिथि से: दिनांक) 'फ़ंक्शन। किसी भी अन्य कैलेंडर 'डेट कॉम्पोनेंट्स ...' से तारीख घटकों को शुरू करना अधिसूचना को शेड्यूल नहीं करता है। (स्विफ्ट 3) –

+0

@ एनजेआईआईके, आपने कई अधिसूचनाओं के लिए इसे कैसे काम किया? मैं एक ही मुद्दे पर फंस गया हूँ। कृपया एक नज़र डालें: https://stackoverflow.com/questions/44132879/ios-local-notification-not-firing-second-time-but-shows-in-getpendingnotificatio –

0

या एक कैलेंडर ट्रिगर के बजाय एक समय ट्रिगर का उपयोग

-1
@IBAction func sendNotification(_ sender: Any) { 

    let content = UNMutableNotificationContent() 
    content.title = "Hello" 
    content.body = "Ved Rauniyar !!!" 
    // content.badge = 1 
    content.sound = UNNotificationSound.default() 
    // Deliver the notification in five seconds. 
    let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: false) 
    let url = Bundle.main.url(forResource:"ved", withExtension: "png") 
    let attachment = try? UNNotificationAttachment(identifier: "FiveSecond", 
                url: url!, 
                options: [:]) 
    content.attachments = [attachment!] 
    let request = UNNotificationRequest.init(identifier: "FiveSecond", content: content, trigger: trigger) 
    // Schedule the notification. 
    let center = UNUserNotificationCenter.current() 
    center.add(request) { (error) in 
     // print(error!) 
    } 
}