Apple प्लैटफ़ॉर्म के ऐप्लिकेशन में मैसेज पाना

प्लैटफ़ॉर्म चुनें: iOS+ Android Web Flutter Unity C++


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

चेतावनी वाली सूचनाएं मैनेज करना

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

FCM से डिसप्ले सूचनाएं पाने के लिए, आपको UNUserNotificationCenter डेलिगेट सेट करना होगा और डेलिगेट के सही तरीके लागू करने होंगे 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 का लोकल और रिमोट सूचना प्रोग्रामिंग गाइड देखें.

अपने ऐप्लिकेशन को मैसेज डिलीवर होने की जानकारी पाने के लिए, 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 के दस्तावेज़ देखें.

सूचना वाले मैसेज के पेलोड को समझना

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

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

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

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

FCM टोकन को डिवाइस के APNs टोकन से जोड़ने के लिए, APNs टोकन को अपने ऐप्लिकेशन डेलिगेट के टोकन रीफ़्रेश हैंडलर Messaging क्लास में 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 को सूचना मिलने की जानकारी पास करने के लिए, 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);
}