2017-08-05 24 views
0

私はFCMに開発証明書とプロダクション証明書をアップロードし、機能でプッシュ通知を有効にし、プロジェクトにGoogleService-Info.plistを配置しました。 私は別のGoogleアカウントを作成しようとしましたが、すべての手順をやり直してみましたが、何も役に立たなかった。FirebaseからiOSアプリケーションへのプッシュ通知を受信して​​いません

Iは、単一のデバイストークンに通知を送信しようとしたが、それは以下

私が試みコードである「無効な登録token.checkトークン形式」を示す拒否されます。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

    NSError *configureError; 
[[GGLContext sharedInstance] configureWithError:&configureError]; 
NSAssert(!configureError, @"Error configuring Google services: %@", configureError); 



GAI *gai = [GAI sharedInstance]; 
gai.trackUncaughtExceptions = YES; 
gai.logger.logLevel = kGAILogLevelVerbose; 

if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) { 

#pragma clang diagnostic push 
#pragma clang diagnostic ignored "-Wdeprecated-declarations" 
    UIRemoteNotificationType allNotificationTypes = 
    (UIRemoteNotificationTypeSound | 
    UIRemoteNotificationTypeAlert | 
    UIRemoteNotificationTypeBadge); 
    [application registerForRemoteNotificationTypes:allNotificationTypes]; 
#pragma clang diagnostic pop 
} else { 

    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 { 

#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 

     [UNUserNotificationCenter currentNotificationCenter].delegate = self; 
     UNAuthorizationOptions authOptions = 
     UNAuthorizationOptionAlert 
     | UNAuthorizationOptionSound 
     | UNAuthorizationOptionBadge; 
     [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) { 
     }]; 


     [FIRMessaging messaging].remoteMessageDelegate = self; 
#endif 
    } 

    [[UIApplication sharedApplication] registerForRemoteNotifications]; 
} 


[FIRApp configure]; 

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshNotification:) 
              name:kFIRInstanceIDTokenRefreshNotification object:nil]; 
    return YES; 


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

[[FIRMessaging messaging] disconnect]; 

} 

- (void)applicationDidBecomeActive:(UIApplication *)application { 
[self connectToFirebase]; 


application.applicationIconBadgeNumber = 0; 
} 

- (void) application:(UIApplication *) application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{ 

} 

#pragma mark -- Custom Firebase code 


- (void)tokenRefreshCallback:(NSNotification *) notification{ 
NSString *refreshedToken = [[FIRInstanceID instanceID] token]; 

[self connectToFirebase]; 
} 

-(void) connectToFirebase{ 

[[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) 
{ 
    if (error != nil) 
    { 

    } 
    else 
    { 

    } 
}]; 
} 
- (void)applicationWillResignActive:(UIApplication *)application {} 


- (void)applicationWillEnterForeground:(UIApplication *)application {} 


- (void)applicationWillTerminate:(UIApplication *)application {} 

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { 
if (userInfo[kGCMMessageIDKey]) { 

    NSLog(@"%@",userInfo); 
}} 


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

NSDictionary *userInfo = notification.request.content.userInfo; 
if (userInfo[kGCMMessageIDKey]) { 

} 


completionHandler(UNNotificationPresentationOptionNone); 
} 

- (void)userNotificationCenter:(UNUserNotificationCenter *)center 
didReceiveNotificationResponse:(UNNotificationResponse *)response 
    withCompletionHandler:(void (^)())completionHandler { 
    NSDictionary *userInfo = response.notification.request.content.userInfo; 
if (userInfo[kGCMMessageIDKey]) { 

} 

completionHandler(); 
    } 

- (void)connectToFcm { 

if (![[FIRInstanceID instanceID] token]) { 
    return; 
} 

[[FIRMessaging messaging] disconnect]; 

[[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) { 
    if (error != nil) { 

    } else { 

    } 
}]; 
} 

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { 
NSLog(@"PUSH ERROR: %@", error); 
} 

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 
NSLog(@"%@",deviceToken); 
} 

- (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage { 

} 

- (void)tokenRefreshNotification:(NSNotification *)notification { 

NSString *refreshedToken = [[FIRInstanceID instanceID] token];  
NSLog(@"%@",refreshedToken); 

[self connectToFcm]; 

} 

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings 
{ 

[application registerForRemoteNotifications]; 
} 
+0

を?デバイストークンを使用していますか? –

+0

selectアプリを使用して送信してください。単一のデバイスの代わりにプロジェクト設定タブで正しい.p12ファイルをアップロードしてください。 – KKRocks

+0

最近、.p12を追加した場合は、Google設定ファイルを再ダウンロードして古いものと置き換える必要があります。 – KKRocks

答えて

0

あなたはfirebaseコンソールからプッシュ通知を送信するためにトークンデバイスを使用しているように見えます。そうであれば、デバイストークンを使用しないでください。代わりにFirebaseはFCMRegistrationトークンを与えます。そのトークンを使用して、その特定のデバイスに通知を送信してください。

私の記事から抜粋。

特定のデバイスにプッシュ通知を送信するには、デバイストークン が必要です。デバイスでアプリケーションを実行すると( シミュレータ上にない)、設定が正しい場合、コンソールに デバイストークンとFCM登録トークンが印刷されます。どちらも同じですが、 と同じではありません。プッシュ通知を送信するときに使用するデバイストークン いくつかのサードパーティのサービスまたは私たち自身のサーバーを通じて。 firebase consoleを通じてプッシュ通知を送信する場合は、FCM 登録トークンを使用します。したがって、私たちの場合、 がコンソールに印刷されたFCM登録トークンを取ります。以下は

enter image description here How to send push notifications using firebase

のadditonalインフォメーションに関する私の記事です:あなたがチェックしてみたい場合場合は、あなたのプッシュ通知コードが正しく設定されたりしませPusher のようなサードパーティのライブラリを使用する。しかし、このか使用するデバイストークンをプッシャーで確認してください。

+0

にアップロード。 :( –

0

あなたが直面している問題かもしれません。

あなたのAPNSに正しい.p12ファイルをアップロードする必要があります。

あなたはここでは、あなたの.P12ファイルを追加する必要があり、単に後藤はプロジェクトクラウドメッセージング

の下で、その後を設定する必要があります。

enter image description here

アップロードした後は、設定ファイルを再ダウンロードし、古いものと交換する必要があります。

あなたが理解し、私はObjective-Cのコードを持っていないので、Objective-Cのに変換することができた場合、私はSWIFTコードをアップロードしている2

ソリューション。機能タブで

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
     FirebaseApp.configure() 
    if #available(iOS 10.0, *) { 
     // For iOS 10 display notification (sent via APNS) 
     UNUserNotificationCenter.current().delegate = self 

     let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] 
     UNUserNotificationCenter.current().requestAuthorization(
     options: authOptions, 
     completionHandler: {_, _ in }) 
    } else { 
     let settings: UIUserNotificationSettings = 
     UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) 
     application.registerUserNotificationSettings(settings) 
    } 
    application.registerForRemoteNotifications() 

    return true 
    } 

func application(_ application: UIApplication, 
        didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 
    Messaging.messaging().apnsToken = deviceToken as Data 
    var token: String = "" 
    for i in 0..<deviceToken.count { 
     token += String(format: "%02.2hhx", deviceToken[i] as CVarArg) 
    } 

    print(token) 
    } 

、それはまた、

enter image description here

+0

無効です示す拒否され、開発者ポータルからの証明書、キーチェーンに保存されたEMを作成し、それらをエクスポートした.p12ファイルを作成し、FCM –

+0

@Shikhaシャルマは、私は私のプロジェクトで正常に動作している私の迅速なコードを追加することができます。 – KKRocks

+0

うん、それは私を助けます。私は客観-Sを必要とするが、それは私はいくつかの助け –

0

ようにする必要があり、このコードを使用:使用しているトークン

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool 
{ 
    if #available(iOS 10.0, *) 
    { 
     // For iOS 10 display notification (sent via APNS) 
     UNUserNotificationCenter.currentNotificationCenter().delegate = self 
     UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge, .Sound, .Alert])   { (granted, error) in 
       if granted 
       { 
       //self.registerCategory() 
      } 
     } 
     // For iOS 10 data message (sent via FCM) 
     FIRMessaging.messaging().remoteMessageDelegate = self 
    } 
    else 
    { 
     let settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: [.Alert,.Badge,.Sound], categories: nil) 
     application.registerUserNotificationSettings(settings) 
    }   
    application.registerForRemoteNotifications()   
    //Configuring Firebase 
    FIRApp.configure() 
    // Add observer for InstanceID token refresh callback. 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.tokenRefreshNotification), name: kFIRInstanceIDTokenRefreshNotification, object: nil)  
    return true 
} 

//Receive Remote Notification on Background 
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) 
{ 
    FIRMessaging.messaging().appDidReceiveMessage(userInfo) 
} 

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) 
{ 
    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Sandbox) 
    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Prod) 
} 

func tokenRefreshNotification(notification: NSNotification) 
{ 
    if let refreshedToken = FIRInstanceID.instanceID().token() 
    { 
      print("InstanceID token: \(refreshedToken)") 
    } 
    // Connect to FCM since connection may have failed when attempted before having a token. 
    connectToFcm() 
} 

func connectToFcm() 
{ 
    FIRMessaging.messaging().connectWithCompletion { (error) in 
      if (error != nil) 
      { 
       print("Unable to connect with FCM. \(error)") 
      } 
      else 
      { 
       print("Connected to FCM.") 
      } 
    } 
} 

func applicationDidBecomeActive(application: UIApplication) 
{    
    connectToFcm() 
} 
関連する問題