한국어

Coding

온누리070 플레이스토어 다운로드
    acrobits softphone
     온누리 070 카카오 프러스 친구추가온누리 070 카카오 프러스 친구추가친추
     카카오톡 채팅 상담 카카오톡 채팅 상담카톡
    
     라인상담
     라인으로 공유

     페북공유

   ◎위챗 : speedseoul


  
     PAYPAL
     
     PRICE
     

pixel.gif

    before pay call 0088 from app


글 수 101

https://stackoverflow.com/questions/37938771/uilocalnotification-is-deprecated-in-ios10


39

It may be a question in advance but I wonder what to use instead of UILocalNotification in iOS10. I am working on an app which has deployment target iOS8 so will it be ok to use UILocalNotification?

98

Yes, you can use UILocalNotification, old APIs also works fine with iOS10, but we had better use the APIs in the User Notifications framework instead. There are also some new features, you can only use with iOS10 User Notifications framework.

This also happens to Remote Notification, for more information: Here.

New Features:

  1. Now you can either present alert, sound or increase badge while the app is in foreground too with iOS 10
  2. Now you can handle all event in one place when user tapped (or slided) the action button, even while the app has already been killed.
  3. Support 3D touch instead of sliding gesture.
  4. Now you can remove specifical local notification just by one row code.
  5. Support Rich Notification with custom UI.

It is really easy for us to convert UILocalNotification APIs to iOS10 User Notifications framework APIs, they are really similar.

I write a Demo here to show how to use new and old APIs at the same time: iOS10AdaptationTips .

For example,

With Swift implementation:

  1. import UserNotifications

    ///    Notification become independent from UIKit
    import UserNotifications
  2. request authorization for localNotification

        let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
            // Enable or disable features based on authorization.
        }
  3. schedule localNotification

  4. update application icon badge number

    @IBAction  func triggerNotification(){
        let content = UNMutableNotificationContent()
        content.title = NSString.localizedUserNotificationString(forKey: "Elon said:", arguments: nil)
        content.body = NSString.localizedUserNotificationString(forKey: "Hello Tom!Get up, let's play with Jerry!", arguments: nil)
        content.sound = UNNotificationSound.default()
        content.badge = UIApplication.shared().applicationIconBadgeNumber + 1;
        content.categoryIdentifier = "com.elonchan.localNotification"
        // Deliver the notification in 60 seconds.
        let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 60.0, repeats: true)
        let request = UNNotificationRequest.init(identifier: "FiveSecond", content: content, trigger: trigger)
    
        // Schedule the notification.
        let center = UNUserNotificationCenter.current()
        center.add(request)
    }
    
    @IBAction func stopNotification(_ sender: AnyObject) {
        let center = UNUserNotificationCenter.current()
        center.removeAllPendingNotificationRequests()
        // or you can remove specifical notification:
        // center.removePendingNotificationRequests(withIdentifiers: ["FiveSecond"])
    }

Objective-C implementation:

  1. import UserNotifications

    // Notifications are independent from UIKit
    #import <UserNotifications/UserNotifications.h>
  2. request authorization for localNotification

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

  4. update application icon badge number

    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
    content.title = [NSString localizedUserNotificationStringForKey:@"Elon said:"
                                                        arguments:nil];
    content.body = [NSString localizedUserNotificationStringForKey:@"Hello Tom!Get up, let's play with Jerry!"
                                                       arguments:nil];
    content.sound = [UNNotificationSound defaultSound];
    
    // 4. update application icon badge number
    content.badge = [NSNumber numberWithInteger:([UIApplication sharedApplication].applicationIconBadgeNumber + 1)];
    // Deliver the notification in five seconds.
    UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger
                                                triggerWithTimeInterval:5.f
                                                repeats:NO];
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"
                                                                        content:content
                                                                        trigger:trigger];
    /// 3. schedule localNotification
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        if (!error) {
            NSLog(@"add NotificationRequest succeeded!");
        }
    }];

Go to here for more information: iOS10AdaptationTips.

updated

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'time interval must be at least 60 if repeating'

let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 60, repeats: true)
  • You are missing argument name in "center.requestAuthorization([.alert, .sound])", it should be " center.requestAuthorization(options: [.alert, .sound])" – MQoder Sep 20 '16 at 9:52
  • I have this set up in my app exactly as written above. The notification only appeared one time. I ran it again with a different notification and now I dont receive any notifications. Any ideas? – Nate4436271 Oct 19 '16 at 18:37
  • @MQoder I‘ve updated the answer. – ElonChan Oct 26 '16 at 3:50 
  • @Nate4436271 I‘ve updated the answer. – ElonChan Oct 26 '16 at 3:50
  • 2
    @Zennichimaro: you can implement both UILocalNotification (iOS 9) and UNNotificationRequest(iOS 10) in your code. Use this to test which iOS is running: if (floor(NSFoundationVersionNumber) >= NSFoundationVersionNumber10_0) { \\ run iOS 10 code } else { // run iOS 9 code } – KoenJan 22 '17 at 15:57
8

Apple have done it again, the correct implementation is: AppDelegate.swift

if #available(iOS 10.0, *) {
        let center = UNUserNotificationCenter.currentNotificationCenter()
        center.requestAuthorizationWithOptions([.Alert, .Sound]) { (granted, error) in
            // Enable or disable features based on authorization.
        }
    } else {
        // Fallback on earlier versions
    }

and don't forget to add

import UserNotifications
2

Local Notifications for iOS 10 in Objetcive-C

If you are programming for a while I am sure you are familiar with the UILocalNotification class, and right now with the arriving of iOS 10 you can see that UILocalNotification is been deprecated. For a detailed implementation visit this blog post

https://medium.com/@jamesrochabrun/local-notifications-are-a-great-way-to-send-notifications-to-the-user-without-the-necessity-of-an-b3187e7176a3#.nxdsf6h2h

1

swift 4

if #available(iOS 10.0, *) {
        let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options: [.alert, .badge, .sound])  { (granted, error) in
            // Enable or disable features based on authorization.
        }
    } else {
        // REGISTER FOR PUSH NOTIFICATIONS
        let notifTypes:UIUserNotificationType  = [.alert, .badge, .sound]
        let settings = UIUserNotificationSettings(types: notifTypes, categories: nil)
        application.registerUserNotificationSettings(settings)
        application.registerForRemoteNotifications()
        application.applicationIconBadgeNumber = 0

    }

MARK: - DELEGATES FOR PUSH NOTIFICATIONS

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let installation = PFInstallation.current()
    installation?.setDeviceTokenFrom(deviceToken)
    installation?.saveInBackground(block: { (succ, error) in
        if error == nil {
            print("DEVICE TOKEN REGISTERED!")
        } else {
            print("\(error!.localizedDescription)")
        }
    })
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    print("application:didFailToRegisterForRemoteNotificationsWithError: %@", error)
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
    print("\(userInfo)")

    // PFPush.handle(userInfo)
    if application.applicationState == .inactive {
        PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(inBackground: userInfo, block: nil)
    }
}

Xcode 글꼴 크기 변경
admin
2019.05.25
조회 수 7097
Text field 숨기기
admin
2019.05.19
조회 수 4430
조회 수 5746
조회 수 4437
IOS Push php
admin
2019.04.20
조회 수 4655
조회 수 4875
조회 수 5521
조회 수 8339
조회 수 5584
Linphone rebuild
admin
2019.04.09
조회 수 4532
How to rename Xcode project
admin
2019.04.08
조회 수 5007
조회 수 5101
조회 수 4545
조회 수 5723
조회 수 5169
조회 수 5208
조회 수 4855
푸시 메시지 구성
admin
2019.04.06
조회 수 4483
조회 수 5466
조회 수 7292
조회 수 4685
조회 수 9923
조회 수 5297
조회 수 5028
조회 수 5209
조회 수 4954
조회 수 5629
조회 수 5441
Objective-C 입문
admin
2018.06.01
조회 수 5740
프로토콜
admin
2018.06.01
조회 수 5698
카테고리
admin
2018.06.01
조회 수 5629
메소드의 포인터
admin
2018.06.01
조회 수 5706
선택기
admin
2018.06.01
조회 수 5624
클래스 형
admin
2018.06.01
조회 수 5696
클래스 메소드
admin
2018.06.01
조회 수 5678
가시성
admin
2018.06.01
조회 수 5746
정적 형식
admin
2018.06.01
조회 수 5651
오브젝트의 해방
admin
2018.06.01
조회 수 12502
이니셜 라이저
admin
2018.06.01
조회 수 5851
재정
admin
2018.06.01
조회 수 10593
상속
admin
2018.06.01
조회 수 5761
메소드
admin
2018.06.01
조회 수 6181
조회 수 6337
가져 오기
admin
2018.06.01
조회 수 6205
Objective-C는?
admin
2018.06.01
조회 수 6230
조회 수 6340
조회 수 7588