3 回答

TA貢獻(xiàn)1886條經(jīng)驗(yàn) 獲得超2個(gè)贊
您的應(yīng)用需要處理所有可能的推送通知傳遞狀態(tài):
您的應(yīng)用剛剛啟動(dòng)
您的應(yīng)用只是從后臺(tái)引入到前臺(tái)
您的應(yīng)用已經(jīng)在前臺(tái)運(yùn)行
您不必在交付時(shí)選擇使用哪種呈現(xiàn)方法來(lái)呈現(xiàn)推送通知,該呈現(xiàn)方法已編碼在通知本身中(可選的警報(bào),證件編號(hào),聲音)。但是,由于您大概可以控制應(yīng)用程序和推送通知的有效負(fù)載,因此您可以在有效負(fù)載中指定是否已經(jīng)向用戶(hù)顯示了警報(bào)視圖和消息。僅在應(yīng)用程序已經(jīng)在前臺(tái)運(yùn)行的情況下,您才知道用戶(hù)不僅通過(guò)警報(bào)或定期從主屏幕啟動(dòng)您的應(yīng)用程序。
您可以使用以下代碼在didReceiveRemoteNotification中判斷您的應(yīng)用是否剛剛出現(xiàn)在前臺(tái):
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if ( application.applicationState == UIApplicationStateActive )
// app was already in the foreground
else
// app was just brought from background to foreground
...
}

TA貢獻(xiàn)1788條經(jīng)驗(yàn) 獲得超4個(gè)贊
傳遞content-available = 1您的有效負(fù)載,didReceiveRemoteNotification甚至在后臺(tái)調(diào)用。例如
{
"alert" : "",
"badge" : "0",
"content-available" : "1",
"sound" : ""
}

TA貢獻(xiàn)1900條經(jīng)驗(yàn) 獲得超5個(gè)贊
當(dāng)應(yīng)用程序在后臺(tái)運(yùn)行時(shí),您必須做一些事情才能管理收到的推送通知。
首先,在服務(wù)器端,您必須設(shè)置{"aps":{"content-available" : 1... / $body['aps']['content-available'] =1;推送通知有效負(fù)載。
其次,在您的Xcode項(xiàng)目中,您必須簡(jiǎn)化“遠(yuǎn)程通知”。通過(guò)轉(zhuǎn)到項(xiàng)目的目標(biāo)->功能,然后啟用功能切換并選中“遠(yuǎn)程通知”復(fù)選框來(lái)完成此操作。
第三,不必使用,而didReceiveRemoteNotification必須調(diào)用 application:didReceiveRemoteNotification:fetchCompletionHandler:,這將使您能夠在收到通知時(shí)在后臺(tái)執(zhí)行所需的任務(wù):
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
if(application.applicationState == UIApplicationStateInactive) {
NSLog(@"Inactive - the user has tapped in the notification when app was closed or in background");
//do some tasks
[self manageRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
else if (application.applicationState == UIApplicationStateBackground) {
NSLog(@"application Background - notification has arrived when app was in background");
NSString* contentAvailable = [NSString stringWithFormat:@"%@", [[userInfo valueForKey:@"aps"] valueForKey:@"content-available"]];
if([contentAvailable isEqualToString:@"1"]) {
// do tasks
[self manageRemoteNotification:userInfo];
NSLog(@"content-available is equal to 1");
completionHandler(UIBackgroundFetchResultNewData);
}
}
else {
NSLog(@"application Active - notication has arrived while app was opened");
//Show an in-app banner
//do tasks
[self manageRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
}
最后,您必須UIRemoteNotificationTypeNewsstandContentAvailability在設(shè)置通知類(lèi)型時(shí)將其添加到通知設(shè)置中。
除此之外,如果在通知到達(dá)時(shí)您的應(yīng)用已關(guān)閉,則您必須在中進(jìn)行管理didFinishLaunchingWithOptions,并且即使用戶(hù)點(diǎn)擊了推送通知也是如此:
if (launchOptions != nil)
{
NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil)
{
NSLog(@"Launched from push notification: %@", dictionary);
[self manageRemoteNotification:dictionary];
}
}
當(dāng)您通過(guò)點(diǎn)擊推送通知啟動(dòng)應(yīng)用程序時(shí),launchOptions為!= nil,如果您通過(guò)點(diǎn)擊圖標(biāo)訪(fǎng)問(wèn)應(yīng)用程序,launchOptions將為== nil。
- 3 回答
- 0 關(guān)注
- 1584 瀏覽
添加回答
舉報(bào)