2017-12-15 6 views
1

내 앱에 로컬 및 원격 알림을 표시하고 있습니다. 이제 알림이 로컬 인 경우 다른 작업을 수행하려는 경우 또는 알림이 원격이면 다른 작업을 수행하려는 시나리오가 있습니다.사용자가 조치 로컬 통지 또는 원격 통지를 받았는지 여부를 감지하는 방법은 무엇입니까?

까지 iOS 9 아래의 코드를 사용하여 알림이 로컬인지 원격인지 확인했습니다.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 


    if launchOptions?[UIApplicationLaunchOptionsKey.localNotification] != nil { 
     // Do what you want to happen when a remote notification is tapped. 
    } 
    return true 
    } 

그러나 iOS 10에서는이 방법이 사용되지 않으므로 알림이 로컬인지 원격인지 확인하는 방법은 무엇입니까?

당신이 튜토리얼을 here

를 참조 애플 문서에 대한 수 UNUserNotificationCenter

를 사용할 필요가 지역 알림 10 + iOS 용

+0

확인이 : https://useyourloaf.com/blog/local-notifications-with-ios-10/ 당신은 내가 업데이트 한 내 대답을 확인할 수 있습니다 –

+0

. 희망이 도움이 될 것입니다. – ivarun

답변

0

당신은 아래 링크를 참조 할 수 있습니다

+0

나는 이미 UNUserNotificationCenter를 사용했다. 로컬 알림 또는 원격 알림을 통해 구분 된 앱을 시작한 것을 어떻게 알 수 있습니까? – Techiee

0

here을 참조하지만. 당신은 당신의 대답을 얻을 것입니다. 주시기 바랍니다없는 경우 나 알고 : 알림 범주에 대한

Local and Push Notifications in IOS 9 and 10 using swift3

추가 정보는 :

당신이 통지 객체 식별자를 할당 할 수 있습니다 로컬 및 원격 통지를 식별합니다. UIMutableUserNotificationCategory은 iOS 10에서 사용되지 않으므로 UNNotificationCategory을 사용하여 알림에 식별자를 할당하십시오. UNMutableNotificationContent에서

https://developer.apple.com/documentation/uikit/uimutableusernotificationcategory/ https://developer.apple.com/documentation/usernotifications/unnotificationcategory

3

같은 아래에 userInfo 추가 : 아래와 같이 didFinishLaunchingWithOptionsUNUserNotificationCenterDelegate 설정 AppDelegate 지금

content.userInfo = ["isLocalNotification" : true] 

통지에 식별자를 할당하기위한 링크 아래 참조 :

UNUserNotificationCenter.current().delegate = self 

그런 didReceive response 방법 UNUserNotificationCenterDelegate의 구현 :

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 

     print(response.notification.request.content.userInfo) 
    } 

출력은 다음과 같은 것을 얻을 것이다 :

[AnyHashable ("isLocalNotification을") : 1]

당신이 할 수있는 로컬 알림을 isLocalNotification 키로 식별하십시오.

2

UserNotifications 프레임 워크를 사용하는 iOS 10 이상.

사용자가 알림에 대해 조치를 취했는지 확인합니다. UNUserNotificationCenterDelegate을 준수하고 구현할 수 있습니다. userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:

원격 알림인지 확인하려면 다음을 수행하십시오. 원격 알림 트리거가 UNPushNotificationTrigger이므로 trigger 유형을 확인할 수 있습니다.

여기 ... 코드의

#pragma mark - UNUserNotificationCenterDelegate 

- (void)userNotificationCenter:(UNUserNotificationCenter *)center 
didReceiveNotificationResponse:(UNNotificationResponse *)response 
     withCompletionHandler:(void (^)(void))completionHandler { 

    if ([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { 
     // User did tap at remote notification 
    } 

    completionHandler(); 
} 
관련 문제