APN后台刷新,正在设置AppDelegate.m
APN background refresh, setting up AppDelegate.m
我想在收到推送通知时触发数据提取。我知道这是在向用户显示通知之前发生的。现在我的 AppDelegate.m:
里有这个
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground )
{
//restore push
[[NSNotificationCenter defaultCenter] postNotificationName:@"appRestorePush" object:nil];
}
else {
//push for when the app is open
self.pushString = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"appOpenPush" object:nil];
}
}
这会控制应用程序的操作,具体取决于是在应用程序在前台打开时收到通知,还是通过推送通知打开应用程序。要添加对后台刷新的支持,我只需将此方法更改为 ?:
void didReceiveRemoteNotification (UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
if([content-available]) {
// fetch content methods here
completionHandler (UIBackgroundFetchResult.NewData);
}
if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground )
{
//restore push
[[NSNotificationCenter defaultCenter] postNotificationName:@"appRestorePush" object:nil];
}
else {
//push for when the app is open
self.pushString = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"appOpenPush" object:nil];
}
}
这是否适用于在显示推送之前获取数据,然后像以前一样处理从推送打开应用程序,或者是否有两种不同的方法,一种用于后台刷新,一种用于处理推送的打开方式?任何指针将不胜感激。谢谢!
首先,您的第二个代码块是 swift-Objective-C 的组合。所以我假设您使用 Objective-C.
进行开发
所以,相关方法如下:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler {
if (application.applicationState == UIApplicationStateInactive) {
// Application is inactive, fetch new data and call the completion handler.
completionHandler(UIBackgroundFetchResultNewData);
} else if (application.applicationState == UIApplicationStateBackground) {
// Application is in background, fetch new data and call the completion handler.
completionHandler(UIBackgroundFetchResultNewData);
} else {
// Application is active, so to what you need
completionHandler(UIBackgroundFetchResultNewData);
}
}
如果您在通知对象中发送 content-available
= 1(在 aps
对象内),则不会向用户显示通知。此参数告诉 OS 您不想向用户显示新信息,而只想从您的服务器获取新数据。
如果您不发送此参数,那么 OS 会将其视为常规通知,并且只有在用户点击显示的通知后才会调用上述方法(除非应用处于活动状态, 并且不会显示通知,并且会立即调用此方法)。
我想在收到推送通知时触发数据提取。我知道这是在向用户显示通知之前发生的。现在我的 AppDelegate.m:
里有这个- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground )
{
//restore push
[[NSNotificationCenter defaultCenter] postNotificationName:@"appRestorePush" object:nil];
}
else {
//push for when the app is open
self.pushString = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"appOpenPush" object:nil];
}
}
这会控制应用程序的操作,具体取决于是在应用程序在前台打开时收到通知,还是通过推送通知打开应用程序。要添加对后台刷新的支持,我只需将此方法更改为 ?:
void didReceiveRemoteNotification (UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
if([content-available]) {
// fetch content methods here
completionHandler (UIBackgroundFetchResult.NewData);
}
if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground )
{
//restore push
[[NSNotificationCenter defaultCenter] postNotificationName:@"appRestorePush" object:nil];
}
else {
//push for when the app is open
self.pushString = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"appOpenPush" object:nil];
}
}
这是否适用于在显示推送之前获取数据,然后像以前一样处理从推送打开应用程序,或者是否有两种不同的方法,一种用于后台刷新,一种用于处理推送的打开方式?任何指针将不胜感激。谢谢!
首先,您的第二个代码块是 swift-Objective-C 的组合。所以我假设您使用 Objective-C.
进行开发所以,相关方法如下:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler {
if (application.applicationState == UIApplicationStateInactive) {
// Application is inactive, fetch new data and call the completion handler.
completionHandler(UIBackgroundFetchResultNewData);
} else if (application.applicationState == UIApplicationStateBackground) {
// Application is in background, fetch new data and call the completion handler.
completionHandler(UIBackgroundFetchResultNewData);
} else {
// Application is active, so to what you need
completionHandler(UIBackgroundFetchResultNewData);
}
}
如果您在通知对象中发送 content-available
= 1(在 aps
对象内),则不会向用户显示通知。此参数告诉 OS 您不想向用户显示新信息,而只想从您的服务器获取新数据。
如果您不发送此参数,那么 OS 会将其视为常规通知,并且只有在用户点击显示的通知后才会调用上述方法(除非应用处于活动状态, 并且不会显示通知,并且会立即调用此方法)。