Firebase ক্লাউড মেসেজিং দিয়ে শুরু করুন


এই কুইকস্টার্টটি বর্ণনা করে কিভাবে আপনার মোবাইল এবং ওয়েব ক্লায়েন্ট অ্যাপগুলিতে Firebase Cloud Messaging সেট আপ করবেন যাতে আপনি নির্ভরযোগ্যভাবে বার্তা পাঠাতে পারেন। সার্ভার পরিবেশের জন্য, আপনার সার্ভার পরিবেশ এবং FCM দেখুন।

ইউনিটির সাথে একটি Firebase Cloud Messaging ক্লায়েন্ট অ্যাপ সেট আপ করুন

ইউনিটি দিয়ে আপনার ক্রস-প্ল্যাটফর্ম Firebase Cloud Messaging ক্লায়েন্ট অ্যাপটি লেখার জন্য, Firebase Cloud Messaging API ব্যবহার করুন। ইউনিটি SDK অ্যান্ড্রয়েড এবং অ্যাপল উভয়ের জন্যই কাজ করে, প্রতিটি প্ল্যাটফর্মের জন্য কিছু অতিরিক্ত সেটআপ প্রয়োজন হয়।

শুরু করার আগে

পূর্বশর্ত

  • ইউনিটি ২০২১ LTS বা তার পরবর্তী সংস্করণ ইনস্টল করুন। ইউনিটি ২০২০ এর জন্য সমর্থন বন্ধ বলে বিবেচিত হবে এবং পরবর্তী প্রধান রিলিজের পরে এটি আর সক্রিয়ভাবে সমর্থিত হবে না। পূর্ববর্তী সংস্করণগুলিও সামঞ্জস্যপূর্ণ হতে পারে কিন্তু সক্রিয়ভাবে সমর্থিত হবে না।

  • (শুধুমাত্র অ্যাপল প্ল্যাটফর্মের জন্য) নিম্নলিখিতগুলি ইনস্টল করুন:

    • এক্সকোড ১৩.৩.১ বা তার বেশি
    • কোকোপডস ১.১২.০ বা তার বেশি
  • নিশ্চিত করুন যে আপনার ইউনিটি প্রকল্পটি এই প্রয়োজনীয়তাগুলি পূরণ করে:

    • iOS-এর জন্য — iOS 13 বা তার উচ্চতর ভার্সনের জন্য
    • tvOS-এর জন্য - tvOS 13 বা তার বেশি ভার্সনের জন্য
    • অ্যান্ড্রয়েডের জন্য — লক্ষ্যমাত্রা API স্তর 21 (ললিপপ) বা উচ্চতর
  • আপনার ইউনিটি প্রজেক্ট চালানোর জন্য একটি ডিভাইস সেট আপ করুন অথবা একটি এমুলেটর ব্যবহার করুন।

    • iOS বা tvOS-এর জন্য — আপনার অ্যাপ চালানোর জন্য একটি ফিজিক্যাল ডিভাইস সেট আপ করুন এবং এই কাজগুলি সম্পন্ন করুন:

    • অ্যান্ড্রয়েডের জন্যএমুলেটরদের অবশ্যই গুগল প্লেতে একটি এমুলেটর ইমেজ ব্যবহার করতে হবে।

যদি আপনার কাছে ইতিমধ্যেই কোন ইউনিটি প্রজেক্ট না থাকে এবং শুধুমাত্র একটি Firebase পণ্য চেষ্টা করে দেখতে চান, তাহলে আপনি আমাদের কুইকস্টার্ট নমুনাগুলির একটি ডাউনলোড করতে পারেন।

ধাপ ১: একটি ফায়ারবেস প্রকল্প তৈরি করুন

আপনার ইউনিটি প্রজেক্টে ফায়ারবেস যোগ করার আগে, আপনার ইউনিটি প্রজেক্টের সাথে সংযোগ স্থাপনের জন্য আপনাকে একটি ফায়ারবেস প্রজেক্ট তৈরি করতে হবে। ফায়ারবেস প্রজেক্ট সম্পর্কে আরও জানতে আন্ডারস্ট্যান্ড ফায়ারবেস প্রজেক্টস দেখুন।

ধাপ ২: Firebase-এ আপনার অ্যাপ নিবন্ধন করুন

আপনার Firebase প্রকল্পের সাথে সংযোগ স্থাপনের জন্য আপনি এক বা একাধিক অ্যাপ বা গেম নিবন্ধন করতে পারেন।

  1. Firebase কনসোলে যান।

  2. প্রজেক্ট ওভারভিউ পৃষ্ঠার কেন্দ্রে, সেটআপ ওয়ার্কফ্লো চালু করতে ইউনিটি আইকন ( ) এ ক্লিক করুন।

    যদি আপনি ইতিমধ্যেই আপনার Firebase প্রকল্পে একটি অ্যাপ যোগ করে থাকেন, তাহলে প্ল্যাটফর্ম বিকল্পগুলি প্রদর্শন করতে অ্যাপ যোগ করুন -এ ক্লিক করুন।

  3. আপনার ইউনিটি প্রজেক্টের কোন বিল্ড টার্গেটটি আপনি নিবন্ধন করতে চান তা নির্বাচন করুন, অথবা আপনি একই সময়ে উভয় টার্গেট নিবন্ধন করতেও নির্বাচন করতে পারেন।

  4. আপনার ইউনিটি প্রজেক্টের প্ল্যাটফর্ম-নির্দিষ্ট আইডি(গুলি) লিখুন।

    • iOS এর জন্যiOS বান্ডেল আইডি ক্ষেত্রে আপনার ইউনিটি প্রজেক্টের iOS আইডি লিখুন।

    • অ্যান্ড্রয়েডের জন্যঅ্যান্ড্রয়েড প্যাকেজ নামের ক্ষেত্রে আপনার ইউনিটি প্রোজেক্টের অ্যান্ড্রয়েড আইডি লিখুন।
      প্যাকেজের নাম এবং অ্যাপ্লিকেশন আইডি শব্দ দুটি প্রায়শই বিনিময়যোগ্যভাবে ব্যবহৃত হয়।

  5. (ঐচ্ছিক) আপনার ইউনিটি প্রজেক্টের প্ল্যাটফর্ম-নির্দিষ্ট ডাকনাম(গুলি) লিখুন।
    এই ডাকনামগুলি অভ্যন্তরীণ, সুবিধাজনক শনাক্তকারী এবং শুধুমাত্র Firebase কনসোলে আপনার কাছে দৃশ্যমান।

  6. অ্যাপ নিবন্ধন করুন এ ক্লিক করুন।

ধাপ ৩: ফায়ারবেস কনফিগারেশন ফাইল যোগ করুন

  1. Firebase কনসোল সেটআপ ওয়ার্কফ্লোতে আপনার প্ল্যাটফর্ম-নির্দিষ্ট Firebase কনফিগারেশন ফাইল(গুলি) পান।

    • iOS এর জন্যDownload GoogleService-Info.plist এ ক্লিক করুন।

    • অ্যান্ড্রয়েডের জন্যgoogle-services.json ডাউনলোড করুন এ ক্লিক করুন।

  2. আপনার ইউনিটি প্রজেক্টের প্রজেক্ট উইন্ডোটি খুলুন, তারপর আপনার কনফিগারেশন ফাইল(গুলি) Assets ফোল্ডারে সরান।

  3. Firebase কনসোলে ফিরে, সেটআপ ওয়ার্কফ্লোতে, Next এ ক্লিক করুন।

ধাপ ৪: ফায়ারবেস ইউনিটি SDK যোগ করুন

  1. Firebase কনসোলে, Firebase Unity SDK ডাউনলোড করুন এ ক্লিক করুন, তারপর সুবিধাজনক কোথাও SDK আনজিপ করুন।

    • আপনি যেকোনো সময় আবার Firebase Unity SDK ডাউনলোড করতে পারেন।

    • Firebase Unity SDK প্ল্যাটফর্ম-নির্দিষ্ট নয়।

  2. আপনার ওপেন ইউনিটি প্রজেক্টে, Assets > Import Package > Custom Package এ যান।

  3. আনজিপ করা SDK থেকে, আপনার অ্যাপে ব্যবহার করতে চান এমন সমর্থিত Firebase পণ্যগুলি নির্বাচন করুন।

    Firebase Cloud Messaging এর সর্বোত্তম অভিজ্ঞতার জন্য, আমরা আপনার প্রোজেক্টে Google Analytics সক্ষম করার পরামর্শ দিচ্ছি। এছাড়াও, Analytics সেট আপ করার অংশ হিসেবে, আপনার অ্যাপে Analytics এর জন্য Firebase প্যাকেজ যোগ করতে হবে।

    Analytics সক্ষম করা হয়েছে

    • Google Analytics জন্য ফায়ারবেস প্যাকেজ যোগ করুন: FirebaseAnalytics.unitypackage
    • Firebase Cloud Messaging জন্য প্যাকেজটি যোগ করুন: FirebaseMessaging.unitypackage

    Analytics সক্ষম করা নেই

    Firebase Cloud Messaging জন্য প্যাকেজটি যোগ করুন: FirebaseMessaging.unitypackage

  4. ইমপোর্ট ইউনিটি প্যাকেজ উইন্ডোতে, ইমপোর্ট এ ক্লিক করুন।

  5. Firebase কনসোলে ফিরে, সেটআপ ওয়ার্কফ্লোতে, Next এ ক্লিক করুন।

ধাপ ৫: গুগল প্লে সার্ভিসেস ভার্সনের প্রয়োজনীয়তা নিশ্চিত করুন

অ্যান্ড্রয়েডের জন্য Firebase Unity SDK-এর কিছু পণ্যের জন্য Google Play services প্রয়োজন। কোন পণ্যগুলিতে এই নির্ভরতা রয়েছে তা জানুন। এই পণ্যগুলি ব্যবহার করার আগে Google Play services আপ-টু-ডেট থাকতে হবে।

আপনার অ্যাপ্লিকেশনের শুরুতে নিম্নলিখিত using স্টেটমেন্ট এবং ইনিশিয়ালাইজেশন কোড যোগ করুন। SDK-তে অন্য কোনও পদ্ধতিতে কল করার আগে আপনি Google Play services প্রয়োজনীয় সংস্করণে পরীক্ষা করতে এবং ঐচ্ছিকভাবে আপডেট করতে পারেন।

using Firebase.Extensions;
Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task => {
  var dependencyStatus = task.Result;
  if (dependencyStatus == Firebase.DependencyStatus.Available) {
    // Create and hold a reference to your FirebaseApp,
    // where app is a Firebase.FirebaseApp property of your application class.
       app = Firebase.FirebaseApp.DefaultInstance;

    // Set a flag here to indicate whether Firebase is ready to use by your app.
  } else {
    UnityEngine.Debug.LogError(System.String.Format(
      "Could not resolve all Firebase dependencies: {0}", dependencyStatus));
    // Firebase Unity SDK is not safe to use here.
  }
});

আপনার ইউনিটি প্রকল্পটি নিবন্ধিত এবং Firebase ব্যবহারের জন্য কনফিগার করা হয়েছে।

Get setup with Apple platforms

Use the following instructions to set up FCM with Unity and Apple platforms.

Upload your APNs authentication key

Upload your APNs authentication key to Firebase. If you don't already have an APNs authentication key, make sure to create one in the Apple Developer Member Center .

  1. Inside your project in the Firebase console, select the gear icon, select Project Settings , and then select the Cloud Messaging tab.

  2. In APNs authentication key under iOS app configuration , click the Upload button to upload your development authentication key, or production authentication key, or both. At least one is required.

  3. Browse to the location where you saved your key, select it, and click Open . Add the key ID for the key (available in the Apple Developer Member Center ) and click Upload .

Enable push notifications on Apple platforms

  1. Click your project in Xcode, then select the General tab from the Editor area .
  2. Scroll to Linked Frameworks and Libraries , then click the + button to add a framework.
  3. In the window that appears, scroll to UserNotifications.framework , click that entry, then click Add .
  4. Click your project in Xcode, then select the Capabilities tab from the Editor area .
  5. Switch Push Notifications to On .
  6. Scroll to Background Modes , then switch it to On .
  7. Select the Remote notifications checkbox under Background Modes .

Initialize Firebase Cloud Messaging

The Firebase Cloud Message library will be initialized when adding handlers for either the TokenReceived or MessageReceived events.

Upon initialization, a registration token is requested for the client app instance. The app will receive the token with the OnTokenReceived event, which should be cached for later use. You'll need this token if you want to target this specific device for messages.

In addition, you will need to register for the OnMessageReceived event if you want to be able to receive incoming messages.

The setup will look like this:

public void Start() {
  Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
  Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;
}

public void OnTokenReceived(object sender, Firebase.Messaging.TokenReceivedEventArgs token) {
  UnityEngine.Debug.Log("Received Registration Token: " + token.Token);
}

public void OnMessageReceived(object sender, Firebase.Messaging.MessageReceivedEventArgs e) {
  UnityEngine.Debug.Log("Received a new message from: " + e.Message.From);
}

Get setup with Android platforms

Use the following instructions to setup FCM with Unity and Android platforms.

Configure an Android entry point Activity

Firebase Cloud Messaging comes bundled with a custom entry point activity that replaces the default UnityPlayerActivity . If you are not using a custom entry point, this replacement happens automatically and you shouldn't have to take any additional action.

The Firebase Cloud Messaging Unity Plugin on Android comes bundled with two additional files:

  • Assets/Plugins/Android/libmessaging_unity_player_activity.jar contains an activity called MessagingUnityPlayerActivity that replaces the standard UnityPlayerActivity .
  • Assets/Plugins/Android/AndroidManifest.xml instructs the app to use MessagingUnityPlayerActivity as the entry point to the app.

These files are provided because the default UnityPlayerActivity does not handle onStop , onRestart activity lifecycle transitions or implement the onNewIntent which is necessary for Firebase Cloud Messaging to correctly handle incoming messages.

Configure a custom entry point Activity

If your app does not use the default UnityPlayerActivity you will need to remove the supplied AndroidManifest.xml and make sure that your custom activity properly handles all transitions of the Android Activity Lifecycle (an example of how to do this is shown below). If your custom activity extends UnityPlayerActivity you can instead extend com.google.firebase.MessagingUnityPlayerActivity which implements all necessary methods.

If you are using a custom Activity and not extending com.google.firebase.MessagingUnityPlayerActivity , you should include the following snippets in your Activity.

/**
 * Workaround for when a message is sent containing both a Data and Notification payload.
 *
 * When the app is in the background, if a message with both a data and notification payload is
 * received the data payload is stored on the Intent passed to onNewIntent. By default, that
 * intent does not get set as the Intent that started the app, so when the app comes back online
 * it doesn't see a new FCM message to respond to. As a workaround, we override onNewIntent so
 * that it sends the intent to the MessageForwardingService which forwards the message to the
 * FirebaseMessagingService which in turn sends the message to the application.
 */
@Override
protected void onNewIntent(Intent intent) {
  Intent message = new Intent(this, MessageForwardingService.class);
  message.setAction(MessageForwardingService.ACTION_REMOTE_INTENT);
  message.putExtras(intent);
  message.setData(intent.getData());
  // For earlier versions of Firebase C++ SDK (< 7.1.0), use `startService`.
  // startService(message);
  MessageForwardingService.enqueueWork(this, message);
}

/**
 * Dispose of the mUnityPlayer when restarting the app.
 *
 * This makes sure that when the app starts up again it does not start with stale data.
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
  if (mUnityPlayer != null) {
    mUnityPlayer.quit();
    mUnityPlayer = null;
  }
  super.onCreate(savedInstanceState);
}

New versions of Firebase C++ SDK (7.1.0 onwards) use JobIntentService which requires additional modifications in AndroidManifest.xml file.

<service android:name="com.google.firebase.messaging.MessageForwardingService"
     android:permission="android.permission.BIND_JOB_SERVICE"
     android:exported="false" >
</service>

Message delivery on Android

When the app is not running at all and a user taps on a notification, the message is not, by default, routed through FCM 's built in callbacks. In this case, message payloads are received through an Intent used to start the application.

Messages received while the app is in the background have the content of their notification field used to populate the system tray notification, but that notification content won't be communicated to FCM . This means that FirebaseMessage.Notification will be a null.

সংক্ষেপে:

App state বিজ্ঞপ্তি উপাত্ত উভয়ই
অগ্রভাগ Firebase.Messaging.FirebaseMessaging.MessageReceived Firebase.Messaging.FirebaseMessaging.MessageReceived Firebase.Messaging.FirebaseMessaging.MessageReceived
পটভূমি System tray Firebase.Messaging.FirebaseMessaging.MessageReceived Notification: system tray
Data: in extras of the intent.

FCM allows messages to be sent containing a deep link into your app. To receive messages that contain a deep link, you must add a new intent filter to the activity that handles deep links for your app. The intent filter should catch deep links of your domain. In AndroidManifest.xml:

<intent-filter>
  <action android:name="android.intent.action.VIEW"/>
  <category android:name="android.intent.category.DEFAULT"/>
  <category android:name="android.intent.category.BROWSABLE"/>
  <data android:host="CHANGE_THIS_DOMAIN.example.com" android:scheme="http"/>
  <data android:host="CHANGE_THIS_DOMAIN.example.com" android:scheme="https"/>
</intent-filter>

It is also possible to specify a wildcard to make the intent filter more flexible. For example:

<intent-filter>
  <action android:name="android.intent.action.VIEW"/>
  <category android:name="android.intent.category.DEFAULT"/>
  <category android:name="android.intent.category.BROWSABLE"/>
  <data android:host="*.example.com" android:scheme="http"/>
  <data android:host="*.example.com" android:scheme="https"/>
</intent-filter>

When users tap a notification containing a link to the scheme and host you specify, your app will start the activity with this intent filter to handle the link.

Prevent auto initialization

FCM generates a registration token for device targeting. When a token is generated, the library uploads the identifier and configuration data to Firebase. If you want to get an explicit opt-in before using the token, you can prevent generation at configure time by disabling FCM (and on Android, Analytics). You can add a metadata value to your Info.plist (not your GoogleService-Info.plist ) on Apple, or your AndroidManifest.xml on Android:

অ্যান্ড্রয়েড

<?xml version="1.0" encoding="utf-8"?>
<application>
  <meta-data android:name="firebase_messaging_auto_init_enabled"
             android:value="false" />
  <meta-data android:name="firebase_analytics_collection_enabled"
             android:value="false" />
</application>

সুইফট

FirebaseMessagingAutoInitEnabled = NO

To re-enable FCM, you can make a runtime call:

Firebase.Messaging.FirebaseMessaging.TokenRegistrationOnInitEnabled = true;

This value persists across app restarts once set.

পরবর্তী পদক্ষেপ

After you have completed the setup steps, here are a few options for moving forward with FCM for Unity: