2013-08-05 3 views
1

방금이 튜토리얼 Push notification을 따라했으며 내 iPhone 앱에 대한 푸시 알림을 성공적으로 구현했습니다. 이제 알림 세부 정보를 얻을 수 있습니다. 그러나 alertBody 알림에 제공된 Label에 알림 alertBody를 넣고 싶습니다.원격 푸시 알림에서 내 앱의 notification.alertBody를 가져 오는 방법은 무엇입니까?

로컬 알림에서 alertBody 알림을 표시하는 코드가 있습니다. 하지만 푸시 알림은 로컬 알림에만 사용되기 때문에 푸시 알림과는 다릅니다. 내 ViewController.m

- (void)viewDidLoad{ 

[super viewDidLoad];  

[[NSNotificationCenter defaultCenter] addObserverForName:@"Notification" object:nil queue:nil usingBlock:^(NSNotification *note) 
NSString *_string = note.object; 
//Do something with the string-------- 
}]; 

} 

그것은 지역 알림에 있지만, 푸시 알림을 완벽하게 작동 내 AppDelagate.m

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif { 
NSLog(@"Recieved Notification %@",notif); 
NSString *_stringFromNotification = notif.alertBody; 
[[NSNotificationCenter defaultCenter] postNotificationName:@"Notification" object:_stringFromNotification]; 
} 

, 그것은 작동하지 않습니다. 이것을 구현하는 방법? 도와주세요. 레이블 또는 문자열에 알림 경고 본문을 넣어야합니다.

답변

0

원격 알림은 앱이 실행되는 샌드 박스 외부에서 실행되므로 로컬 알림과 동일한 방식으로 알림을 캡처 할 수 없습니다 (예 : application:didReceiveLocalNotification). 응용 프로그램이 원격 통지를 통해 시작되는 경우 원격 통지를, 응용 프로그램 대리인의 – application:didReceiveRemoteNotification: 메서드가 호출됩니다받을 때 응용 프로그램이 이미 실행중인 경우, 당신은 application:didFinishLaunchingWithOptions

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]; 

    if (notification) { 
     // do something with the notification.alertBody 
    } else { 
     // from the springboard 
    } 
} 
0

를 통해 알림을 캡처 할 수 있습니다; 응용 프로그램이 현재 실행 중이 아니고 알림에 대한 응답으로 시작되는 경우 원격 알림 정보는 – application:didFinishLaunchingWithOptions: 메서드의 launchOptions 사전에 입력됩니다.

0

구현중인 방법은 지역 알림 용입니다. 푸시 알림을 처리하려면 메서드

- (void)application:(UIApplication*)application didReceiveRemoteNotification: (NSDictionary*)userInfo{ 
NSLog(@"Received notification: %@", userInfo); 

} 

을 사용해야합니다. 앱이 배경에만있는 경우이 메소드가 호출됩니다. 응용 프로그램이 배경에 없으면 당신은 다음과 같은 방식으로

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
UILocalNotification *notificationData = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]; 

if(!notificationData){ 
    NSLog(@"App launched by tapping on app icon"); 
}else{ 
    NSLog(@"Notification data -> %@",notificationData); 
} 
} 
3
first of all register for remote notifications in AppDelegate.m in method, 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
//Invoke APNS. 
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes: 
    (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; 
} 

And then use following delegate method to recieve remote notification: 
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { 
    NSLog(@"Received =%@",userInfo);////userInfo will contain all the details of notification like alert body. 
} 
에서 데이터를 가져올 수 있습니다
관련 문제