0
我想在收到推送通知時觸發數據提取。我知道這發生在通知顯示給用戶之前。現在我有這個在我的AppDelegate.m:APN後臺刷新,設置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];
}
}
將這項工作既顯示前推,然後處理來自一鍵打開應用程序像以前一樣或獲取的數據是有兩個單獨的方法,一個用於後臺刷新,另一個用於處理如何打開推送?任何指針將非常感激。謝謝!
你可以添加推送消息到APN,但不是嗎?即您可以擁有content-available = 1,並在獲取後將「發送給您的電話」消息發送給手機? 後臺刷新後我怎麼能得到它發佈我的一個通知,即[[NSNotificationCenter defaultCenter] postNotificationName:@「appRestorePush」object:nil];? – Kex
當然。但是,如果可用內容設置爲1,並且您的應用程序處於後臺/未運行狀態,則無法執行任何UI工作,但可以(未測試)顯示本地通知。 – Asaf