FirebaseInstanceIdService は非推奨です 質問する

FirebaseInstanceIdService は非推奨です 質問する

皆さんがこのクラスをご存知であれば幸いです。このクラスは、Firebase 通知トークンが更新されるたびに通知トークンを取得するために使用されます。次のメソッドから、このクラスから更新されたトークンを取得します。

@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + refreshedToken);
}

FCMを実装したいのでこれを使用するには、MyClassを拡張してFirebaseInstanceIdService

しかし、FirebaseInstanceIdServiceは非推奨であることを示しています

誰かこれを知っていますか? これは非推奨なので、更新されたトークンを取得するには、代わりにどのメソッドまたはクラスを使用すればよいですか。

使用しています:implementation 'com.google.firebase:firebase-messaging:17.1.0'

同じ文書を確認しましたが、これについては何も言及されていません。 :FCM セットアップ ドキュメント


アップデート

この問題は修正されました。

Googleが を廃止したためFirebaseInstanceService

方法を見つけるために質問したところ、 FirebaseMessagingServiceからトークンを取得できることが分かりました。

以前と同様に、私が質問したとき、ドキュメントは更新されていませんでしたが、Google ドキュメントは更新されましたので、詳細については、この Google ドキュメントを参照してください。Firebaseメッセージングサービス

古いバージョン: FirebaseInstanceService (非推奨)

@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + refreshedToken);
}

新着 出典: FirebaseMessagingService

@Override
public void onNewToken(String s) {
    super.onNewToken(s);
    Log.d("NEW_TOKEN",s);
}

ベストアンサー1

2020年11月12日更新

FirebaseInstanceIdも非推奨です

今、私たちはFirebaseMessaging.getInstance().token

サンプルコード

        FirebaseMessaging.getInstance().token.addOnCompleteListener {
            if(it.isComplete){
                firebaseToken = it.result.toString()
                Util.printLog(firebaseToken)
            }
        }

    

はい FirebaseInstanceIdServiceは非推奨です

ドキュメントより:-このクラスは非推奨になりました。 を推奨します。overriding onNewTokenFirebaseMessagingService実装されたら、このサービスは安全に削除できます。

FCMトークンを取得するためにサービスを使用する必要はありません。安全にサービスFirebaseInstanceIdServiceを削除できます。FirebaseInstanceIdService

今、私たちは入る@Override onNewToken必要があるTokenFirebaseMessagingService

サンプルコード

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onNewToken(String s) {
        Log.e("NEW_TOKEN", s);
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Map<String, String> params = remoteMessage.getData();
        JSONObject object = new JSONObject(params);
        Log.e("JSON_OBJECT", object.toString());

        String NOTIFICATION_CHANNEL_ID = "Nilesh_channel";

        long pattern[] = {0, 1000, 500, 1000};

        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Your Notifications",
                    NotificationManager.IMPORTANCE_HIGH);

            notificationChannel.setDescription("");
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setVibrationPattern(pattern);
            notificationChannel.enableVibration(true);
            mNotificationManager.createNotificationChannel(notificationChannel);
        }

        // to diaplay notification in DND Mode
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = mNotificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID);
            channel.canBypassDnd();
        }

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);

        notificationBuilder.setAutoCancel(true)
                .setColor(ContextCompat.getColor(this, R.color.colorAccent))
                .setContentTitle(getString(R.string.app_name))
                .setContentText(remoteMessage.getNotification().getBody())
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setAutoCancel(true);


        mNotificationManager.notify(1000, notificationBuilder.build());
    }
}

編集

FirebaseMessagingServiceマニフェストファイルで次のように登録する必要があります

    <service
        android:name=".MyFirebaseMessagingService"
        android:stopWithTask="false">
        <intent-filter>
            
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

#アクティビティでトークンを取得する方法

.getToken();アクティビティでトークンを取得する必要がある場合は、使用しないでください。getInstanceId ()

今、私たちはgetInstanceId ()トークンを生成する

getInstanceId ()このプロジェクトIDに対して自動的に生成されたトークンを返します。Firebase

インスタンス ID がまだ存在しない場合は、インスタンス ID が生成され、Firebase バックエンドに定期的に情報が送信され始めます。

戻り値

  • およびInstanceIdResultを保持する を介して結果を確認するために使用できるタスク。IDtoken

サンプルコード

FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( MyActivity.this,  new OnSuccessListener<InstanceIdResult>() {
     @Override
     public void onSuccess(InstanceIdResult instanceIdResult) {
           String newToken = instanceIdResult.getToken();
           Log.e("newToken",newToken);

     }
 });

##編集2

Kotlinの実際のコードはこちら

class MyFirebaseMessagingService : FirebaseMessagingService() {

    override fun onNewToken(p0: String?) {

    }

    override fun onMessageReceived(remoteMessage: RemoteMessage?) {


        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        val NOTIFICATION_CHANNEL_ID = "Nilesh_channel"

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val notificationChannel = NotificationChannel(NOTIFICATION_CHANNEL_ID, "Your Notifications", NotificationManager.IMPORTANCE_HIGH)

            notificationChannel.description = "Description"
            notificationChannel.enableLights(true)
            notificationChannel.lightColor = Color.RED
            notificationChannel.vibrationPattern = longArrayOf(0, 1000, 500, 1000)
            notificationChannel.enableVibration(true)
            notificationManager.createNotificationChannel(notificationChannel)
        }

        // to diaplay notification in DND Mode
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = notificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID)
            channel.canBypassDnd()
        }

        val notificationBuilder = NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)

        notificationBuilder.setAutoCancel(true)
                .setColor(ContextCompat.getColor(this, R.color.colorAccent))
                .setContentTitle(getString(R.string.app_name))
                .setContentText(remoteMessage!!.getNotification()!!.getBody())
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setAutoCancel(true)


        notificationManager.notify(1000, notificationBuilder.build())

    }
}

おすすめ記事