gpt4 book ai didi

iOS 7 通过点击推送通知查看特定的View Controller

转载 作者:行者123 更新时间:2023-11-29 12:51:56 25 4
gpt4 key购买 nike

我已经为我的应用程序设置了一个推送通知,这样当我点击推送通知时,应用程序就会转到主控件 View 。但是,我想根据添加到我的应用程序的内容查看特定的 View Controller 。我该怎么做?

我的应用委托(delegate)代码。

     - (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 ),但我发现它们有点令人困惑,而且我没有任何想法。请有人指导我如何制作这个。

最佳答案

我有一个解决方案给你。请引用下面我的示例代码:

-(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.
}
}

关于iOS 7 通过点击推送通知查看特定的View Controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22224379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com