किसी डिवाइस पर क्लाइंट ऐप्लिकेशन इंस्टॉल होने के बाद, वह FCM APNs इंटरफ़ेस के ज़रिए मैसेज पा सकता है. सूचना कंपोज़र की मदद से, उपयोगकर्ता सेगमेंट को सूचनाएं तुरंत भेजी जा सकती हैं. इसके अलावा, ऐप्लिकेशन सर्वर पर बनाए गए मैसेज भी भेजे जा सकते हैं.
सूचनाएं मैनेज करना
FCM, APNs के ज़रिए Apple ऐप्लिकेशन को टारगेट करने वाले सभी मैसेज डिलीवर करता है. UNUserNotificationCenter के ज़रिए APNs सूचनाएं पाने के बारे में ज़्यादा जानने के लिए, Apple का यह दस्तावेज़ देखें: सूचनाएं और सूचनाओं से जुड़ी कार्रवाइयां मैनेज करना.
आपको UNUserNotificationCenter delegate सेट करना होगा. साथ ही, FCM से डिसप्ले सूचनाएं पाने के लिए, डेलिगेट के सही तरीके लागू करने होंगे.
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
पैरामीटर सेट करें.
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
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 के दस्तावेज़ देखें. ये दस्तावेज़ अपने ऐप्लिकेशन में बैकग्राउंड अपडेट पुश करने के बारे में हैं.
सूचना मैसेज के पेलोड की व्याख्या करना
सूचना वाले मैसेज का पेलोड, कुंजियों और वैल्यू की डिक्शनरी होती है. 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); }