在我的 iOS 应用程序中,我有一个 UIWebView
所以我如何在 UIWebView
中打开指定的 URL
,使用 推送通知
?
如果有人用notification
打开应用程序,我想在
UIWebView
。
我可以将URL
(在后台)与推送通知
绑定(bind)吗?
谢谢。
最佳答案
根据苹果...
If the app is running and receives a remote notification, the app calls this method to process the notification. Your implementation of this method should use the notification to take an appropriate course of action. ... If the app is not running when a push notification arrives, the method launches the app and provides the appropriate information in the launch options dictionary. The app does not call this method to handle that push notification. Instead, your implementation of the
application:willFinishLaunchingWithOptions:
orapplication:didFinishLaunchingWithOptions:
method needs to get the push notification payload data and respond appropriately.
因此,存在三种可能的情况:
1) 应用程序在前台:您将拥有完全的控制权,只需实现 didReceiveNotification
并执行任何您想做的事情。
2) 应用正在运行,但在后台:直到用户使用收到的通知实际打开您的应用时,才会触发操作。
3) 应用未运行:在这种情况下,您应该实现 didFinishLaunchingWithOptions
以获取附加信息并执行任务。
所以 didFinishLaunchingWithOptions
的代码应该是这样的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *userInfo = [launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
if(apsInfo) {
// Get the URL or any other data
}
}
这是 didReceiveNotification
的近似值
/**
* Remote Notification Received while application was open.
*/
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
#if !TARGET_IPHONE_SIMULATOR
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive)
{
NSString *message = nil;
id aps = [userInfo objectForKey:@"aps"];
if ([aps isKindOfClass:[NSDictionary class]]) {
message = [aps objectForKey:@"alert"];
}
if (message) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Notificación"
message:message
delegate:self
cancelButtonTitle:@"Aceptar"
otherButtonTitles:nil, nil];
[alertView show];
}
}
// Aditional data
NSString *url = [userInfo objectForKey:@"url"];
NSLog(@"Received Push URL: %@", url);
if(url!=nil)
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
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);
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
NSString *badge = [apsInfo objectForKey:@"badge"];
NSLog(@"Received Push Badge: %@", badge);
application.applicationIconBadgeNumber = [[apsInfo objectForKey:@"badge"] integerValue];
#endif
}
关于ios - 在 Webview 中打开指定的 URL(iOS 推送),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23387157/