Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

FCMでアプリ再インストールした際にPUSH通知が届かなくなる件

Posted at

##今回の仕様
アプリ側でトークンを取得してからDBに登録してその登録されたトークン宛にPUSH通知を送信しています。
下記コードのタイミングです。

code
InstanceID.instanceID().instanceID { (result, error) in
    if let error = error {
        print("Error fetching remote instance ID: \(error)")
    } else if let result = result {
        print("Remote instance ID token: \(result.token)")
        // FCMトークンを更新する
        registFCMToken(fcmToken: result.token)
    }
}

#ど゛う゛し゛て゛と゛ど゛か゛な゛い゛ん゛だ゛よ゛お゛お゛お゛お゛お゛
FCMでiOS端末にPUSH通知を送信しようとした時にサーバー側でこんなエラーが・・・
The provided registration token is not registered. A previously valid registration token can be unregistered for a variety of reasons. See the error documentation for more details. Remove this registration token and stop using it to send messages.
様々なって何!?
前まで送信出来ていたのにおかしい!
少しググってみたら再インストールとかが要因らしい。

##解決策
AppDelegateにこいつを追加してあげる。
重要なのはここ

Messaging.messaging().apnsToken = deviceToken

AppDelegate.swift
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    print("device token regiter OK")
    // XXX: 再インストール時になぜかこれがないとPUSH通知がこないのでとりあえず書いておく。
    Messaging.messaging().apnsToken = deviceToken
}

コメントにある通りなんでこれで直るのかもよくわかってないです。
これでアプリを再インストールするとちゃんと届くようになりました。

うーん、PUSH通知は苦手!

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?