我已经为我的应用设置了推送通知,以便当我点击推送通知时,应用会转到主控制视图。但是,我想查看特定的视图控制器,具体取决于我添加到我的应用程序的内容。我怎么能这样做?
我的应用委托代码。
- (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);
}
?>
这是我第一次使用推送通知而我没有找到适当的解决方案。我找到了一些建议(link1 link2),但我发现它们有点令人困惑,我没有得到任何想法。请有人指导我如何做到这一点。
答案 0 :(得分:1)
我有一个解决方案给你。请参考下面的示例代码:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
// Below Code Shows Message While Your Application is in Active State
NSString *cancelTitle = @"Ok";
NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"App Start"
message:message
delegate:nil
cancelButtonTitle:cancelTitle
otherButtonTitles: nil];
[alertView show];
[alertView release];
} else {
// Do stuff that you would do if the application was not active
// Please add your code to go to specific view controller here.
}
}
答案 1 :(得分:0)
好的,这样做:
在.php中添加另一个键,如:
...
{
"alert" :"'.$message.'",
"badge" : 1,
"sound" : "bingbong.aiff",
"condition" : "viewController1"
}
...
你可以在那里写下你想要的东西。这将告诉您在推送时想要显示的屏幕,它不需要是真实控制器的名称,只需要一些条件以便您可以改变通知
然后覆盖didReceiveRemoteNotification,如下所示:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
// Below Code Shows Message While Your Application is in Active State
NSString *strControllerToShow = [[userInfo valueForKey:@"aps"] valueForKey:@"condition"];;
if(condition != nil){
if([condition isEqualToString:@"viewController1"]){
// create vc
// set properties
// push it on navigation stack
}
if([condition isEqualToString:@"viewController2"]){
// create vc
// set properties
// push it on navigation stack
}
...
}
} else {
// Do stuff that you would do if the application was not active
// Please add your code to go to specific view controller here.
}
}
就是这样......
答案 2 :(得分:0)
每当我们收到推送通知时,我们会通过ios接收通知方法。
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;
In this method base on your requirement you can navigate on any view; suppose that you want to navigate on firstviewcontroller, so code is like that:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
// here you need to check in which view controller is right now on screen;
//1. if it is same in which you are then no problem just referesh controller;
//otherwise push to your view controller like following"
firstviewcontroller = [[firstviewcontroller alloc] initWithNibName:@"firstviewcontroller" bundle:nil];
[self.navigationController pushviewcontroller:firstviewcontroller animated:YES];
}