3 回答

TA貢獻1886條經驗 獲得超2個贊
您的應用需要處理所有可能的推送通知傳遞狀態:
您的應用剛剛啟動
您的應用只是從后臺引入到前臺
您的應用已經在前臺運行
您不必在交付時選擇使用哪種呈現方法來呈現推送通知,該呈現方法已編碼在通知本身中(可選的警報,證件編號,聲音)。但是,由于您大概可以控制應用程序和推送通知的有效負載,因此您可以在有效負載中指定是否已經向用戶顯示了警報視圖和消息。僅在應用程序已經在前臺運行的情況下,您才知道用戶不僅通過警報或定期從主屏幕啟動您的應用程序。
您可以使用以下代碼在didReceiveRemoteNotification中判斷您的應用是否剛剛出現在前臺:
- (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貢獻1788條經驗 獲得超4個贊
傳遞content-available = 1您的有效負載,didReceiveRemoteNotification甚至在后臺調用。例如
{
"alert" : "",
"badge" : "0",
"content-available" : "1",
"sound" : ""
}

TA貢獻1900條經驗 獲得超5個贊
當應用程序在后臺運行時,您必須做一些事情才能管理收到的推送通知。
首先,在服務器端,您必須設置{"aps":{"content-available" : 1... / $body['aps']['content-available'] =1;推送通知有效負載。
其次,在您的Xcode項目中,您必須簡化“遠程通知”。通過轉到項目的目標->功能,然后啟用功能切換并選中“遠程通知”復選框來完成此操作。
第三,不必使用,而didReceiveRemoteNotification必須調用 application:didReceiveRemoteNotification:fetchCompletionHandler:,這將使您能夠在收到通知時在后臺執行所需的任務:
- (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在設置通知類型時將其添加到通知設置中。
除此之外,如果在通知到達時您的應用已關閉,則您必須在中進行管理didFinishLaunchingWithOptions,并且即使用戶點擊了推送通知也是如此:
if (launchOptions != nil)
{
NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil)
{
NSLog(@"Launched from push notification: %@", dictionary);
[self manageRemoteNotification:dictionary];
}
}
當您通過點擊推送通知啟動應用程序時,launchOptions為!= nil,如果您通過點擊圖標訪問應用程序,launchOptions將為== nil。
- 3 回答
- 0 關注
- 1587 瀏覽
添加回答
舉報