我检查了我在GitHub和教程中找到的关于该主题的所有内容,但找不到合适的解决方案。
当我在移动推送网关tut中读到时,我可以发送一个字符串或类似的内容:
{
"aps" : {
"alert": "If you are reading this, you should have just received an alert.",
"badge": 9,
"sound": "bingbong.aiff"
}
}
所以我尝试将其作为NSString
,NSDictionary
和NSData
发送,但我无法收到。
我尝试的解决方案:
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];
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中使用此代码(与设置指南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];
}
}
我在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中做到这一点?
答案 0 :(得分:1)
事实证明,其他问题导致我的问题,所以我的一个实现是正确的。
这是一个合适的字典,您可以通过PubNub作为推送消息发送。
NSDictionary *myPushMessage = @{ @"aps" : @{ @"alert" : @"Push notification content" } };
PNChannel *pushReceiver = [PNChannel channelWithName:recevierChannel shouldObservePresence:NO];
[PubNub sendMessage: myPushMessage toChannel:pushReceiver];
如果您关注移动推送网关tutorial,它必须完美运行。