我有一个非常基本的Webview应用程序,我正在使用Parse.com发送JSON推送通知。我正在发送带有网址的JSON推送通知,以便在我的网络视图中打开,如下所示:
{
"alert": "Push Title goes here",
"url": "http://www.google.com"
}
当我发送上述消息时,如果应用程序已关闭或在后台,它会显示横幅通知,如果您点按通知,则会打开我们的应用并指示您转到指定的网址。
但是,如果应用程序位于前台,它只会自动加载指定的网址而不进行交互,如果说它们正在检出并且推送的网址意外地将其从结帐页面带走,则可能会出现问题。
理想情况下,我们希望弹出一个包含通知的提醒,并选择点击“查看”以加载网址,或选择“关闭”,这将关闭通知并允许用户继续浏览应用程序他们在哪里。
以下是我目前的情况:
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[Parse setApplicationId:@"XXX"
clientKey:@"XXX"];
// Register for Push Notitications
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
// Override point for customization after application launch.
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
NSDictionary *notificationPayload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
NSString *pushURL = [notificationPayload objectForKey:@"url"];
if (notification) {
NSDictionary *aDict=[NSDictionary dictionaryWithObject: pushURL forKey:@"urlToLoad"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"LoadRequestFromAppDel" object:Nil userInfo:aDict];
}
return YES;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSString *pushURL = [userInfo objectForKey:@"url"];
if (userInfo) {
NSDictionary *aDict=[NSDictionary dictionaryWithObject: pushURL forKey:@"urlToLoad"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"LoadRequestFromAppDel" object:Nil userInfo:aDict];
}
}
这是我的 ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(LoadRequestFromAppDel:) name:@"LoadRequestFromAppDel" object:Nil];
NSAssert(self.back, @"Unconnected IBOutlet 'back'");
NSAssert(self.forward, @"Unconnected IBOutlet 'forward'");
NSAssert(self.refresh, @"Unconnected IBOutlet 'refresh'");
NSAssert(self.stop, @"Unconnected IBOutlet 'stop'");
NSAssert(self.webView, @"Unconnected IBOutlet 'webView'");
self.webView.delegate = self;
self.webView.scalesPageToFit = YES;
NSURL* url = [NSURL URLWithString:@"http://www.myurl.com"];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
[self updateButtons];
}
- (void)LoadRequestFromAppDel: (NSNotification*)aNotif
{
NSString *aStrUrl=[[aNotif userInfo] objectForKey:@"urlToLoad"];
NSURL* pushurl = [NSURL URLWithString:aStrUrl];
NSURLRequest* requestObj = [NSURLRequest requestWithURL:pushurl];
[self.webView loadRequest:requestObj];
[self updateButtons];
}
我还会注意到这是我的第一个iOS应用程序,所以我的知识非常有限,但我已经做到了这一点。非常感谢更多的帮助!
答案 0 :(得分:0)
您需要向AppDelegate添加额外的方法,以便在应用程序在前台运行时接收通知。从Parse文档:
如果您的应用在收到通知时已经在运行,那么 数据在
application:didReceiveRemoteNotification:fetchCompletionHandler:
方法通过userInfo
字典。
请参阅:https://www.parse.com/docs/ios/guide#push-notifications-responding-to-the-payload