Apple प्लेटफ़ॉर्म पर एकाधिक डिवाइस पर संदेश भेजें

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

यह ट्यूटोरियल FCM के लिए एडमिन SDK या REST API का उपयोग करके आपके ऐप सर्वर से विषय संदेश भेजने और उन्हें Apple ऐप में प्राप्त करने और संभालने पर केंद्रित है। यह पृष्ठ इसे प्राप्त करने के लिए सेटअप से लेकर सत्यापन तक के सभी चरणों को सूचीबद्ध करता है - इसलिए यदि आपने FCM के लिए Apple क्लाइंट ऐप सेट किया है या अपना पहला संदेश भेजने के चरणों के माध्यम से काम किया है, तो यह आपके द्वारा पहले ही पूरे किए गए चरणों को कवर कर सकता है।

अपने Apple प्रोजेक्ट में Firebase जोड़ें

यदि आपने पहले से ही अपने ऐप के लिए अन्य फायरबेस सुविधाओं को सक्षम कर लिया है तो यह अनुभाग आपके द्वारा पूरे किए जा सकने वाले कार्यों को शामिल करता है। विशेष रूप से एफसीएम के लिए, आपको अपनी एपीएन प्रमाणीकरण कुंजी अपलोड करनी होगी और दूरस्थ सूचनाओं के लिए पंजीकरण करना होगा

आवश्यक शर्तें

  • निम्नलिखित स्थापित करें:

    • एक्सकोड 14.1 या बाद का संस्करण
  • सुनिश्चित करें कि आपका प्रोजेक्ट इन आवश्यकताओं को पूरा करता है:

    • आपके प्रोजेक्ट को इन प्लेटफ़ॉर्म संस्करणों या बाद के संस्करण को लक्षित करना चाहिए:
      • आईओएस 11
      • मैकओएस 10.13
      • टीवीओएस 12
      • वॉचओएस 6
  • अपना ऐप चलाने के लिए एक भौतिक Apple डिवाइस सेट करें और इन कार्यों को पूरा करें:

    • अपने Apple डेवलपर खाते के लिए Apple पुश अधिसूचना प्रमाणीकरण कुंजी प्राप्त करें।
    • ऐप > क्षमताओं के अंतर्गत XCode में पुश नोटिफिकेशन सक्षम करें।

यदि आपके पास पहले से कोई Xcode प्रोजेक्ट नहीं है और आप केवल Firebase उत्पाद आज़माना चाहते हैं, तो आप हमारे क्विकस्टार्ट नमूनों में से एक डाउनलोड कर सकते हैं।

एक फायरबेस प्रोजेक्ट बनाएं

इससे पहले कि आप अपने ऐप्पल ऐप में फ़ायरबेस जोड़ सकें, आपको अपने ऐप से कनेक्ट करने के लिए एक फ़ायरबेस प्रोजेक्ट बनाना होगा। फायरबेस प्रोजेक्ट्स के बारे में अधिक जानने के लिए फायरबेस प्रोजेक्ट्स को समझें पर जाएँ।

अपने ऐप को फायरबेस के साथ पंजीकृत करें

अपने ऐप्पल ऐप में फ़ायरबेस का उपयोग करने के लिए, आपको अपने ऐप को अपने फ़ायरबेस प्रोजेक्ट के साथ पंजीकृत करना होगा। अपने ऐप को पंजीकृत करने को अक्सर अपने ऐप को अपने प्रोजेक्ट में "जोड़ना" कहा जाता है।

  1. फायरबेस कंसोल पर जाएं।

  2. प्रोजेक्ट अवलोकन पृष्ठ के केंद्र में, सेटअप वर्कफ़्लो लॉन्च करने के लिए iOS+ आइकन पर क्लिक करें।

    यदि आपने पहले से ही अपने फायरबेस प्रोजेक्ट में एक ऐप जोड़ा है, तो प्लेटफ़ॉर्म विकल्प प्रदर्शित करने के लिए ऐप जोड़ें पर क्लिक करें।

  3. बंडल आईडी फ़ील्ड में अपने ऐप की बंडल आईडी दर्ज करें।

  4. (वैकल्पिक) अन्य ऐप जानकारी दर्ज करें: ऐप उपनाम और ऐप स्टोर आईडी

  5. ऐप रजिस्टर करें पर क्लिक करें.

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

  1. अपनी फायरबेस Apple प्लेटफ़ॉर्म कॉन्फ़िगरेशन फ़ाइल ( GoogleService-Info.plist ) प्राप्त करने के लिए GoogleService-Info.plist डाउनलोड करें पर क्लिक करें।

  2. अपनी कॉन्फ़िग फ़ाइल को अपने Xcode प्रोजेक्ट के रूट में ले जाएँ। यदि संकेत दिया जाए, तो सभी लक्ष्यों में कॉन्फ़िगरेशन फ़ाइल जोड़ने का चयन करें।

यदि आपके प्रोजेक्ट में एकाधिक बंडल आईडी हैं, तो आपको प्रत्येक बंडल आईडी को फायरबेस कंसोल में एक पंजीकृत ऐप के साथ जोड़ना होगा ताकि प्रत्येक ऐप की अपनी GoogleService-Info.plist फ़ाइल हो सके।

अपने ऐप में फायरबेस एसडीके जोड़ें

फायरबेस निर्भरता को स्थापित और प्रबंधित करने के लिए स्विफ्ट पैकेज मैनेजर का उपयोग करें।

  1. Xcode में, अपना ऐप प्रोजेक्ट खुला होने पर, फ़ाइल > पैकेज जोड़ें पर नेविगेट करें।
  2. संकेत मिलने पर, Firebase Apple प्लेटफ़ॉर्म SDK रिपॉजिटरी जोड़ें:
  3.   https://github.com/firebase/firebase-ios-sdk.git
  4. फायरबेस क्लाउड मैसेजिंग लाइब्रेरी चुनें।
  5. अपने लक्ष्य की बिल्ड सेटिंग्स के अन्य लिंकर फ़्लैग अनुभाग में -ObjC फ़्लैग जोड़ें।
  6. फायरबेस क्लाउड मैसेजिंग के साथ एक इष्टतम अनुभव के लिए, हम आपके फायरबेस प्रोजेक्ट में Google Analytics को सक्षम करने और अपने ऐप में Google Analytics के लिए फायरबेस एसडीके जोड़ने की सलाह देते हैं। आप या तो आईडीएफए संग्रह के बिना या आईडीएफए संग्रह के साथ लाइब्रेरी का चयन कर सकते हैं।
  7. समाप्त होने पर, Xcode स्वचालित रूप से पृष्ठभूमि में आपकी निर्भरता को हल करना और डाउनलोड करना शुरू कर देगा।

अपनी एपीएन प्रमाणीकरण कुंजी अपलोड करें

अपनी APNs प्रमाणीकरण कुंजी को Firebase पर अपलोड करें। यदि आपके पास पहले से APNs प्रमाणीकरण कुंजी नहीं है, तो Apple डेवलपर सदस्य केंद्र में एक बनाना सुनिश्चित करें।

  1. फायरबेस कंसोल में अपने प्रोजेक्ट के अंदर, गियर आइकन चुनें, प्रोजेक्ट सेटिंग्स चुनें और फिर क्लाउड मैसेजिंग टैब चुनें।

  2. iOS ऐप कॉन्फ़िगरेशन के अंतर्गत APNs प्रमाणीकरण कुंजी में, अपलोड बटन पर क्लिक करें।

  3. उस स्थान पर ब्राउज़ करें जहां आपने अपनी कुंजी सहेजी थी, उसे चुनें और खोलें पर क्लिक करें। कुंजी के लिए कुंजी आईडी जोड़ें ( Apple डेवलपर सदस्य केंद्र में उपलब्ध) और अपलोड पर क्लिक करें।

अपने ऐप में फ़ायरबेस प्रारंभ करें

आपको अपने एप्लिकेशन में फ़ायरबेस इनिशियलाइज़ेशन कोड जोड़ना होगा। फ़ायरबेस मॉड्यूल आयात करें और दिखाए गए अनुसार एक साझा इंस्टेंस कॉन्फ़िगर करें:

  1. अपने UIApplicationDelegate में FirebaseCore मॉड्यूल को आयात करें, साथ ही आपके ऐप प्रतिनिधि द्वारा उपयोग किए जाने वाले किसी भी अन्य Firebase मॉड्यूल को आयात करें। उदाहरण के लिए, क्लाउड फायरस्टोर और प्रमाणीकरण का उपयोग करने के लिए:

    स्विफ्टयूआई

    import SwiftUI
    import FirebaseCore
    import FirebaseFirestore
    import FirebaseAuth
    // ...
          

    तीव्र

    import FirebaseCore
    import FirebaseFirestore
    import FirebaseAuth
    // ...
          

    उद्देश्य सी

    @import FirebaseCore;
    @import FirebaseFirestore;
    @import FirebaseAuth;
    // ...
          
  2. अपने ऐप प्रतिनिधि के application(_:didFinishLaunchingWithOptions:) विधि में FirebaseApp साझा किए गए इंस्टेंस को कॉन्फ़िगर करें:

    स्विफ्टयूआई

    // Use Firebase library to configure APIs
    FirebaseApp.configure()

    तीव्र

    // Use Firebase library to configure APIs
    FirebaseApp.configure()

    उद्देश्य सी

    // Use Firebase library to configure APIs
    [FIRApp configure];
  3. यदि आप स्विफ्टयूआई का उपयोग कर रहे हैं, तो आपको एक एप्लिकेशन प्रतिनिधि बनाना होगा और इसे UIApplicationDelegateAdaptor या NSApplicationDelegateAdaptor के माध्यम से अपने App स्ट्रक्चर में संलग्न करना होगा। आपको ऐप डेलीगेट स्विज़लिंग को भी अक्षम करना होगा। अधिक जानकारी के लिए स्विफ्टयूआई निर्देश देखें।

    स्विफ्टयूआई

    @main
    struct YourApp: App {
      // register app delegate for Firebase setup
      @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
    
      var body: some Scene {
        WindowGroup {
          NavigationView {
            ContentView()
          }
        }
      }
    }
          

दूरस्थ सूचनाओं के लिए पंजीकरण करें

या तो स्टार्टअप पर, या अपने एप्लिकेशन प्रवाह में वांछित बिंदु पर, दूरस्थ सूचनाओं के लिए अपने ऐप को पंजीकृत करें। दिखाए गए अनुसार registerForRemoteNotifications पर कॉल करें:

तीव्र


UNUserNotificationCenter.current().delegate = self

let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
  options: authOptions,
  completionHandler: { _, _ in }
)

application.registerForRemoteNotifications()

उद्देश्य सी


[UNUserNotificationCenter currentNotificationCenter].delegate = self;
UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert |
    UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
[[UNUserNotificationCenter currentNotificationCenter]
    requestAuthorizationWithOptions:authOptions
    completionHandler:^(BOOL granted, NSError * _Nullable error) {
      // ...
    }];

[application registerForRemoteNotifications];

किसी विषय के लिए क्लाइंट ऐप की सदस्यता लें

क्लाइंट ऐप्स किसी भी मौजूदा विषय की सदस्यता ले सकते हैं, या वे एक नया विषय बना सकते हैं। जब कोई क्लाइंट ऐप किसी नए विषय नाम (जो आपके फायरबेस प्रोजेक्ट के लिए पहले से मौजूद नहीं है) की सदस्यता लेता है, तो उस नाम का एक नया विषय एफसीएम में बनाया जाता है और कोई भी ग्राहक बाद में इसकी सदस्यता ले सकता है।

किसी विषय की सदस्यता लेने के लिए, अपने एप्लिकेशन के मुख्य थ्रेड से सदस्यता विधि को कॉल करें (एफसीएम थ्रेड-सुरक्षित नहीं है)। यदि सदस्यता अनुरोध प्रारंभ में विफल हो जाता है, तो FCM स्वचालित रूप से पुनः प्रयास करता है। ऐसे मामलों के लिए जहां सदस्यता पूरी नहीं की जा सकती, सदस्यता एक त्रुटि फेंकती है जिसे आप पूर्णता हैंडलर में पकड़ सकते हैं जैसा कि दिखाया गया है:

तीव्र

Messaging.messaging().subscribe(toTopic: "weather") { error in
  print("Subscribed to weather topic")
}

उद्देश्य सी

[[FIRMessaging messaging] subscribeToTopic:@"weather"
                                completion:^(NSError * _Nullable error) {
  NSLog(@"Subscribed to weather topic");
}];

यह कॉल एफसीएम बैकएंड के लिए एक अतुल्यकालिक अनुरोध करता है और क्लाइंट को दिए गए विषय की सदस्यता देता है। subscribeToTopic:topic पर कॉल करने से पहले, सुनिश्चित करें कि क्लाइंट ऐप इंस्टेंस को कॉलबैक didReceiveRegistrationToken के माध्यम से पहले ही एक पंजीकरण टोकन प्राप्त हो चुका है।

हर बार ऐप शुरू होने पर, एफसीएम यह सुनिश्चित करता है कि सभी अनुरोधित विषयों की सदस्यता ले ली गई है। सदस्यता समाप्त करने के लिए, unsubscribeFromTopic:topic पर कॉल करें, और FCM पृष्ठभूमि में विषय से सदस्यता समाप्त कर देता है।

विषय संदेश प्राप्त करें और संभालें

एफसीएम अन्य डाउनस्ट्रीम संदेशों की तरह ही विषय संदेश वितरित करता है।

application(_:didReceiveRemoteNotification:fetchCompletionHandler:) जैसा कि दिखाया गया है:

तीव्र

func application(_ application: UIApplication,
                 didReceiveRemoteNotification userInfo: [AnyHashable: Any]) async
  -> UIBackgroundFetchResult {
  // If you are receiving a notification message while your app is in the background,
  // this callback will not be fired till the user taps on the notification launching the application.
  // TODO: Handle data of notification

  // With swizzling disabled you must let Messaging know about the message, for Analytics
  // Messaging.messaging().appDidReceiveMessage(userInfo)

  // Print message ID.
  if let messageID = userInfo[gcmMessageIDKey] {
    print("Message ID: \(messageID)")
  }

  // Print full message.
  print(userInfo)

  return UIBackgroundFetchResult.newData
}

उद्देश्य सी

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
    fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  // If you are receiving a notification message while your app is in the background,
  // this callback will not be fired till the user taps on the notification launching the application.
  // TODO: Handle data of notification

  // With swizzling disabled you must let Messaging know about the message, for Analytics
  // [[FIRMessaging messaging] appDidReceiveMessage:userInfo];

  // ...

  // Print full message.
  NSLog(@"%@", userInfo);

  completionHandler(UIBackgroundFetchResultNewData);
}

भेजें अनुरोध बनाएं

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

बैकएंड पर अपने भेजने वाले तर्क में, वांछित विषय का नाम निर्दिष्ट करें जैसा कि दिखाया गया है:

नोड.जे.एस

// The topic name can be optionally prefixed with "/topics/".
const topic = 'highScores';

const message = {
  data: {
    score: '850',
    time: '2:45'
  },
  topic: topic
};

// Send a message to devices subscribed to the provided topic.
getMessaging().send(message)
  .then((response) => {
    // Response is a message ID string.
    console.log('Successfully sent message:', response);
  })
  .catch((error) => {
    console.log('Error sending message:', error);
  });

जावा

// The topic name can be optionally prefixed with "/topics/".
String topic = "highScores";

// See documentation on defining a message payload.
Message message = Message.builder()
    .putData("score", "850")
    .putData("time", "2:45")
    .setTopic(topic)
    .build();

// Send a message to the devices subscribed to the provided topic.
String response = FirebaseMessaging.getInstance().send(message);
// Response is a message ID string.
System.out.println("Successfully sent message: " + response);

अजगर

# The topic name can be optionally prefixed with "/topics/".
topic = 'highScores'

# See documentation on defining a message payload.
message = messaging.Message(
    data={
        'score': '850',
        'time': '2:45',
    },
    topic=topic,
)

# Send a message to the devices subscribed to the provided topic.
response = messaging.send(message)
# Response is a message ID string.
print('Successfully sent message:', response)

जाना

// The topic name can be optionally prefixed with "/topics/".
topic := "highScores"

// See documentation on defining a message payload.
message := &messaging.Message{
	Data: map[string]string{
		"score": "850",
		"time":  "2:45",
	},
	Topic: topic,
}

// Send a message to the devices subscribed to the provided topic.
response, err := client.Send(ctx, message)
if err != nil {
	log.Fatalln(err)
}
// Response is a message ID string.
fmt.Println("Successfully sent message:", response)

सी#

// The topic name can be optionally prefixed with "/topics/".
var topic = "highScores";

// See documentation on defining a message payload.
var message = new Message()
{
    Data = new Dictionary<string, string>()
    {
        { "score", "850" },
        { "time", "2:45" },
    },
    Topic = topic,
};

// Send a message to the devices subscribed to the provided topic.
string response = await FirebaseMessaging.DefaultInstance.SendAsync(message);
// Response is a message ID string.
Console.WriteLine("Successfully sent message: " + response);

आराम

POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1

Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
{
  "message":{
    "topic" : "foo-bar",
    "notification" : {
      "body" : "This is a Firebase Cloud Messaging Topic Message!",
      "title" : "FCM Message"
      }
   }
}

कर्ल कमांड:

curl -X POST -H "Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA" -H "Content-Type: application/json" -d '{
  "message": {
    "topic" : "foo-bar",
    "notification": {
      "body": "This is a Firebase Cloud Messaging Topic Message!",
      "title": "FCM Message"
    }
  }
}' https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1

विषयों के संयोजन के लिए एक संदेश भेजने के लिए, एक शर्त निर्दिष्ट करें, जो एक बूलियन अभिव्यक्ति है जो लक्ष्य विषयों को निर्दिष्ट करती है। उदाहरण के लिए, निम्न स्थिति उन डिवाइसों को संदेश भेजेगी जो TopicA और TopicB या TopicC की सदस्यता लेते हैं:

"'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)"

एफसीएम पहले कोष्ठक में किसी भी स्थिति का मूल्यांकन करता है, और फिर बाएं से दाएं अभिव्यक्ति का मूल्यांकन करता है। उपरोक्त अभिव्यक्ति में, किसी एक विषय की सदस्यता लेने वाले उपयोगकर्ता को संदेश प्राप्त नहीं होता है। इसी तरह, जो उपयोगकर्ता TopicA की सदस्यता नहीं लेता है उसे संदेश प्राप्त नहीं होता है। ये संयोजन इसे प्राप्त करते हैं:

  • TopicA और TopicB
  • TopicA और TopicC

आप अपनी सशर्त अभिव्यक्ति में अधिकतम पाँच विषय शामिल कर सकते हैं।

किसी शर्त पर भेजने के लिए:

नोड.जे.एस

// Define a condition which will send to devices which are subscribed
// to either the Google stock or the tech industry topics.
const condition = '\'stock-GOOG\' in topics || \'industry-tech\' in topics';

// See documentation on defining a message payload.
const message = {
  notification: {
    title: '$FooCorp up 1.43% on the day',
    body: '$FooCorp gained 11.80 points to close at 835.67, up 1.43% on the day.'
  },
  condition: condition
};

// Send a message to devices subscribed to the combination of topics
// specified by the provided condition.
getMessaging().send(message)
  .then((response) => {
    // Response is a message ID string.
    console.log('Successfully sent message:', response);
  })
  .catch((error) => {
    console.log('Error sending message:', error);
  });

जावा

// Define a condition which will send to devices which are subscribed
// to either the Google stock or the tech industry topics.
String condition = "'stock-GOOG' in topics || 'industry-tech' in topics";

// See documentation on defining a message payload.
Message message = Message.builder()
    .setNotification(Notification.builder()
        .setTitle("$GOOG up 1.43% on the day")
        .setBody("$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.")
        .build())
    .setCondition(condition)
    .build();

// Send a message to devices subscribed to the combination of topics
// specified by the provided condition.
String response = FirebaseMessaging.getInstance().send(message);
// Response is a message ID string.
System.out.println("Successfully sent message: " + response);

अजगर

# Define a condition which will send to devices which are subscribed
# to either the Google stock or the tech industry topics.
condition = "'stock-GOOG' in topics || 'industry-tech' in topics"

# See documentation on defining a message payload.
message = messaging.Message(
    notification=messaging.Notification(
        title='$GOOG up 1.43% on the day',
        body='$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.',
    ),
    condition=condition,
)

# Send a message to devices subscribed to the combination of topics
# specified by the provided condition.
response = messaging.send(message)
# Response is a message ID string.
print('Successfully sent message:', response)

जाना

// Define a condition which will send to devices which are subscribed
// to either the Google stock or the tech industry topics.
condition := "'stock-GOOG' in topics || 'industry-tech' in topics"

// See documentation on defining a message payload.
message := &messaging.Message{
	Data: map[string]string{
		"score": "850",
		"time":  "2:45",
	},
	Condition: condition,
}

// Send a message to devices subscribed to the combination of topics
// specified by the provided condition.
response, err := client.Send(ctx, message)
if err != nil {
	log.Fatalln(err)
}
// Response is a message ID string.
fmt.Println("Successfully sent message:", response)

सी#

// Define a condition which will send to devices which are subscribed
// to either the Google stock or the tech industry topics.
var condition = "'stock-GOOG' in topics || 'industry-tech' in topics";

// See documentation on defining a message payload.
var message = new Message()
{
    Notification = new Notification()
    {
        Title = "$GOOG up 1.43% on the day",
        Body = "$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.",
    },
    Condition = condition,
};

// Send a message to devices subscribed to the combination of topics
// specified by the provided condition.
string response = await FirebaseMessaging.DefaultInstance.SendAsync(message);
// Response is a message ID string.
Console.WriteLine("Successfully sent message: " + response);

आराम

POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1

Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
{
   "message":{
    "condition": "'dogs' in topics || 'cats' in topics",
    "notification" : {
      "body" : "This is a Firebase Cloud Messaging Topic Message!",
      "title" : "FCM Message",
    }
  }
}

कर्ल कमांड:

curl -X POST -H "Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA" -H "Content-Type: application/json" -d '{
  "notification": {
    "title": "FCM Message",
    "body": "This is a Firebase Cloud Messaging Topic Message!",
  },
  "condition": "'dogs' in topics || 'cats' in topics"
}' https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1

अगले कदम