gpt4 book ai didi

iphone - 重定向 UIAlertView 按钮推送通知

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:45:47 25 4
gpt4 key购买 nike

我看了很多关于这个问题的帖子,但没有一个能真正帮助我。这似乎很容易做到,但不知何故我想不通。

我想在这里做什么?好吧,我向我的应用程序发送推送通知。一切正常。当它在后台、不活动甚至应用程序未运行时,我都可以处理它。但是另一种状态——应用程序在前台——让我很头疼。

我有正在使用的代码片段。一切都很好。当应用程序在前台运行时,用户将获得 UIAlertview。但是如何向 View 按钮添加一个 Action 呢?我的意思是这个特定的 Action :当有人点击查看按钮时,他/她将被重定向到已通过推送通知传递的 url。

就像我之前在代码片段中使用的一样(准确地说是这部分):

NSString *notifUrl = [apsInfo objectForKey:@"url"];
NSLog(@"Received Push Url: %@", notifUrl);

我希望这是有道理的,并且有人可以帮助我。我迷路了……这是与该主题相关的完整代码片段。

-(void)Redirect:(NSString*)url{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
NSLog(@"%@",url);
}

/**
* Remote Notification Received while application was open.
*/
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

#if !TARGET_IPHONE_SIMULATOR

NSLog(@"remote notification: %@",[userInfo description]);
NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];

NSString *alert = [apsInfo objectForKey:@"alert"];
NSLog(@"Received Push Alert: %@", alert);

NSString *sound = [apsInfo objectForKey:@"sound"];
NSLog(@"Received Push Sound: %@", sound);
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);

NSString *badge = [apsInfo objectForKey:@"badge"];
NSLog(@"Received Push Badge: %@", badge);
application.applicationIconBadgeNumber = [[apsInfo objectForKey:@"badge"] integerValue];

NSString *notifUrl = [apsInfo objectForKey:@"url"];
NSLog(@"Received Push Url: %@", notifUrl);

// app was already in the foreground
if ( application.applicationState == UIApplicationStateActive ) {

UIAlertView *alertPush = [[UIAlertView alloc]initWithTitle: @"Message"
message: alert
delegate: self
cancelButtonTitle:@"View"
otherButtonTitles: @"Cancel", nil];

[alertPush show];
[alertPush release];
}
// app is inactive or in the background
else {
[self Redirect:notifUrl];
}

#endif
}

// what to do if user taps the viewbutton in push notification alert view
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 0) {

NSLog(@"View Button has been tapped");
// We need the url that has been passed by the push notification


}
else {
// Do something
}
}

最佳答案

只需在 .h 文件中定义变量 notifUrl,这样您也可以在类的其他方法中访问它。然后将 url 存储在该变量中,并在 alertview 委托(delegate)方法中使用它。

//Add this in .h file
NSString *notifUrl;

//Add this in .m file

-(void)Redirect:(NSString*)url{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
NSLog(@"%@",url);
}

/**
* Remote Notification Received while application was open.
*/
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

#if !TARGET_IPHONE_SIMULATOR

NSLog(@"remote notification: %@",[userInfo description]);
NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];

NSString *alert = [apsInfo objectForKey:@"alert"];
NSLog(@"Received Push Alert: %@", alert);

NSString *sound = [apsInfo objectForKey:@"sound"];
NSLog(@"Received Push Sound: %@", sound);
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);

NSString *badge = [apsInfo objectForKey:@"badge"];
NSLog(@"Received Push Badge: %@", badge);
application.applicationIconBadgeNumber = [[apsInfo objectForKey:@"badge"] integerValue];

//Define this variable in .h file so that you can access this in other methods as well
notifUrl = [apsInfo objectForKey:@"url"];
NSLog(@"Received Push Url: %@", notifUrl);
[notifUrl retain]; // this is retain value of notifUrl so that you can use it later
// app was already in the foreground
if ( application.applicationState == UIApplicationStateActive ) {

UIAlertView *alertPush = [[UIAlertView alloc]initWithTitle: @"Message"
message: alert
delegate: self
cancelButtonTitle:@"View"
otherButtonTitles: @"Cancel", nil];

[alertPush show];
[alertPush release];
}
// app is inactive or in the background
else {
[self Redirect:notifUrl];
}

#endif
}

// what to do if user taps the viewbutton in push notification alert view
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 0) {

NSLog(@"View Button has been tapped");
NSLog(@"Received Push Url: %@", notifUrl);
[self Redirect:notifUrl];
}
else {
// Do something
}
}

关于iphone - 重定向 UIAlertView 按钮推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18764038/

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