2014-07-14 83 views
1

我检查了我在GitHub和教程中找到的有关该主题的所有内容,但找不到正确的解决方案。为APNs正确格式化的邮件

正如我在移动推送网关tut阅读,我可以给一个字符串或者是这样的:

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

所以我试着将它发送的NSStringNSDictionaryNSData,但我不能”收到它。

的溶液我尝试:

一个,

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]; 

E,

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中使用此代码(与安装指南版本相同)。

// #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]; 
    } 
} 

而且我用这个在我ViewController的‘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!",但是我也有这个相同的会话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

如果您对答复感到满意,请接受它。 –