Instance ID API منسوخ شده است. اگر نیاز به دسترسی به شناسه های نصب برنامه منحصر به فرد دارید، از Firebase installs API استفاده کنید. همچنین نصب Firebase و Instance ID را ببینید.
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
مثالهای زیر به شما کمک میکنند تا Instance ID را در یک کلاینت اندروید پیادهسازی کنید. توجه داشته باشید که این نمونهها از دامنه GCM استفاده میکنند که فقط برای اهداف نمایشی مفید است زیرا Google Cloud Messaging از استفاده کنار گذاشته شده است.
سرویس های Google Play را راه اندازی کنید
برای نوشتن برنامه مشتری خود، از SDK خدمات Google Play استفاده کنید، همانطور که در تنظیم SDK خدمات Google Play توضیح داده شده است. کتابخانه خدمات Play شامل کتابخانه Instance ID است.
String authorizedEntity = PROJECT_ID; // Project id from Google Developer Console
String scope = "GCM"; // e.g. communicating using GCM, but you can use any
// URL-safe characters up to a maximum of 1000, or
// you can also leave it blank.
String token = InstanceID.getInstance(context).getToken(authorizedEntity,scope);
توکن ها و شناسه های نمونه را مدیریت کنید
Instance ID به شما امکان میدهد توکنها را حذف و بهروزرسانی کنید.
همچنین میتوانید ID Instance را از جمله تمام نشانههای مرتبط حذف کنید. دفعه بعد که با getInstance() تماس گرفتید، یک Instance ID جدید دریافت خواهید کرد:
سرویس Instance ID به صورت دورهای (مثلاً هر 6 ماه یکبار) تماسهای برگشتی را آغاز میکند و از برنامه شما درخواست میکند که توکنهای خود را تازهسازی کند. همچنین ممکن است زمانی که:
مسائل امنیتی وجود دارد؛ به عنوان مثال، مشکلات SSL یا پلت فرم.
اطلاعات دستگاه دیگر معتبر نیست. به عنوان مثال، پشتیبان گیری و بازیابی.
سرویس Instance ID در غیر این صورت تحت تأثیر قرار می گیرد.
برای دریافت این تماسها، سرویس شنونده Instance ID را در برنامه خود پیادهسازی کنید:
تاریخ آخرین بهروزرسانی 2025-09-04 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","easyToUnderstand","thumb-up"],["مشکلم را برطرف کرد","solvedMyProblem","thumb-up"],["غیره","otherUp","thumb-up"]],[["اطلاعاتی که نیاز دارم وجود ندارد","missingTheInformationINeed","thumb-down"],["بیشازحد پیچیده/ مراحل بسیار زیاد","tooComplicatedTooManySteps","thumb-down"],["قدیمی","outOfDate","thumb-down"],["مشکل ترجمه","translationIssue","thumb-down"],["مشکل کد / نمونهها","samplesCodeIssue","thumb-down"],["غیره","otherDown","thumb-down"]],["تاریخ آخرین بهروزرسانی 2025-09-04 بهوقت ساعت هماهنگ جهانی."],[[["\u003cp\u003eThis documentation provides examples of how to implement Instance ID in an Android client using the Google Play services SDK.\u003c/p\u003e\n"],["\u003cp\u003eYou can get an Instance ID, generate tokens using your Project ID, and manage these tokens by deleting or refreshing them.\u003c/p\u003e\n"],["\u003cp\u003eInstance ID service may initiate callbacks for token refresh due to security issues, device information changes, or other service-related events.\u003c/p\u003e\n"],["\u003cp\u003eTo handle token refresh callbacks, implement the Instance ID listener service and configure it in your Android Manifest file.\u003c/p\u003e\n"]]],[],null,["# Android Implementation\n\nThe following examples will help you implement Instance ID in an Android\nclient. Note that these examples use the GCM scope, which is useful only\nfor demonstration purposes because\n[Google Cloud Messaging](/cloud-messaging/android/client) has been\nretired from use.\n\nSet Up Google Play Services\n---------------------------\n\nTo write your client application, use the Google Play services SDK,\nas described in [Set up Google Play\nServices SDK](http://developer.android.com/google/play-services/setup.html). The Play Services Library includes the Instance ID library.\n\nGet an Instance ID\n------------------\n\nThe following line of code returns an Instance ID: \n\n String iid = InstanceID.getInstance(context).getId();\n\nGenerate a token\n----------------\n\nGenerating tokens requires a Project ID generated by the\n[Google Developers Console](https://console.developers.google.com/project). \n\n String authorizedEntity = PROJECT_ID; // Project id from Google Developer Console\n String scope = \"GCM\"; // e.g. communicating using GCM, but you can use any\n // URL-safe characters up to a maximum of 1000, or\n // you can also leave it blank.\n String token = InstanceID.getInstance(context).getToken(authorizedEntity,scope);\n\nManage tokens and Instance IDs\n------------------------------\n\nInstance ID lets you delete and refresh tokens.\n\n### Delete tokens and Instance IDs\n\n String authorizedEntity = PROJECT_ID;\n String scope = \"GCM\";\n InstanceID.getInstance(context).deleteToken(authorizedEntity,scope);\n\nYou can also delete the Instance ID itself, including all associated\ntokens. The next time you call `getInstance()` you will get a new\nInstance ID: \n\n InstanceID.getInstance(context).deleteInstanceID();\n String newIID = InstanceID.getInstance(context).getId();\n\n| **Note:** If your app used tokens that were deleted by `deleteInstanceID`, your app will need to generate replacement tokens.\n\n### Refresh tokens\n\nThe Instance ID service initiates callbacks periodically (for example,\nevery 6 months), requesting that your app refreshes its tokens. It may also\ninitiate callbacks when:\n\n- There are security issues; for example, SSL or platform issues.\n- Device information is no longer valid; for example, backup and restore.\n- The Instance ID service is otherwise affected.\n\nImplement the Instance ID listener service in your app to receive these\ncallbacks: \n\n public class MyInstanceIDService extends InstanceIDListenerService {\n public void onTokenRefresh() {\n refreshAllTokens();\n }\n\n private void refreshAllTokens() {\n // assuming you have defined TokenList as\n // some generalized store for your tokens\n ArrayList\u003cTokenList\u003e tokenList = TokensList.get();\n InstanceID iid = InstanceID.getInstance(this);\n for(tokenItem : tokenList) {\n tokenItem.token =\n iid.getToken(tokenItem.authorizedEntity,tokenItem.scope,tokenItem.options);\n // send this tokenItem.token to your server\n }\n }\n };\n\nYou must also configure this service in the Manifest file for the project: \n\n \u003cservice android:name=\".MyInstanceIDService\" android:exported=\"false\"\u003e\n \u003cintent-filter\u003e\n \u003caction android:name=\"com.google.android.gms.iid.InstanceID\"/\u003e\n \u003c/intent-filter\u003e\n \u003c/service\u003e"]]