2

我一直試圖在ios應用中獲得長時間的Firebase推送通知。我已經嘗試了互聯網上找到的所有東西。但可悲的是沒有運氣。 任何幫助,將不勝感激。在iOS中未收到FCM通知

我通過Firebase控制檯發送通知。

這裏是我的代碼:

#import "AppDelegate.h" 

#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 
@import UserNotifications; 
#endif 

@import Firebase; 
@import FirebaseInstanceID; 
@import FirebaseMessaging; 

#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 
@interface AppDelegate() <UNUserNotificationCenterDelegate, FIRMessagingDelegate> 
@end 
#endif 

@implementation AppDelegate 


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    // Override point for customization after application launch. 

    // [START configure_firebase] 
    [FIRApp configure]; 
    // [END configure_firebase] 

    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) { 
     UIUserNotificationType allNotificationTypes = 
     (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge); 
     UIUserNotificationSettings *settings = 
     [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil]; 
     [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; 
    } else { 
     // iOS 10 or later 
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 
     UNAuthorizationOptions authOptions = 
     UNAuthorizationOptionAlert 
     | UNAuthorizationOptionSound 
     | UNAuthorizationOptionBadge; 
     [[UNUserNotificationCenter currentNotificationCenter] 
     requestAuthorizationWithOptions:authOptions 
     completionHandler:^(BOOL granted, NSError * _Nullable error) { 
     } 
     ]; 

     // For iOS 10 display notification (sent via APNS) 
     [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self]; 
     // For iOS 10 data message (sent via FCM) 
     [[FIRMessaging messaging] setRemoteMessageDelegate:self]; 
#endif 
    } 

    [[UIApplication sharedApplication] registerForRemoteNotifications]; 

    // Add observer for InstanceID token refresh callback. 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshNotification:) 
               name:kFIRInstanceIDTokenRefreshNotification object:nil]; 

    return YES; 
} 

// [START refresh_token] 
- (void)tokenRefreshNotification:(NSNotification *)notification { 
    // Note that this callback will be fired everytime a new token is generated, including the first 
    // time. So if you need to retrieve the token as soon as it is available this is where that 
    // should be done. 
    NSString *refreshedToken = [[FIRInstanceID instanceID] token]; 
    NSLog(@"InstanceID token: %@", refreshedToken); 

    NSLog(@"id = ---------- %@",[[[UIDevice currentDevice] identifierForVendor] UUIDString]); 


    [[NSNotificationCenter defaultCenter] postNotificationName:@"fcmtoken" object:refreshedToken]; 


    // Connect to FCM since connection may have failed when attempted before having a token. 
    [self connectToFcm]; 

    // TODO: If necessary send token to application server. 
} 
// [END refresh_token] 

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { 
    // If you are receiving a notification message while your app is in the background, 
    // this callback will not be fired till the user taps on the notification launching the application. 
    // TODO: Handle data of notification 

    // Print message ID. 
    NSLog(@"Message ID: %@", userInfo[@"gcm.message_id"]); 

    // Print full message. 
    NSLog(@"didReceiveRemoteNotification= %@", userInfo); 
} 

// [START ios_10_message_handling] 
// Receive displayed notifications for iOS 10 devices. 
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 
- (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"]); 

    // Print full message. 
    NSLog(@"willPresentNotification: %@", userInfo); 
} 

// Receive data message on iOS 10 devices. 
- (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage { 
    // Print full message 
    NSLog(@"message Recived = %@", [remoteMessage appData]); 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ROFL" 
                message:@"Dee dee doo doo." 
                delegate:self 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil]; 
    [alert show]; 

} 
#endif 

// [START connect_to_fcm] 
- (void)connectToFcm { 
    [[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) { 
     if (error != nil) { 
      NSLog(@"Unable to connect to FCM. %@", error); 
     } else { 
      NSLog(@"Connected to FCM."); 
     } 
    }]; 
} 
// [END connect_to_fcm] 

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 

    NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken"); 
    // for development 
    [[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeSandbox]; 

    // for production 
    //  [[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeProd]; 


} 

- (void)applicationWillResignActive:(UIApplication *)application { 
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 
} 

- (void)applicationDidEnterBackground:(UIApplication *)application { 
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 

    //[[FIRMessaging messaging] disconnect]; 
    // NSLog(@"Disconnected from FCM"); 
} 

- (void)applicationWillEnterForeground:(UIApplication *)application { 
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 
} 

- (void)applicationDidBecomeActive:(UIApplication *)application { 
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 

    [self connectToFcm]; 
} 

- (void)applicationWillTerminate:(UIApplication *)application { 
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 
} 

@end 

在日誌中我能夠看到令牌爲我的設備產生的,還當我通知發送到令牌應用程序沒有收到通知,當它在後臺。

但是,當我啓動我的應用程序時,它調用applicationReceivedRemoteMessage:,我可以看到令人不快的警報。

有什麼問題請提前幫助,謝謝!

+0

在有效載荷組優先級爲高 –

+0

測試,但仍然沒有幫助:( – Krunal

+0

發表您的有效載荷 –

回答

0

當您的APP進入後臺時,通知消息會通過Apple APNS服務發送,所以我認爲您的.cer是錯誤的;蘋果有兩個.cer在編碼/ AppStore中推送通知,如果你的APP沒有進入AppStore,請嘗試使用developerment .cer來做。

我的英語不好,但可能對你有幫助!

====編輯==== 試試這個嗎? >性能 - -

NSString *pushToken = [[[[deviceToken description]

      stringByReplacingOccurrencesOfString:@"<" withString:@""] 
         stringByReplacingOccurrencesOfString:@">" withString:@""] 
         stringByReplacingOccurrencesOfString:@" " withString:@""]; 
+0

我用發展.CER並添加到我的provisioning profile。 – Krunal

+0

@Krunal neen」 t發送.p12到Firebase? – darkThanBlack

+0

已上傳.p12到firebase太 – Krunal

0

對不起possting的問題,但它可能會幫助別人......我通過啓用推送通知的能力在Xcode上.xcodeproj 8.1

點擊解決這個問題>啓用推送通知

我重新創建了provisiong配置文件。

3

這發生在我的項目的原因是因爲我使用的是相同的設備令牌與我的發展產生分貝......原來我需要刪除並重新安裝應用程序我的手機上,並註冊通知獲取新的設備令牌並生成新的FCM註冊ID ..工作,我成功地收到我的通知。

希望這會有所幫助。

+0

你救了我的命 – Edmund