Xcode中的通知不会出现

时间:2017-03-03 18:13:06

标签: ios objective-c xcode

我正在尝试向我的代码添加通知,以便在按下按钮时,用户将保存到数据库并弹出一条通知,说明用户已成功保存到数据库中。我检查了我的代码,但它仍然无法正常工作。没有错误,但也没有任何通知。我正在尝试在屏幕顶部创建一个小横幅,上面写着“客户保存到数据库”。

这是我的代码;如果您能发现任何错误或建议如何修复它,我将非常感激!

使用Xcode 8.2.1。和目标C

AppDelegate.m中的

home/aherys/Documents/nanotekspicelib/builder.cpp: Dans le constructeur « builder::builder() »:
/home/aherys/Documents/nanotekspicelib/builder.cpp:15:64: erreur : no matching function for call to « std::map<std::__cxx11::basic_string<char>, nts::IComponent* (* const)(const std::__cxx11::basic_string<char>&)>::insert(std::pair<const char*, nts::IComponent* (builder::*)(const std::__cxx11::basic_string<char>&) const>) »
     listFunction.insert(make_pair("4081", &builder::create4081));
                                                                ^
In file included from /usr/include/c++/6.3.1/map:61:0,
                 from /home/aherys/Documents/nanotekspicelib/builder.h:10,
                 from /home/aherys/Documents/nanotekspicelib/builder.cpp:5:
/usr/include/c++/6.3.1/bits/stl_map.h:731:7: note : candidate: std::pair<typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<std::pair<const _Key, _Tp> >::other>::iterator, bool> std::map<_Key, _Tp, _Compare, _Alloc>::insert(const value_type&) [with _Key = std::__cxx11::basic_string<char>; _Tp = nts::IComponent* (* const)(const std::__cxx11::basic_string<char>&); _Compare = std::less<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, nts::IComponent* (* const)(const std::__cxx11::basic_string<char>&)> >; typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<std::pair<const _Key, _Tp> >::other>::iterator = std::_Rb_tree_iterator<std::pair<const std::__cxx11::basic_string<char>, nts::IComponent* (* const)(const std::__cxx11::basic_string<char>&)> >; std::map<_Key, _Tp, _Compare, _Alloc>::value_type = std::pair<const std::__cxx11::basic_string<char>, nts::IComponent* (* const)(const std::__cxx11::basic_string<char>&)>]
       insert(const value_type& __x)
NewCustomerViewController.m中的

-(void)application:(UIApplication *)application     didRegisterUserNotificationSettings:(nonnull UIUserNotificationSettings *)notificationSettings{}

在viewdidload中:

@interface NewCustomerViewController(){NSUserDefaults *defaults;}

最后,在我的IBaction中为节省客户的按钮:

defaults = [NSUserDefaults standardUserDefaults];
UIUserNotificationType types = UIUserNotificationTypeBadge| UIUserNotificationTypeSound| UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];

再次感谢!

1 个答案:

答案 0 :(得分:0)

在iOS 10中,您在iOS 9及更低版本中使用本地通知的方式完全不同。 以下是本地通知的代码:

目标C:

1. In App-delegate.h file use @import UserNotifications;
2. App-delegate should conform to UNUserNotificationCenterDelegate protocol
3. In didFinishLaunchingOptions use below code :

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

-(void)showAlert {
    UIAlertController *objAlertController = [UIAlertControlleralertControllerWithTitle:@"Alert" message:@"show an alert!"preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction
                                   actionWithTitle:@"OK"
                                   style:UIAlertActionStyleCancel
                                   handler:^(UIAlertAction *action) {
                                       NSLog(@"Ok clicked!");
                                   }];
    [objAlertController addAction:cancelAction];
    
    
    [[[[[UIApplication sharedApplication] windows] objectAtIndex:0] rootViewController] presentViewController:objAlertController animated:YEScompletion:^{
        
    }];
    
}
4. Now create a button in any view controller and in IBAction use below code :
UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];
    objNotificationContent.title = [NSStringlocalizedUserNotificationStringForKey:@“Notification!”arguments:nil];
    objNotificationContent.body = [NSStringlocalizedUserNotificationStringForKey:@“This is local notification message!“
                                                         arguments:nil];
    objNotificationContent.sound = [UNNotificationSounddefaultSound];
    
    /// 4. update application icon badge number
    objNotificationContent.badge = @([[UIApplicationsharedApplication] applicationIconBadgeNumber] + 1);
    
    // Deliver the notification in five seconds.
    UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger
                                                  triggerWithTimeInterval:10.f repeats:NO];       
    
    UNNotificationRequest *request = [UNNotificationRequestrequestWithIdentifier:@“ten”
                                                                          content:objNotificationContent trigger:trigger];
    /// 3. schedule localNotification
    UNUserNotificationCenter *center = [UNUserNotificationCentercurrentNotificationCenter];
    [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        if (!error) {
            NSLog(@“Local Notification succeeded“);
        }
  else {
      NSLog(@“Local Notification failed“);
  }
    }];

您可以参考苹果参考文档https://developer.apple.com/reference/usernotifications for UserNotification