【发布时间】:2014-03-06 12:21:51
【问题描述】:
我为我的应用设置了推送通知,这样当我点击推送通知时,应用会转到主控件视图。但是,我想根据添加到应用程序中的内容查看特定的视图控制器。我该怎么做?
我的应用委托代码。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeNone)];
return YES;
}
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
const char* data = [deviceToken bytes];
NSMutableString * token = [NSMutableString string];
for (int i = 0; i < [deviceToken length]; i++) {
[token appendFormat:@"%02.2hhX", data[i]];
}
NSString *urlString = [NSString stringWithFormat:@"url"?token=%@",token];
NSURL *url = [[NSURL alloc] initWithString:urlString];
NSLog(@"token %@",urlString);
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSLog(@"request %@ ",urlRequest);
NSData *urlData;
NSURLResponse *response;
urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil];
NSLog(@"data %@",urlData);
// NSLog(@"token ",sendUserToken);
}
我的 php 推送通知脚本。
<?php
token ="my token"
$payload = '{
"aps" :
{
"alert" :"'.$message.'",
"badge" : 1,
"sound" : "bingbong.aiff"
}
}';
$ctx = stream_context_create();
stream_context_set_option($ctx,'ssl', 'local_cert','ck.pem');
stream_context_set_option($ctx,'ssl','passphrase', 'balpad');
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195',$err,$errstr,60,STREAM_CLIENT_CONNECT,$ctx);
if(!fp){
print "Failed to connect $err $errstrn";
return;
}else{
print "notifications sent!";
}
$devArray = array();
$devArray[] = $deviceToken;
foreach($deviceToken as $token){
$msg = chr(0) . pack("n",32) . pack("H*", str_replace(' ',' ',$token)).pack("n",strlen($payload)) . $payload;
print "sending message:" .$payload . "n";
fwrite($fp,$msg);
}
fclose($fp);
}
?>
这是我第一次使用推送通知,但我还没有找到合适的解决方案。我找到了一些建议(link1link2),但我发现它们有点令人困惑,我没有任何想法。请有人指导我如何制作这个。
【问题讨论】:
标签: ios iphone objective-c notifications