Firebase क्लाउड से मैसेज भेजने की सेवा का इस्तेमाल करके मैसेज पाना

इस गाइड में, मोबाइल और वेब क्लाइंट ऐप्लिकेशन में Firebase Cloud Messaging सेट अप करने का तरीका बताया गया है, ताकि आपको भरोसेमंद तरीके से मैसेज मिल सकें.

किसी डिवाइस पर क्लाइंट ऐप्लिकेशन इंस्टॉल होने के बाद, वह FCM APNs इंटरफ़ेस के ज़रिए मैसेज पा सकता है. सूचना कंपोज़र की मदद से, उपयोगकर्ता सेगमेंट को सूचनाएं तुरंत भेजी जा सकती हैं. इसके अलावा, ऐप्लिकेशन सर्वर पर बनाए गए मैसेज भी भेजे जा सकते हैं.

सूचनाएं मैनेज करना

FCM, APNs के ज़रिए Apple ऐप्लिकेशन को टारगेट करने वाले सभी मैसेज डिलीवर करता है. UNUserNotificationCenter का इस्तेमाल करके APNs सूचनाएं पाने के बारे में ज़्यादा जानने के लिए, सूचनाएं और सूचनाओं से जुड़ी कार्रवाइयां मैनेज करना के बारे में Apple का दस्तावेज़ देखें.

आपको UNUserNotificationCenter delegate सेट करना होगा. साथ ही, FCM से डिसप्ले सूचनाएं पाने के लिए, डेलिगेट के सही तरीके लागू करने होंगे.

Swift

// Receive displayed notifications for iOS 10+ devices.
func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification) async
  -> UNNotificationPresentationOptions {
  let userInfo = notification.request.content.userInfo

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

  // ...

  // Print full message.
  print(userInfo)

  // Change this to your preferred presentation option
  // Note: UNNotificationPresentationOptions.alert has been deprecated.
  return [.list, .banner, .sound]
}

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse) async {
  let userInfo = response.notification.request.content.userInfo

  // ...

  // Print full message.
  print(userInfo)
}

Objective-C

// Receive displayed notifications for iOS 10+ devices.
// Handle incoming notification messages while app is in the foreground.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
  NSDictionary *userInfo = notification.request.content.userInfo;

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

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

  // Change this to your preferred presentation option
  completionHandler(UNNotificationPresentationOptionList |
                    UNNotificationPresentationOptionBanner |
                    UNNotificationPresentationOptionSound);
}

- (void)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken {
    NSLog(@"FCM registration token: %@", fcmToken);
    // Notify about received token.
    NSDictionary *dataDict = [NSDictionary dictionaryWithObject:fcmToken forKey:@"token"];
    [[NSNotificationCenter defaultCenter] postNotificationName:
     @"FCMToken" object:nil userInfo:dataDict];
    // TODO: If necessary send token to application server.
    // Note: This callback is fired at each app startup and whenever a new token is generated.
}

- (void)logFCMToken {
  NSString *fcmToken = [FIRMessaging messaging].FCMToken;
  NSLog(@"Local FCM registration token: %@", fcmToken);

  NSString* displayToken = [NSString stringWithFormat:@"Logged FCM token: %@", fcmToken];

  [[FIRMessaging messaging] tokenWithCompletion:^(NSString * _Nullable token, NSError * _Nullable error) {
    if (error != nil) {
      NSLog(@"Error fetching the remote FCM registration token: %@", error);
    } else {
      NSLog(@"Remote FCM registration token: %@", token);
      NSString* message =
        [NSString stringWithFormat:@"FCM registration token: %@", token];
      // display message
      NSLog(@"%@", message);
    }
  }];
  NSLog(@"%@", displayToken);
}

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

@end

अगर आपको सूचनाओं में कस्टम कार्रवाइयां जोड़नी हैं, तो click_action पैरामीटर को सूचना पेलोड में सेट करें. APNs पेलोड में category कुंजी के लिए इस्तेमाल की जाने वाली वैल्यू का इस्तेमाल करें. कस्टम ऐक्शन का इस्तेमाल करने से पहले, उन्हें रजिस्टर करना ज़रूरी है. ज़्यादा जानकारी के लिए, Apple की Local and Remote Notification Programming Guide देखें.

अपने ऐप्लिकेशन पर मैसेज डिलीवर होने की जानकारी पाने के लिए, FCM रिपोर्टिंग डैशबोर्ड देखें. यह डैशबोर्ड, Apple और Android डिवाइसों पर भेजे गए और खोले गए मैसेज की संख्या रिकॉर्ड करता है. साथ ही, Android ऐप्लिकेशन के लिए "इंप्रेशन" (उपयोगकर्ताओं को दिखने वाली सूचनाएं) का डेटा भी रिकॉर्ड करता है.

साइलेंट पुश नोटिफ़िकेशन मैनेज करना

content-available कुंजी (APNs के content-available के बराबर) का इस्तेमाल करके मैसेज भेजने पर, मैसेज साइलेंट सूचनाओं के तौर पर डिलीवर किए जाएंगे. इससे आपका ऐप्लिकेशन बैकग्राउंड में चालू हो जाएगा, ताकि बैकग्राउंड में डेटा रीफ़्रेश करने जैसे टास्क पूरे किए जा सकें. फ़ोरग्राउंड सूचनाओं के उलट, इन सूचनाओं को application(_:didReceiveRemoteNotification:fetchCompletionHandler:) तरीके का इस्तेमाल करके मैनेज किया जाना चाहिए.

application(_:didReceiveRemoteNotification:fetchCompletionHandler:) को इस तरह लागू करें:

Swift

@MainActor
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 full message.
  print(userInfo)
  print("Call exportDeliveryMetricsToBigQuery() from AppDelegate")
  Messaging.serviceExtension().exportDeliveryMetricsToBigQuery(withMessageInfo: userInfo)
  return UIBackgroundFetchResult.newData
}

Objective-C

- (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 until 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);
}

Apple प्लैटफ़ॉर्म, बैकग्राउंड में सूचनाएं मिलने की गारंटी नहीं देते. बैकग्राउंड में सूचनाएं न मिलने की वजहों के बारे में जानने के लिए, Apple के इस दस्तावेज़ को पढ़ें: Pushing Background Updates to Your App.

सूचना वाले मैसेज के पेलोड की व्याख्या करना

सूचना वाले मैसेज का पेलोड, कुंजियों और वैल्यू की डिक्शनरी होती है. APNs के ज़रिए भेजे गए सूचना वाले मैसेज का APNs पेलोड फ़ॉर्मैट यह होता है:

  {
    "aps" : {
      "alert" : {
        "body" : "great match!",
        "title" : "Portugal vs. Denmark",
      },
      "badge" : 1,
    },
    "customKey" : "customValue"
  }

मेथड स्विज़लिंग की सुविधा बंद होने पर मैसेज मैनेज करना

डिफ़ॉल्ट रूप से, अगर आपने अपने ऐप्लिकेशन के ऐप्लिकेशन डेलिगेट क्लास को UNUserNotificationCenter और Messaging डेलिगेट प्रॉपर्टी असाइन की हैं, तो FCM आपके ऐप्लिकेशन डेलिगेट क्लास को स्विज़ल करेगा. इससे आपके FCM टोकन को डिवाइस के APNs टोकन के साथ अपने-आप जोड़ा जा सकेगा. साथ ही, सूचना मिलने के इवेंट को Analytics पर भेजा जा सकेगा. अगर आपने मेथड स्विज़लिंग की सुविधा को साफ़ तौर पर बंद कर दिया है, SwiftUI ऐप्लिकेशन बनाया है या किसी भी डेलिगेट के लिए अलग क्लास का इस्तेमाल किया है, तो आपको इन दोनों टास्क को मैन्युअल तरीके से पूरा करना होगा.

FCM टोकन को डिवाइस के APNs टोकन से जोड़ने के लिए, अपने ऐप्लिकेशन डेलिगेट के टोकन रीफ़्रेश हैंडलर में Messaging क्लास को APNs टोकन पास करें. इसके लिए, apnsToken प्रॉपर्टी का इस्तेमाल करें.

Swift

func application(_ application: UIApplication,
    didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  Messaging.messaging().apnsToken = deviceToken;
}

Objective-C

- (void)application:(UIApplication *)application
    didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  [FIRMessaging messaging].APNSToken = deviceToken;
}

सूचना मिलने की जानकारी को Analytics पर भेजने के लिए, appDidReceiveMessage(_:) तरीके का इस्तेमाल करें.

Swift

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
  withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
  let userInfo = notification.request.content.userInfo

  Messaging.messaging().appDidReceiveMessage(userInfo)

  // Change this to your preferred presentation option
  completionHandler([[.alert, .sound]])
}

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) {
  let userInfo = response.notification.request.content.userInfo

  Messaging.messaging().appDidReceiveMessage(userInfo)

  completionHandler()
}

func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
  fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
  Messaging.messaging().appDidReceiveMessage(userInfo)
  completionHandler(.noData)
}

Objective-C

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
      willPresentNotification:(UNNotification *)notification
        withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
  NSDictionary *userInfo = notification.request.content.userInfo;

  [[FIRMessaging messaging] appDidReceiveMessage:userInfo];

  // Change this to your preferred presentation option
  completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionAlert);
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
        withCompletionHandler:(void(^)(void))completionHandler {
  NSDictionary *userInfo = response.notification.request.content.userInfo;

  [[FIRMessaging messaging] appDidReceiveMessage:userInfo];

  completionHandler();
}

- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
  [[FIRMessaging messaging] appDidReceiveMessage:userInfo];
  completionHandler(UIBackgroundFetchResultNoData);
}