[英]iOS How to Handle Parse JSON Push Notification when App is in Foreground
I have a very basic Webview app that I'm using Parse.com to send JSON push notifications to. 我有一个非常基本的Webview应用程序,正在使用Parse.com向其发送JSON推送通知。 I'm sending JSON push notification with a url to open in my web view like this: 我正在发送带有URL的JSON推送通知,如下所示:
{
"alert": "Push Title goes here",
"url": "http://www.google.com"
}
When I send the above message, if the app is closed or in the background it works by showing a banner notification and if you tap on the notification, it opens up our app and directs you to the specified url. 当我发送上述消息时,如果该应用程序关闭或在后台运行,则它会显示横幅通知,并且如果您点击通知,它将打开我们的应用程序并将您定向到指定的网址。
However, if the app is in the foreground, it just automatically loads the specified url with no interaction, which could be problematic if say they are in the middle of checking out and the pushed url takes them away from the checkout page unexpectedly. 但是,如果应用程序位于前台,则它会自动加载指定的url,而不会进行交互,如果说它们处于检出过程中并且推送的URL意外地使它们离开检出页面,则可能会出现问题。
Ideally, we'd like to have an alert pop up with the notification and the option to tap "view" to load the url, or to "close" which would just close the notification and allow the user to continue browsing the app where they were. 理想情况下,我们希望弹出一个带有通知的警报,并提供以下选项:点按“查看”以加载网址,或选择“关闭”,以关闭通知并允许用户继续浏览应用程序。是。
Here's what I currently have: 这是我目前拥有的:
AppDelegate.m 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];
}
}
and here is my ViewController.m 这是我的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];
}
I'll also note that this is my first iOS app, so my knowledge is very limited but I've gotten this far..any more help is much appreciated! 我还要注意,这是我的第一个iOS应用程序,所以我的知识非常有限,但是我已经走了这么远了。
You need to add an extra method to your AppDelegate to receive the notifications when your app is running in the foreground. 您需要在AppDelegate中添加其他方法,以便在应用程序在前台运行时接收通知。 From Parse documentation: 从解析文档中:
If your app is already running when the notification is received, the data is made available in the
application:didReceiveRemoteNotification:fetchCompletionHandler:
method through theuserInfo
dictionary. 如果收到通知时您的应用程序已经在运行,则可以通过userInfo
字典在application:didReceiveRemoteNotification:fetchCompletionHandler:
方法中使用该数据。
see: https://www.parse.com/docs/ios/guide#push-notifications-responding-to-the-payload 请参阅: https : //www.parse.com/docs/ios/guide#push-notifications-responding-to-the-payload
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.