2014-07-14 3 views
1

GitHub 및 자습서에서 해당 항목에 대해 찾은 모든 항목을 검사했지만 올바른 해결 방법을 찾을 수 없습니다.APN에 대한 올바른 형식의 메시지

내가 게이트웨이 tut 모바일 푸시에서 읽을 때, 나는이 같은 문자열이나 뭐 보낼 수

:

{ 
     "aps" : { 
     "alert": "If you are reading this, you should have just received an alert.", 
     "badge": 9, 
     "sound": "bingbong.aiff" 
     } 
} 

그래서 NSString, NSDictionaryNSData로 보내려고했다,하지만 나는 '나오지 않았어 그것을 받으 라.

솔루션은 I 시도 :

A,

NSString *apns = [NSString stringWithFormat:@"hello world."]; 
[PubNub sendMessage: apns toChannel:channel_3]; 

B,

NSDictionary *dict = @{ 
         @"aps" : @{ @"alert" : @"new push" } 
         }; 
[PubNub sendMessage: dict toChannel:channel_3]; 

C,

// create the apns dictionary 
NSString *apns = [NSString stringWithFormat:@"hello world."]; 
NSDictionary *dict = @{ @"aps" : @{ @"alert" : apns } }; 


//create the json 
NSError *error; 
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict 
                options:NSJSONWritingPrettyPrinted 
                error:&error]; 

NSString* jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 
[PubNub sendMessage: jsonString toChannel:channel_3]; 

D를

,
NSString *apns = [NSString stringWithFormat:@"hello world."]; 
NSDictionary *dict = @{ @"aps" : @{ @"alert" : apns } }; 


//create the json 
NSError *error; 
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict 
                options:NSJSONWritingPrettyPrinted 
                error:&error]; 


[PubNub sendMessage: jsonData toChannel:channel_3]; 

전자,

NSDictionary *pushPub = [[NSDictionary alloc]init]; 
pushPub = @{ @"alert": someString} ; 
NSMutableDictionary *fullPush = [NSMutableDictionary dictionary]; 
[fullPush setObject:pusPub forKey:@"aps"]; 
[PubNub sendMessage: jsonData toChannel:channel_3]; 

나는 이러한 차별화 시도 할 수있는 것을 더 이상 아이디어가 없습니다. 그렇지 않으면 환영 메시지를 받았으므로 제대로 구현되었습니다.

두 iPhone에서이 코드를 테스트하고 AppDelegate에서이 코드를 사용합니다 (설정 가이드 verison과 동일).

// #5 Process received push notification 
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
{ 

    NSLog(@"PUSH TEST LOG"); 
    NSString *message = nil; 
    id alert = [userInfo objectForKey:@"aps"]; 
    if ([alert isKindOfClass:[NSString class]]) { 
     message = alert; 
    } else if ([alert isKindOfClass:[NSDictionary class]]) { 
     message = [alert objectForKey:@"alert"]; 
    } 
    if (alert) { 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:message 
                  message:message delegate:self 
                cancelButtonTitle:@"Thanks PubNub!" 
                otherButtonTitles:@"Send Me More!", nil]; 
     [alertView show]; 
    } 
} 

내가 'S'의 viewDidLoad '

[[PNObservationCenter defaultCenter] addClientConnectionStateObserver:self withCallbackBlock:^(NSString *origin, BOOL connected, PNError *connectionError){ 

     if (connected) 
     { 
      NSLog(@"OBSERVER: Successful Connection!"); 

      // Subscribe on connect 
      [PubNub subscribeOnChannels:channels]; 


      // #3 Define AppDelegate 
      AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 

      // #4 Pass the deviceToken from the Delegate 
      deviceToken = appDelegate.dToken; 

      // #5 Double check we've passed the token properly 
      NSLog(@"Device token received: %@", deviceToken); 

      // #6 If we have the device token, enable apns for our channel if it isn't already enabled. 
      if (deviceToken) { 

       // APNS enabled already? 
       [PubNub requestPushNotificationEnabledChannelsForDevicePushToken:deviceToken 
                withCompletionHandlingBlock:^(NSArray *channels, PNError *error){ 
                 if (channels.count == 0) 
                 { 
                  NSLog(@"BLOCK: requestPushNotificationEnabledChannelsForDevicePushToken: Channel: %@ , Error %@",channels,error); 

                  // Enable APNS on this Channel with deviceToken 
                  [PubNub enablePushNotificationsOnChannel:myPushChannel 
                        withDevicePushToken:deviceToken 
                      andCompletionHandlingBlock:^(NSArray *channel, PNError *error){ 
                       NSLog(@"BLOCK: enablePushNotificationsOnChannel: %@ , Error %@",channel,error); 
                      }]; 
                 } 
                }]; 
      } 
     } 
     else if (!connected || connectionError != nil) 
     { 
      NSLog(@"OBSERVER: Error %@, Connection Failed!", connectionError.localizedDescription); 
     } 

    }]; 

내가 콘솔 OBSERVER: Error %@, Connection Failed!"에서이 로그를보고 내 ViewController에서 이것을 사용은, 그러나 나는 또한 동일한 세션 PubNub client successfully subscribed on channels: desiredChannels 내에서이 있었다 그래서 나는 클라이언트를 생각한다 푸시 메시지를받는 채널을 구독 할 수 있으며 문제는 내 메시지와 관련이 있습니다. 아마도 누군가가 내게 올바른 예를 보여줄 수 있는데, obj-c에서 어떻게 할 수 있을까요?

답변

1

다른 문제가 내 문제를 야기 했으므로 구현 중 하나가 옳았습니다.

다음은 PubNub를 통해 푸시 메시지로 보낼 수있는 적절한 사전입니다.

NSDictionary *myPushMessage = @{ @"aps" : @{ @"alert" : @"Push notification content" } }; 

PNChannel *pushReceiver = [PNChannel channelWithName:recevierChannel shouldObservePresence:NO]; 

[PubNub sendMessage: myPushMessage toChannel:pushReceiver]; 

모바일 푸시 게이트웨이 tutorial을 따른 경우 완벽하게 작동해야합니다.

+1

귀하의 답변에 만족 하셨다면, 받아주십시오. –