Catch up on highlights from Firebase at Google I/O 2023. Learn more

ईमेल एक्शन लिंक बनाना

मोबाइल ऐप्स को कभी-कभी उपयोगकर्ताओं के साथ बातचीत करने और उन्हें ईमेल भेजकर कुछ कार्रवाई करने के लिए प्रेरित करने की आवश्यकता होती है।

फायरबेस क्लाइंट एसडीके उपयोगकर्ताओं को ऐसे लिंक वाले ईमेल भेजने की क्षमता प्रदान करते हैं जिनका उपयोग वे पासवर्ड रीसेट, ईमेल पता सत्यापन और ईमेल-आधारित साइन-इन के लिए कर सकते हैं। ये टेम्प्लेट-आधारित ईमेल Google द्वारा भेजे जाते हैं और इनमें सीमित अनुकूलन क्षमता होती है।

यदि आप इसके बजाय अपने स्वयं के ईमेल टेम्प्लेट और अपनी स्वयं की ईमेल वितरण सेवा का उपयोग करना चाहते हैं, तो यह पृष्ठ बताता है कि उपरोक्त प्रवाह के लिए प्रोग्रामेटिक रूप से कार्रवाई लिंक जेनरेट करने के लिए फायरबेस व्यवस्थापक एसडीके का उपयोग कैसे करें, जिसे आप अपने उपयोगकर्ताओं को ईमेल में शामिल कर सकते हैं।

यह निम्नलिखित लाभों के साथ आता है:

  • ईमेल टेम्प्लेट कस्टमाइज़ करें। इसमें नई शैलियों और कस्टम ब्रांडिंग को जोड़ने, शब्दों और लोगो को बदलने, उपयोगकर्ताओं को पूरे नाम के बजाय पहले नाम से संबोधित करने आदि की क्षमता शामिल है।
  • संदर्भ के आधार पर विभिन्न टेम्पलेट लागू करें। उदाहरण के लिए, यदि उपयोगकर्ता न्यूज़लेटर की सदस्यता लेने के लिए अपने ईमेल का सत्यापन कर रहा है, तो ईमेल सामग्री में संदर्भ प्रदान करने की आवश्यकता हो सकती है। एक अन्य उदाहरण ईमेल लिंक साइन इन है: एक परिदृश्य में इसे उसी उपयोगकर्ता द्वारा, या किसी अन्य उपयोगकर्ता द्वारा आमंत्रण के रूप में ट्रिगर किया जा सकता है। संदर्भ को ईमेल में शामिल करने की आवश्यकता होगी।
  • अनुकूलित ईमेल टेम्प्लेट का स्थानीयकरण करें।
  • एक सुरक्षित सर्वर वातावरण से लिंक उत्पन्न करने की क्षमता।
  • मोबाइल ऐप या ब्राउज़र के माध्यम से लिंक को कैसे खोला जाए, और अतिरिक्त राज्य की जानकारी कैसे पास की जाए, आदि को अनुकूलित करने की क्षमता।
  • ईमेल क्रिया लिंक का निर्माण करते समय मोबाइल एप्लिकेशन प्रवाह के लिए उपयोग किए जाने वाले डायनेमिक लिंक डोमेन को कस्टमाइज़ करने की क्षमता, और यहां तक ​​कि संदर्भ या मोबाइल ऐप के आधार पर एक अलग डायनेमिक लिंक डोमेन निर्दिष्ट करने की क्षमता।

एक्शनकोड सेटिंग्स को इनिशियलाइज़ करें

ईमेल एक्शन लिंक जेनरेट करने से पहले, आपको ActionCodeSettings इंस्टेंस को इनिशियलाइज़ करने की आवश्यकता हो सकती है।

ActionCodeSettings आपको एक जारी यूआरएल के माध्यम से अतिरिक्त स्थिति पारित करने की अनुमति देती है जो उपयोगकर्ता द्वारा ईमेल लिंक पर क्लिक करने के बाद पहुंच योग्य है। यह उपयोगकर्ता को कार्रवाई पूरी होने के बाद ऐप पर वापस जाने की क्षमता भी प्रदान करता है। इसके अलावा, आप यह निर्दिष्ट कर सकते हैं कि ईमेल एक्शन लिंक को सीधे मोबाइल एप्लिकेशन से हैंडल करना है या नहीं, जब यह इंस्टॉल हो या ब्राउज़र से।

मोबाइल ऐप के माध्यम से खोले जाने वाले लिंक के लिए, आपको अपने मोबाइल ऐप से इन लिंक का पता लगाने के लिए फायरबेस डायनेमिक लिंक सक्षम करने और कुछ कार्य करने होंगे। ईमेल कार्रवाइयों के लिए फायरबेस डायनेमिक लिंक को कॉन्फ़िगर करने के तरीके के बारे में निर्देश देखें।

ActionCodeSettings इंस्टेंस को इनिशियलाइज़ करने के लिए, निम्न डेटा प्रदान करें:

पैरामीटर टाइप विवरण
url डोरी

लिंक सेट करता है (राज्य/जारी यूआरएल) जिसका अलग-अलग संदर्भों में अलग-अलग अर्थ हैं:

  • जब लिंक को वेब एक्शन विजेट्स में हैंडल किया जाता है, तो यह continueUrl क्वेरी पैरामीटर में डीप लिंक होता है।
  • जब लिंक को सीधे ऐप में हैंडल किया जाता है, तो यह डायनामिक लिंक के डीप लिंक में continueUrl यूआरएल क्वेरी पैरामीटर है।
iOS ({बंडल आईडी: स्ट्रिंग}|अपरिभाषित) बंडल आईडी सेट करता है। यदि यह स्थापित है तो यह ऐप्पल ऐप में लिंक खोलने का प्रयास करेगा। ऐप को कंसोल में पंजीकृत होना चाहिए।
android ({पैकेजनाम: स्ट्रिंग, इंस्टॉलऐप:बूलियन|अपरिभाषित, न्यूनतम संस्करण: स्ट्रिंग|अपरिभाषित}|अपरिभाषित) Android पैकेज का नाम सेट करता है। यदि यह स्थापित है तो यह एंड्रॉइड ऐप में लिंक को खोलने का प्रयास करेगा। यदि installApp पास हो जाता है, तो यह निर्दिष्ट करता है कि यदि डिवाइस इसका समर्थन करता है और ऐप पहले से इंस्टॉल नहीं है तो Android ऐप इंस्टॉल करना है या नहीं। यदि यह फ़ील्ड packageName के बिना प्रदान की जाती है, तो यह समझाते हुए एक त्रुटि उत्पन्न होती है कि packageName को इस फ़ील्ड के संयोजन में प्रदान किया जाना चाहिए। यदि minimumVersion संस्करण निर्दिष्ट है, और ऐप का पुराना संस्करण स्थापित है, तो उपयोगकर्ता को ऐप को अपग्रेड करने के लिए Play Store पर ले जाया जाता है। Android ऐप को कंसोल में पंजीकृत होना चाहिए।
handleCodeInApp (बूलियन|अपरिभाषित) ईमेल एक्शन लिंक को मोबाइल ऐप में खोला जाएगा या पहले वेब लिंक में। डिफॉल्ट गलत है। जब सही पर सेट किया जाता है, तो एक्शन कोड लिंक को यूनिवर्सल लिंक या एंड्रॉइड ऐप लिंक के रूप में भेजा जाएगा और इंस्टॉल होने पर ऐप द्वारा खोला जाएगा। झूठे मामले में, कोड पहले वेब विजेट पर भेजा जाएगा और फिर जारी रखने पर इंस्टॉल होने पर ऐप पर रीडायरेक्ट कर दिया जाएगा।
dynamicLinkDomain (स्ट्रिंग | अपरिभाषित) डायनामिक लिंक डोमेन (या सबडोमेन) को वर्तमान लिंक के लिए उपयोग करने के लिए सेट करता है यदि इसे फायरबेस डायनेमिक लिंक का उपयोग करके खोला जाना है। चूंकि प्रति प्रोजेक्ट कई डायनामिक लिंक डोमेन कॉन्फ़िगर किए जा सकते हैं, इसलिए यह फ़ील्ड स्पष्ट रूप से किसी एक को चुनने की क्षमता प्रदान करती है। यदि कोई प्रदान नहीं किया जाता है, तो सबसे पुराना डोमेन डिफ़ॉल्ट रूप से उपयोग किया जाता है।

निम्न उदाहरण दिखाता है कि ईमेल सत्यापन लिंक कैसे भेजा जाए जो पहले मोबाइल ऐप में एक फायरबेस डायनेमिक लिंक (Apple ऐप com.example.ios या Android ऐप com.example.android के रूप में खुलेगा, जहां ऐप पहले से इंस्टॉल नहीं होने पर इंस्टॉल होगा और न्यूनतम संस्करण 12 है)। डीप लिंक में URL पेलोड जारी रहेगा https://www.example.com/checkout?cartId=1234 । उपयोग किया जाने वाला डायनामिक लिंक डोमेन coolapp.page.link है, जिसे फायरबेस डायनेमिक लिंक के साथ उपयोग के लिए कॉन्फ़िगर किया जाना चाहिए।

Node.js

const actionCodeSettings = {
  // URL you want to redirect back to. The domain (www.example.com) for
  // this URL must be whitelisted in the Firebase Console.
  url: 'https://www.example.com/checkout?cartId=1234',
  // This must be true for email link sign-in.
  handleCodeInApp: true,
  iOS: {
    bundleId: 'com.example.ios',
  },
  android: {
    packageName: 'com.example.android',
    installApp: true,
    minimumVersion: '12',
  },
  // FDL custom domain.
  dynamicLinkDomain: 'coolapp.page.link',
};

जावा

ActionCodeSettings actionCodeSettings = ActionCodeSettings.builder()
    .setUrl("https://www.example.com/checkout?cartId=1234")
    .setHandleCodeInApp(true)
    .setIosBundleId("com.example.ios")
    .setAndroidPackageName("com.example.android")
    .setAndroidInstallApp(true)
    .setAndroidMinimumVersion("12")
    .setDynamicLinkDomain("coolapp.page.link")
    .build();

अजगर

action_code_settings = auth.ActionCodeSettings(
    url='https://www.example.com/checkout?cartId=1234',
    handle_code_in_app=True,
    ios_bundle_id='com.example.ios',
    android_package_name='com.example.android',
    android_install_app=True,
    android_minimum_version='12',
    dynamic_link_domain='coolapp.page.link',
)

जाओ

actionCodeSettings := &auth.ActionCodeSettings{
	URL:                   "https://www.example.com/checkout?cartId=1234",
	HandleCodeInApp:       true,
	IOSBundleID:           "com.example.ios",
	AndroidPackageName:    "com.example.android",
	AndroidInstallApp:     true,
	AndroidMinimumVersion: "12",
	DynamicLinkDomain:     "coolapp.page.link",
}

सी#

var actionCodeSettings = new ActionCodeSettings()
{
    Url = "https://www.example.com/checkout?cartId=1234",
    HandleCodeInApp = true,
    IosBundleId = "com.example.ios",
    AndroidPackageName = "com.example.android",
    AndroidInstallApp = true,
    AndroidMinimumVersion = "12",
    DynamicLinkDomain = "coolapp.page.link",
};

अधिक जानने के लिए, ईमेल क्रियाओं में पासिंग स्टेट देखें।

पासवर्ड रीसेट लिंक जेनरेट करने के लिए, मौजूदा उपयोगकर्ता का ईमेल और एक वैकल्पिक ActionCodeSettings ऑब्जेक्ट प्रदान करें। कार्रवाई ईमेल कार्रवाई लिंक के साथ हल हो जाएगी। उपयोग किया गया ईमेल किसी मौजूदा उपयोगकर्ता का होना चाहिए।

Node.js

// Admin SDK API to generate the password reset link.
const userEmail = 'user@example.com';
getAuth()
  .generatePasswordResetLink(userEmail, actionCodeSettings)
  .then((link) => {
    // Construct password reset email template, embed the link and send
    // using custom SMTP server.
    return sendCustomPasswordResetEmail(userEmail, displayName, link);
  })
  .catch((error) => {
    // Some error occurred.
  });

जावा

String email = "user@example.com";
try {
  String link = FirebaseAuth.getInstance().generatePasswordResetLink(
      email, actionCodeSettings);
  // Construct email verification template, embed the link and send
  // using custom SMTP server.
  sendCustomEmail(email, displayName, link);
} catch (FirebaseAuthException e) {
  System.out.println("Error generating email link: " + e.getMessage());
}

अजगर

email = 'user@example.com'
link = auth.generate_password_reset_link(email, action_code_settings)
# Construct password reset email from a template embedding the link, and send
# using a custom SMTP server.
send_custom_email(email, link)

जाओ

email := "user@example.com"
link, err := client.PasswordResetLinkWithSettings(ctx, email, actionCodeSettings)
if err != nil {
	log.Fatalf("error generating email link: %v\n", err)
}

// Construct password reset template, embed the link and send
// using custom SMTP server.
sendCustomEmail(email, displayName, link)

सी#

var email = "user@example.com";
var link = await FirebaseAuth.DefaultInstance.GeneratePasswordResetLinkAsync(
    email, actionCodeSettings);
// Construct email verification template, embed the link and send
// using custom SMTP server.
SendCustomEmail(email, displayName, link);

लिंक जनरेट होने के बाद, इसे कस्टम पासवर्ड रीसेट ईमेल में डाला जा सकता है और फिर कस्टम एसएमटीपी सर्वर का उपयोग करके संबंधित उपयोगकर्ता को ईमेल किया जा सकता है।

यदि आप डिफ़ॉल्ट पासवर्ड रीसेट लैंडिंग पृष्ठ का उपयोग नहीं कर रहे हैं और अपना स्वयं का कस्टम हैंडलर बना रहे हैं, तो कस्टम ईमेल एक्शन हैंडलर बनाना देखें।

एक ईमेल सत्यापन लिंक उत्पन्न करने के लिए, मौजूदा उपयोगकर्ता का असत्यापित ईमेल और एक वैकल्पिक ActionCodeSettings ऑब्जेक्ट प्रदान करें। कार्रवाई ईमेल कार्रवाई लिंक के साथ हल हो जाएगी। उपयोग किया गया ईमेल किसी मौजूदा उपयोगकर्ता का होना चाहिए।

Node.js

// Admin SDK API to generate the email verification link.
const useremail = 'user@example.com';
getAuth()
  .generateEmailVerificationLink(useremail, actionCodeSettings)
  .then((link) => {
    // Construct email verification template, embed the link and send
    // using custom SMTP server.
    return sendCustomVerificationEmail(useremail, displayName, link);
  })
  .catch((error) => {
    // Some error occurred.
  });

जावा

String email = "user@example.com";
try {
  String link = FirebaseAuth.getInstance().generateEmailVerificationLink(
      email, actionCodeSettings);
  // Construct email verification template, embed the link and send
  // using custom SMTP server.
  sendCustomEmail(email, displayName, link);
} catch (FirebaseAuthException e) {
  System.out.println("Error generating email link: " + e.getMessage());
}

अजगर

email = 'user@example.com'
link = auth.generate_email_verification_link(email, action_code_settings)
# Construct email from a template embedding the link, and send
# using a custom SMTP server.
send_custom_email(email, link)

जाओ

email := "user@example.com"
link, err := client.EmailVerificationLinkWithSettings(ctx, email, actionCodeSettings)
if err != nil {
	log.Fatalf("error generating email link: %v\n", err)
}

// Construct email verification template, embed the link and send
// using custom SMTP server.
sendCustomEmail(email, displayName, link)

सी#

var email = "user@example.com";
var link = await FirebaseAuth.DefaultInstance.GenerateEmailVerificationLinkAsync(
    email, actionCodeSettings);
// Construct email verification template, embed the link and send
// using custom SMTP server.
SendCustomEmail(email, displayName, link);

लिंक जनरेट होने के बाद, इसे कस्टम सत्यापन ईमेल में डाला जा सकता है और फिर कस्टम SMTP सर्वर का उपयोग करके संबंधित उपयोगकर्ता को ईमेल किया जा सकता है।

यदि आप डिफ़ॉल्ट ईमेल सत्यापन लैंडिंग पृष्ठ का उपयोग नहीं कर रहे हैं और अपना स्वयं का कस्टम हैंडलर बना रहे हैं, तो कस्टम ईमेल एक्शन हैंडलर बनाना देखें।

इससे पहले कि आप ईमेल लिंक साइन-इन के साथ उपयोगकर्ताओं को प्रमाणित कर सकें, आपको अपने फायरबेस प्रोजेक्ट के लिए ईमेल लिंक साइन-इन सक्षम करना होगा।

साइन-इन लिंक जनरेट करने के लिए, उपयोगकर्ता का ईमेल और एक ActionCodeSettings ऑब्जेक्ट प्रदान करें। साइन-इन पूरा करने के लिए लिंक पर क्लिक करने के बाद उपयोगकर्ता को कहां वापस करना है, इस बारे में जानकारी प्रदान करने के लिए इस मामले में ActionCodeSettings ऑब्जेक्ट की आवश्यकता होती है। कार्रवाई ईमेल कार्रवाई लिंक के साथ हल हो जाएगी।

पासवर्ड रीसेट और ईमेल सत्यापन के विपरीत, उपयोग किए गए ईमेल को मौजूदा उपयोगकर्ता से संबंधित होने की आवश्यकता नहीं है, क्योंकि इस ऑपरेशन का उपयोग ईमेल लिंक के माध्यम से आपके ऐप में नए उपयोगकर्ताओं को साइन अप करने के लिए किया जा सकता है।

Node.js

// Admin SDK API to generate the sign in with email link.
const useremail = 'user@example.com';
getAuth()
  .generateSignInWithEmailLink(useremail, actionCodeSettings)
  .then((link) => {
    // Construct sign-in with email link template, embed the link and
    // send using custom SMTP server.
    return sendSignInEmail(useremail, displayName, link);
  })
  .catch((error) => {
    // Some error occurred.
  });

जावा

String email = "user@example.com";
try {
  String link = FirebaseAuth.getInstance().generateSignInWithEmailLink(
      email, actionCodeSettings);
  // Construct email verification template, embed the link and send
  // using custom SMTP server.
  sendCustomEmail(email, displayName, link);
} catch (FirebaseAuthException e) {
  System.out.println("Error generating email link: " + e.getMessage());
}

अजगर

email = 'user@example.com'
link = auth.generate_sign_in_with_email_link(email, action_code_settings)
# Construct email from a template embedding the link, and send
# using a custom SMTP server.
send_custom_email(email, link)

जाओ

email := "user@example.com"
link, err := client.EmailSignInLink(ctx, email, actionCodeSettings)
if err != nil {
	log.Fatalf("error generating email link: %v\n", err)
}

// Construct sign-in with email link template, embed the link and send
// using custom SMTP server.
sendCustomEmail(email, displayName, link)

सी#

var email = "user@example.com";
var link = await FirebaseAuth.DefaultInstance.GenerateSignInWithEmailLinkAsync(
    email, actionCodeSettings);
// Construct email verification template, embed the link and send
// using custom SMTP server.
SendCustomEmail(email, displayName, link);

लिंक जेनरेट होने के बाद, इसे कस्टम साइन-इन ईमेल में डाला जा सकता है और फिर कस्टम एसएमटीपी सर्वर का उपयोग करके संबंधित उपयोगकर्ता को ईमेल किया जा सकता है।

ईमेल लिंक का उपयोग करके उपयोगकर्ताओं को Firebase से प्रमाणित करने के बारे में अधिक जानें। यह उपयोगकर्ता द्वारा लिंक पर क्लिक करने और ऐप पर वापस रीडायरेक्ट किए जाने के बाद साइन-इन को पूरा करने के तरीके के बारे में जानकारी प्रदान करने में मदद करेगा।