Apple के ऐप्लिकेशन में मैसेज पाना

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

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

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

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

Swift


extension AppDelegate: UNUserNotificationCenterDelegate {
  // 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
    return [[.alert, .sound]]
  }

  func userNotificationCenter(_ center: UNUserNotificationCenter,
                              didReceive response: UNNotificationResponse) async {
    let userInfo = response.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)
  }
}

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

// Handle notification messages after display notification is tapped by the user.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void(^)(void))completionHandler {
  NSDictionary *userInfo = response.notification.request.content.userInfo;
  if (userInfo[kGCMMessageIDKey]) {
    NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]);
  }

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

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

  completionHandler();
}

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

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

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

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

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

Swift

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
}

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

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

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

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

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

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

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

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

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