簡體   English   中英

當應用程序在iOS 10中處於后台時,未收到推送通知

[英]Push notification not received when App is in Background in iOS 10

我正在使用FCM(Firebase雲消息傳遞)在iOS中發送推送通知。

當App處於前台狀態時,我能夠收到通知。 但是當應用程序處於后台狀態時,不會收到通知。 每當應用程序進入前台狀態時,只有那時才會收到通知。

我的代碼是:

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
   willPresentNotification:(UNNotification *)notification
     withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
// Print message ID.
NSDictionary *userInfo = notification.request.content.userInfo;
NSLog(@"Message ID: %@", userInfo[@"gcm.message_id"]);

// Pring full message.
NSLog(@"%@", userInfo);

if( [UIApplication sharedApplication].applicationState == UIApplicationStateInactive )
{
    NSLog( @"INACTIVE" );
    completionHandler(UNNotificationPresentationOptionAlert);
}
else if( [UIApplication sharedApplication].applicationState == UIApplicationStateBackground )
{
    NSLog( @"BACKGROUND" );
    completionHandler( UNNotificationPresentationOptionAlert );
}
else
{
    NSLog( @"FOREGROUND" );
    completionHandler( UNNotificationPresentationOptionAlert );
}}



- (void)applicationDidEnterBackground:(UIApplication *)application {


}

當App處於后台狀態時:

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
   willPresentNotification:(UNNotification *)notification
     withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler

- 根本沒有被召喚。

我在應用程序功能的后台模式中啟用了推送通知和遠程通知。 但App仍未收到通知。

我提到了一些StackOverflow問題,但無法解決問題。 在iOS版本10中有什么要添加的或者我的代碼中有任何錯誤嗎?

對於iOS 10,我們需要調用以下2種方法。

對於FOREGROUND狀態

- (void)userNotificationCenter:(UNUserNotificationCenter *)center  willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler  
{  
    NSLog( @"Handle push from foreground" );  
    // custom code to handle push while app is in the foreground  
    NSLog(@"%@", notification.request.content.userInfo);
    completionHandler(UNNotificationPresentationOptionAlert);
} 

對於背景狀態

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler  
{  
    NSLog( @"Handle push from background or closed" );  
    // if you set a member variable in didReceiveRemoteNotification, you  will know if this is from closed or background  
    NSLog(@"%@", response.notification.request.content.userInfo);
    completionHandler();
}  

在此之前,我們必須添加UserNotifications框架並導入AppDelegate.h文件

#import <UserNotifications/UserNotifications.h>  
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>  

我們與客戶進行了多次測試,並對該主題進行了大量研究。 iOS 10.x,特別是10.3.x在處理和顯示推送方面存在問題。 解決問題的唯一方法是升級到更新的iOS版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM